9

Possible Duplicate:
How do I delete files greater than a certain date on linux

How to delete all files in current directory and it`s sub directories older than one year?

marked as duplicate by Nifle, Sathyajith Bhat, Dennis Williamson, Doug Harris, BinaryMisfit Sep 24 '10 at 21:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Sep 24 '10 at 19:09

This question came from our site for professional and enthusiast programmers.

  • 1
    Do man find. Boom. – mkoistinen Sep 24 '10 at 19:08
  • 1
    Note to closers: that other question is actually confusingly different, its answers aren't completely straightforward to transpose. – Gilles Sep 24 '10 at 21:16
15
find /the/dir/to/start/in -type f -mtime +365 -ls -exec rm -f -- {} \;
  • 2
    find /path/to/files -type f -mtime +365 -delete would be easier. – Chris S Sep 24 '10 at 19:38
  • -delete isn't in my aix find so I'm not accustomed to using it. I'm glad its implemented in other find binaries though. – bot403 Sep 24 '10 at 19:41
  • find … -exec rm -f {} + will be a little faster (and it's portable except to ancient systems). – Gilles Sep 24 '10 at 21:17
  • 1
    Also, it's a good idea to use -- in case the first file name begins with a - (although you can guarantee it won't happen if the directory passed to find doesn't begin with a -). – Gilles Sep 24 '10 at 21:22
  • This is extremely dangerous as you don't see what you are about to delete and why in the world are you force deleting with -f? Some files are protected for a reason. It's much more safe to store the results of find into a file, then examine them and run rm on contents of the file. – Petr Aug 27 '16 at 10:07
2

If you are removing lots of files this is typically a lot faster than "-exec" or piping to "xargs":

find . -type f -mtime +365 | perl -lne unlink