Delete Files Older Than x Days on Linux

find /home/ftp/services/* -mtime +5 -exec rm {} \;

  • 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.

find /home/ftp/services/* -type f -name ‘*.jpg’ -mtime +30 -exec rm {} \;

find /home/ftp/services/* -mtime +180 -size +1G -exec rm {} \;

find /home/ftp/services/* -mtime +180 -size +100M -exec rm {} \;

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

extradrmtech

Since 20 years I work on Database Architecture and data migration protocols. I am also a consultant in Web content management solutions. I am an experienced web-developer with over 10 years developing PHP/MySQL, C#, VB.Net applications ranging from simple web sites to extensive web-based business applications. When not writing code, I like to dance salsa and swing and have fun with my little family.

You may also like...