How to delete expired files periodically under Linux

Source: Internet
Author: User

When managing files under Linux, we usually have the need to delete expired files on a regular basis. For example, scheduled to delete more than the specified time log files, or longer, your log files occupy the larger space, that is not far from exploding disk, but also to address the needs of the site convenient, such as our established Taobao Guest API program, in order to improve the speed of access to merchandise pages, we need to carry out the cache of key data, Also need to periodically delete expired cache files, if the use of PHP program scan Delete, it will not outweigh the gains. So how to periodically delete expired files under Linux? Here is a simple introduction to the small, convenient to need friends.

Deleting expired files under Linux is simple, with the following command:

Copy code code as follows:

find/var/www/-type f-mtime +30-exec rm-f ' {}; '

The above command deletes expired files that were 30 days old.

Command Description:

/var/www/is the path to find the file.

-type f is the specified file type as a normal file.

-mtime +30 refers to files that are modified for a period of 30 days from now.

-exec rm-f refers to the execution of delete matching files and does not prompt, some systems need to have single quotes, and some do not need.

The above command, added to the crontab, can be implemented periodically deleted.

For example, 1 30 minutes a day to implement the deletion, you can do this:

Copy code code as follows:

#crontab-E

1 * * * find/var/www/-type f-mtime +30-exec rm-f ' {}; '

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.