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?
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 {} +
-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
find /path/to/base/dir/*
.
Commented
Jul 5, 2015 at 8:28
-mindepth 1
(rather than /path/to/folder/*
).
Commented
Feb 13, 2017 at 16:27
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
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.
-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
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
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
xargs --show-limits
.
Commented
Oct 9, 2015 at 12:43
-print0
/-0
take care of the special shell characters, or no?
xargs
version will @mpen, but the first line won't.
Commented
May 13, 2017 at 22:19
find
could do it without looking at the name then...ctime
is the inode change time. For a directory, it changes when files are added or removed from the directory.