1. Delete File command:
Find corresponds to directory-mtime + days-name "filename"-exec rm-rf {} \;
Instance command:
find/opt/soft/log/-mtime +30-name "*.log"-exec rm-rf {} \;
Description
Delete all files with ". Log" 30 days ago in the/opt/soft/log/directory. The specific parameters are described as follows:
Find:linux Lookup command, the user looks for the specified condition of the file;
/opt/soft/log/: Any directory that you want to clean;
-mtime: standard sentence formulation;
+30: Find 30 days before the file, where the number of days to represent;
"*.log": the type of data you want to find, "*.jpg" means to find all the files with the extension jpg, "*" to find all the files, this can be used flexibly, extrapolate;
-exec: fixed wording;
RM-RF: Force delete file, including directory;
{} \; : fixed wording, a pair of curly braces + space +\+;
2. Planning tasks:
If it is too troublesome to manually execute a statement each time, you can write this small statement into an executable shell script file, and then set up cron scheduling execution, which allows the system to 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 permissions to run
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 exit (: Wq).
2.3 Scheduled Tasks:
#crontab-E
Add the auto-del-30-days-ago-log.sh execution script to the System planning task, which is automatically executed
Input:
0 * * */opt/soft/log/auto-del-7-days-ago-log.sh >/dev/null 2>&1
The setting here is to perform the data Cleanup task on the auto-del-7-days-ago-log.sh file 0:10 A.M. every day.