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?

  • Do you mean files inside the sub directories? or the sub directories themselves? – rahul Apr 7 '15 at 16:35
  • 2
    find /path/to -type d -empty -delete – Costas Apr 7 '15 at 16:39
  • 1
    Delete files in subdirectories that are also 5+ days old – Teddy291 Apr 7 '15 at 16:46
  • 2
    Possibly fun when I have files with spaces. E.g a file called "test one" and rm gets fed rm test one. (Which will delete a file called "test" and a file called "one", but not a file called "test one"). Hint: -delete or -print0 – Hennes Apr 7 '15 at 17:17
  • 3
    As a side note, always quote the argument provided by find to avoid issues with special characters, as mentioned in the answer's first line. E.g.: find /path/to/files/ -exec somecommand '{}' \; – Walf Jun 17 '16 at 1:54
up vote 204 down vote accepted

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

  • 17
    Also use -type f to delete files only (and keep sub directories) – Oleg Mar 4 '16 at 8:44
  • 2
    Alternatively, if you want to do the same for all files NEWER than five days: find /path/to/directory/ -mindepth 1 -mtime -5 -delete – zmonteca Apr 19 '16 at 17:29
  • If my path contains spaces how should I do it? find /path/to/dir\ with\ spaces/ -mindepth 1 -mtime +5 -delete? – uom-pgregorio May 23 '17 at 19:59
  • 2
    @uom-pgregorio I would suggest putting the path in quotes. – atripes Oct 6 '17 at 14:40
  • I have a cronjob running once a week to delete those scratch files than inevitably accumulate on the disk: find /path/to/scratch/space -mtime +60 -print -delete. So from time to time cron emails me telling me which files I just lost ... – Rolf Mar 20 at 8:13

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.

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.

  • 4
    Note that for each and every file you will execute the rm command. If you have 1000 files older than 5 days then rm will get started 1000 times. For this reason consider the -delete option as in Costa's comment or -exec rm {} \+ – Hennes Apr 7 '15 at 17:14
  • @Hennes: -- 1) not sure you need to escape + in that case. -- 2) better to write -exec rm '{}' + to fend off the evil of files with special characters (spaces, newlines, etc...) in their name. – Cbhihe Jul 12 '16 at 9:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.