Linux automatically deletes logs and example commands from n days ago

Linux automatically deletes logs and example commands from n days ago

1. Delete file command:

find the corresponding directory -mtime + number of days -name "file name" -exec rm -rf {} \;

Example command:

find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} \;

illustrate:

Delete all files with ".log" in the /opt/soft/log/ directory that are older than 30 days. The specific parameters are described as follows:

find: Linux search command, users search for files with specified conditions;

/opt/soft/log/: any directory you want to clean;

-mtime: standard statement writing method;

+30: Search for files 30 days ago, where numbers represent the number of days;

"*.log": the type of data you want to find, "*.jpg" means to find all files with the extension jpg, "*" means to find all files, this can be used flexibly, and you can draw inferences from one example;

-exec: fixed writing;

rm -rf: forcefully delete files, including directories;

{} \; : Fixed writing method, a pair of curly brackets + space + \+;

2. Planned tasks:

If you think it is too troublesome to execute the statement manually every time, you can write this small statement into an executable shell script file, and then set up cron scheduling execution, so that the system can automatically clean up related files.

2.1 Create a shell:

touch /opt/soft/bin/auto-del-30-days-ago-log.sh
chmod +x auto-del-30-days-ago-log.sh

Create a new executable file auto-del-30-days-ago-log.sh and assign it executable permissions

2.2 Edit the shell script:

vi auto-del-30-days-ago-log.sh

Edit the auto-del-30-days-ago-log.sh file as follows:

#!/bin/sh
find /opt/soft/log/ -mtime +30 -name "*.log" -exec rm -rf {} \;

OK, save and exit (:wq).

2.3 Planned tasks:

#crontab -e

Add the auto-del-30-days-ago-log.sh execution script to the system scheduled tasks and execute it automatically at a certain time

enter:

10 0 * * * /opt/soft/log/auto-del-7-days-ago-log.sh >/dev/null 2>&1

The setting here is to execute the auto-del-7-days-ago-log.sh file at 0:10 am every day to perform data cleaning tasks.

After completing the above three steps, you will no longer have to worry about whether the hard disk space is full and it is time to clean up the log files. You will no longer receive alarm messages about insufficient hard disk space on the server. You can go read a book and drink coffee with peace of mind!

Each time you add a task, be sure to restart the crond service, otherwise it will not take effect

The code is as follows:

service crond restart

Creating Your First Shell Script

1. Write a script

a) Create a file using the touch command: touch my_script

b) Open the my_script file with the vim editor: vi my_script

c) Edit the my_script file with the vim editor. The content is as follows:

#!/bin/bash tells the shell what program to use to interpret the script #My first script
ls -l .*

2. Allow Shell to execute it

chmod 755 my_script

3. Execute the my_script script

./my_script

Summarize

The above is what I introduced to you about Linux automatic deletion of logs n days ago. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Linux uses scheduled tasks to clean up logs 45 days ago every week
  • Scheduled cutting of Tomcat logs in Linux and deleting log records older than a specified number of days
  • How to write a script to cut nginx logs every day under Linux system
  • Detailed explanation of the process of log timing polling under Linux
  • Scheduled cutting of Mongodb database logs in Linux and deleting log records older than a specified number of days
  • Code to delete logs 7 days ago under Linux (php+shell)
  • How to automatically delete archive log files under Linux
  • A simple method to implement Linux timed log deletion

<<:  MySQL 5.7.17 installation and configuration method graphic tutorial under win7

>>:  Two ways to declare private variables in JavaScript

Recommend

Vue uses openlayers to load Tiandi Map and Amap

Table of contents 1. World Map 1. Install openlay...

How to start and restart nginx in Linux

Nginx (engine x) is a high-performance HTTP and r...

Summary of commonly used escape characters in HTML

The commonly used escape characters in HTML are s...

Implementation example of Docker rocketmq deployment

Table of contents Preparation Deployment process ...

js to achieve simple accordion effect

This article shares the specific code of js to ac...

How to add vim implementation code examples in power shell

1. Go to Vim's official website to download t...

Pure CSS to achieve cool neon light effect (with demo)

I have recently been following the CSS Animation ...

Introduction to the use of common Dockerfile commands

Table of contents 01 CMD 02 ENTRYPOINT 03 WORKDIR...

How to quickly build ELK based on Docker

[Abstract] This article quickly builds a complete...

Explain TypeScript mapped types and better literal type inference

Table of contents Overview Using mapped types to ...

Method for comparing the size of varchar type numbers in MySQL database

Create a test table -- --------------------------...

Specific use of lazy loading and preloading in js

Delayed loading (lazy loading) and preloading are...

JS calculates the probability of winning based on the prize weight

Table of contents 1. Example scenario 1.1. Set th...