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

Source: Internet
Author: User

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)

Related Article

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.