214

I have directories named as:

2012-12-12
2012-10-12
2012-08-08

How would I delete the directories that are older than 10 days with a bash shell script?

3
  • 1
    To they bare any relation on their actual creation/modification time? Because find could do it without looking at the name then...
    – Wrikken
    Commented Dec 13, 2012 at 21:33
  • they should have creation/modification time on them as well
    – bobsr
    Commented Dec 13, 2012 at 21:41
  • 8
    What do you mean by "older than"? Are you referring to the time the directory was created, the time its contents were last changed, or something else? Be careful with some of the answers below; ctime is the inode change time. For a directory, it changes when files are added or removed from the directory.
    – ajk
    Commented Feb 20, 2014 at 11:36

5 Answers 5

506

This will do it recursively for you:

find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf {} \;

Explanation:

  • find: the unix command for finding files / directories / links etc.
  • /path/to/base/dir: the directory to start your search in.
  • -type d: only find directories
  • -ctime +10: only consider the ones with modification time older than 10 days
  • -exec ... \;: for each such result found, do the following command in ...
  • rm -rf {}: recursively force remove the directory; the {} part is where the find result gets substituted into from the previous part.

Alternatively, use:

find /path/to/base/dir/* -type d -ctime +10 | xargs rm -rf

Which is a bit more efficient, because it amounts to:

rm -rf dir1 dir2 dir3 ...

as opposed to:

rm -rf dir1; rm -rf dir2; rm -rf dir3; ...

as in the -exec method.


With modern versions of find, you can replace the ; with + and it will do the equivalent of the xargs call for you, passing as many files as will fit on each exec system call:

find . -type d -ctime +10 -exec rm -rf {} +
16
  • 19
    -mtime was better for me as it checks content changes rather than permission changes, otherwise this was perfect. Commented Nov 4, 2013 at 17:30
  • 9
    i think this will also delete the base dir itself
    – Or Gal
    Commented Nov 11, 2014 at 10:24
  • 5
    @OrGal you are absolutely correct. In order to prevent this, just use: find /path/to/base/dir/*.
    – zloynemec
    Commented Jul 5, 2015 at 8:28
  • 22
    you can use -maxdepth 1 to ignore contents of the directories
    – Sam
    Commented Sep 27, 2016 at 19:54
  • 9
    The more efficient approach can backfire if you have too many folders to delete: stackoverflow.com/questions/11289551/…. For the same reason, in order to avoid deletion of the base folder it's better to use -mindepth 1 (rather than /path/to/folder/*). Commented Feb 13, 2017 at 16:27
59

If you want to delete all subdirectories under /path/to/base, for example

/path/to/base/dir1
/path/to/base/dir2
/path/to/base/dir3

but you don't want to delete the root /path/to/base, you have to add -mindepth 1 and -maxdepth 1 options, which will access only the subdirectories under /path/to/base

-mindepth 1 excludes the root /path/to/base from the matches.

-maxdepth 1 will ONLY match subdirectories immediately under /path/to/base such as /path/to/base/dir1, /path/to/base/dir2 and /path/to/base/dir3 but it will not list subdirectories of these in a recursive manner. So these example subdirectories will not be listed:

/path/to/base/dir1/dir1
/path/to/base/dir2/dir1
/path/to/base/dir3/dir1

and so forth.

So , to delete all the sub-directories under /path/to/base which are older than 10 days;

find /path/to/base -mindepth 1 -maxdepth 1 -type d -ctime +10 | xargs rm -rf
0
33

find supports -delete operation, so:

find /base/dir/* -ctime +10 -delete;

I think there's a catch that the files need to be 10+ days older too. Haven't tried, someone may confirm in comments.

The most voted solution here is missing -maxdepth 0 so it will call rm -rf for every subdirectory, after deleting it. That doesn't make sense, so I suggest:

find /base/dir/* -maxdepth 0  -type d -ctime +10 -exec rm -rf {} \;

The -delete solution above doesn't use -maxdepth 0 because find would complain the dir is not empty. Instead, it implies -depth and deletes from the bottom up.

1
  • I can confirm -delete works, but like you said you only can use it to delete empty directories, much like rmdir. Commented Jan 9, 2018 at 16:39
3

I was struggling to get this right using the scripts provided above and some other scripts especially when files and folder names had newline or spaces.

Finally stumbled on tmpreaper and it has been worked pretty well for us so far.

tmpreaper -t 5d ~/Downloads


tmpreaper  --protect '*.c' -t 5h ~/my_prg

Original Source link

Has features like test, which checks the directories recursively and lists them. Ability to delete symlinks, files or directories and also the protection mode for a certain pattern while deleting

3

OR

rm -rf `find /path/to/base/dir/* -type d -mtime +10`

Updated, faster version of it:

find /path/to/base/dir/* -mtime +10 -print0 | xargs -0 rm -f
4
  • 2
    This one could easily exceed the maximum commandline length. See xargs --show-limits. Commented Oct 9, 2015 at 12:43
  • 2
    Also won't work if there are filenames containing spaces or other special shell characters. Commented Jan 4, 2017 at 1:16
  • @Carpetsmoker Doesn't the -print0/-0 take care of the special shell characters, or no?
    – mpen
    Commented May 13, 2017 at 22:18
  • You're right that the xargs version will @mpen, but the first line won't. Commented May 13, 2017 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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