In the Linux/unix system, our application generates log files every day, the application and database are backed up every day, log files and backup files accumulate a lot of storage space for a long time, and some log and backup files do not need to be retained for a long time, usually keep the files within 7 days, So how do we find and delete the log files and backup files that were generated 7 days ago and delete them?
Linux/unix provides a Find operating system command that can be used to achieve our goals.
$man find to see how you can use the Find command.
1. Find the file N days ago
$find/temp/-type f-mtime +n-print
Note:/temp/indicates the search for files under the/temp/directory
-type F to find the system normal file, do not include directory files
-mtime +n pointed out the file for n*24 hours ago
-print to print out the found files
For example: Find documents 7 days ago
$find/temp/-type f-mtime +7-print
Find the file 3 days ago
find/temp/-type F-mtime +3-print
2. Find and delete files from 7 days ago
$find/temp/-type f-mtime +7-print-exec rm-f {} \;
Note:-exec indicates that the following system commands are to be executed
Rm-f Delete the found file
{} only the symbol can be followed by the command
\ Terminator
3. You can also use Xargs instead of-exec
$find/temp/-type f-mtime +7-print | Xargs rm-f
examples of use of the Find command:
Such as:
* Find the largest top 10 files under/var:
$ Find/var-type F-ls | Sort-k 7-r-N | Head-10
* Find files larger than 5GB under/var/log/:
$ find/var/log/-type f-size +5120m-exec ls-lh {} \;
* Find all today's files and copy them to another directory:
$ find/home/me/files-ctime 0-print-exec CP {}/mnt/backup/{} \;
* Find out all the temporary files from one week ago and delete them:
$ find/temp/-mtime +7-type F | Xargs/bin/rm-f
* Find all the MP3 files and modify all uppercase letters to lowercase:
$ find/home/me/music/-type f-name *.mp3-exec rename ' y/[a-z]/[a-z]/' {} ' \;
Linux/unix how to find and delete a file at a point in time (go)