Delete files older than X days +

I have found the command to delete files older than 5 days in a folder

find /path/to/files* -mtime +5 -exec rm {} ;

But how do I also do this for subdirectories in that folder?

Asked By: Teddy77


It’s the same. You just have to provide the parent directory rather than the prefix of files. In your example, it would be:

find /path/to -type f -mtime +5 -exec rm {} ;

This will delete all the files older than 5 days which are under /path/to and its sub-directories.

To delete empty sub-directories, refer to @Costas comment above.

Answered By: apaul

Be careful with special file names (spaces, quotes) when piping to rm.

There is a safe alternative – the -delete option:

find /path/to/directory/ -mindepth 1 -mtime +5 -delete

That’s it, no separate rm call and you don’t need to worry about file names.

Replace -delete with -depth -print to test this command before you run it (-delete implies -depth).


  • -mindepth 1: without this, . (the directory itself) might also
    match and therefore get deleted.
  • -mtime +5: process files whose
    data was last modified 5*24 hours ago.
Answered By: basic6

Note that this command will not work when it finds too many files. It will yield an error like:

bash: /usr/bin/find: Argument list too long

Meaning the exec system call’s limit on the length of a command line was exceeded. Instead of executing rm that way it’s a lot more efficient to use xargs. Here’s an example that works:

find /root/Maildir/ -mindepth 1 -type f -mtime +14 | xargs rm

This will remove all files (type f) modified longer than 14 days ago under /root/Maildir/ recursively from there and deeper (mindepth 1). See the find manual for more options.

Answered By: Julius
find . -mtime +3 -type f -not -name '*pid*' |xargs rm -rf
Answered By: Susil

Disclaimer: I’m the current author of rawhide (rh) (see

It probably makes the most sense to delete empty directories after deleting files that are older than X days. The modification time of directories is when entries were added or deleted from them. Deleting old files will update the modification time of the directory that contained them to the time of the deletion. So, you don’t want to refer to the modification time of the directories themselves. Just delete empty directories. Deleting files at least 5 days old, and then deleting the resulting empty directories can be done with:

rh -UUU /path/to 'f && old(5*days)
rh -UUU /path/to 'd && empty'

-UUU unlinks/removes/deletes the matching files.

The rest is the search criteria.

f matches regular files.

old(5*days) matches files modified at least 5 days ago.

d matches directories.

empty matches empty directories (and regular files of zero size).

It can also be done with find, of course:

find . -type d -empty -delete

As stated by Costas.

Answered By: raf
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.