find and delete file or folder older than x days

You can make use of this piece of code

find /tmp/* -mtime +7 -exec rm {} \;

Explanation

  • 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 +7, it will find files older than 7 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.

Source : http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/

For deleting folders, after emptying inside of them you can rmdirinstad of rm in the piece of code, also if you only want to see directories you can add

-type d

to piece of code such as below:

find /tmp/*/* -mtime +7 -type d -exec rmdir {} \;

Leave a Comment