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 that 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 .