Have you ever wondered if you need to clean up the old file/old email on server every week or month,Here the following article will share to you how to use Scheduled Command to clean up the old files .We’ll use this in order to figure out what files are older than a certain number of days, and then use the rm command to delete them.

Command Syntax:

find /path/to/files* -mtime +5 -exec rm {} ;

Note that there are spaces between rm, {}, and ;

  • The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
  • The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
  • The third argument, -exec, allows you to pass in a command such as rm. The {} ; at the end is required to end the command.

This should work on Ubuntu,Centos, Redhat, or pretty much any version of linux.

Above completed the Scheduled step, now need configure with Crontab

Step 1:

crontab -e

Step 2:

5 2 * * 0 find /home/*/homes/bounce/Maildir/cur/. -mtime +3 -exec rm {} ;

Crontab Description:

Minute  Hour   Day of Month   Month   Day of Week     Command
(0-59)   (0-23)      (1-31)             (1-12)          (0-6)
5             2              *                     *                   0               find /home/.

Here is my crontab rule.

Share this to your if you found this article help you.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *