Wednesday, December 24, 2014

Delete files that are X days old


                            Some time in Linux you want to clear out older files in a directory.so here is the command that will delete all files all the files that are older than seven days.

Let us look at the above command

find: This is the command that will search for the files.
/path/to/files :This is the top level directory to start searching.
-type f : This ensures we don't remove directories,but only files.
-mtime +7 : Removes files older than '7' days.Change to '+14' to delete files older than two weeks
-exec : This indicates what to do with the files we found.
rm -rf : Removes the files recursively and forcefully.
{}: This represents each file we find.
\;  : This is the end of the exec

                    You can also create a cron job to automate the process.The below crontab file will execute the above command every night at 2 am and delete all files older than 7 days in the folder.





No comments:

Post a Comment