Nov.12

Linux Tips and Tricks

Delete files that are ‘X’ days old

Sometimes ins linux, you want to clear out older files in a directory. One example could be if you have a security system and it continuously writes video files to a directory on your NAS until it fills it up.you have figured out that it keep a week’s collections of videos, there will be plenty of space for other users. So, here is a command that will delete all files that are older than seven days . So remember to execute this command with caution as it can delete your important data if not used correctly

 

#find /path/to/files/ -type f -mtime +7 -exec rm -rf {} \;

 

find : This command will search for 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 files we found.
rm -rf  :  Removes the files recursively and forcefully.
{} : This represents each file we find.
\; : This is the end of exec.
 
On successfully testing the above command you can create a Cron job to automate this task

Share this Story:
  • facebook
  • twitter
  • gplus

About bentech4u

Comments(1)

  1. varghese
    3448 days ago

    If it is a new invention its great,then proceed Ben…

Leave a Reply to varghese Cancel reply