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 ' {}; '