Linux is a very automatic file generation system, log, mail, backup and so on. Although now hard disk cheap, we can have a lot of hard disk space for these files wasted, so that the system regularly clean up some unnecessary files there is a refreshing thing. Do not have to worry about whether you need to clear the log every day, do not have to receive the hard disk space of the alarm SMS, want to have a good rest, let us put this matter to the machine timed to execute it.
1. Delete File command:
Find corresponding directory-mtime + days-name "file name"-exec rm-rf {} \;
Instance command:
find/opt/soft/log/-mtime +30-name "*.log"-exec rm-rf {} \;
Description
Remove all files with ". Log" 30 days ago in the/opt/soft/log/directory. The specific parameters are described as follows:
Find:linux the search command, the user finds the file of the specified condition;
/opt/soft/log/: Any directory for which you want to clean up;
-mtime: standard sentence notation;
+30: Find the file 30 days ago, here the number of days to represent;
"*.log": the type of data you want to find, "*.jpg" means to find all files with the extension jpg, "*" means to find all the files, this can be used flexibly, extrapolate;
-exec: fixed notation;
RM-RF: Forced deletion of files, including directories;
{} \; : fixed notation, pair of curly braces + space +\+;
2. Scheduled Tasks:
If it is too troublesome to manually execute the statement every time, you can write this small statement into an executable shell script file, and then set cron Schedule execution, then you can let the system automatically clean up the relevant files.
2.1 Creating the 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 runnable permissions
2.2 Edit 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 exit (: Wq).
2.3 Scheduled Tasks:
#crontab-E
Add the auto-del-30-days-ago-log.sh execution script to the system scheduled task, to the point of automatic execution
Input:
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 for the Data Cleanup task 0:10 A.M. every day.
Complete the above three steps, you will never worry about whether the hard disk space is full, the cleanup log file, and will not be the server hard disk space for alarm information, rest assured to read a book Drink coffee!
Linux log automatic Cleanup (find delete data 30 days ago)