Linux/unix how to find and delete a file at a point in time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.