Delete files older than a week in Linux

I've just found out that my disk is getting full and the reason for it was the ever growing content in the backup folder. So I figured out why not delete all files older than a week? It turns out that there is a much better way than manually selecting the files to delete. Here is the command to use:

find /path/to/the/files -mtime +7 -exec rm {} \;

  1. The first parameter to find '/path/to/the/files' is simply a path where to start looking for files. Note that this will search recursively so make sure you don't delete any unwanted files because of that. If you want to limit yourself to some depth of folders you want to look into the -maxdepth parameter.
  2. The second parameter '-mtime +7' is the part that does the magic. It simply tells find to select only files that have been modified more than a week ago.
  3. The last parameter '-exec rm {} \;' is the part that does the deleting. The command that does the deleting is 'rm' and the curly braces are replaced with the path of each file that is found by find. The '\;' part tells find where the command passed for execution ends.

This command should work pretty much on any Linux that has a shell and find and rm utilities which, except for some embedded systems, are all Linuxes.

Your thoughts

No comments

Add comment

* - required field