Linux/unix how to find and delete a file at a point in time
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
/Temp/--+-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
/Temp/--+7-print
Find the file 3 days ago
/Temp/--+3-print
2. Find and delete files from 7 days ago
/Temp/--+7-print-exec-{} \;
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
/Temp/--+7-print| -F
Examples of use of the Find command:
Such as:
* Find the largest top 10 files under/var:
/var--| -7--| -Ten
* Find files larger than 5GB under/var/log/:
/var/Log/--+5120M-exec-{} \;
* Find all today's files and copy them to another directory:
/Home/me/-0-print-exec{}/mnt/backup/ {} \;
* Find out all the temporary files from one week ago and delete them:
/Temp/-+7-| /bin/-F
* Find all the MP3 files and modify all uppercase letters to lowercase:
/Home/me/music/--*. -exec' y/[a-z]/[a-z]/'{} ' ;
Linux/unix how to find and delete a file at a point in time