Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to delete directories older than n days

Tags:

bash

shell

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?

like image 736
bobsr Avatar asked Dec 13 '12 21:12

bobsr


People also ask

How do I remove 30 days old files in UNIX?

-mtime +30 : This refers to all the files which are older than 30 days. mtime stands for Modification time in Unix. You can change the number based on your requirement. -exec rm {} \ : This is actually the execution command which calls for deletion of all the files filtered by all the above criteria.

How do I delete files older than a certain date in Linux?

For just files find /path ! -type f -newermt "YYYY-MM-DD HH:MM:SS" -delete .


2 Answers

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 {} + 
like image 117
sampson-chen Avatar answered Sep 22 '22 14:09

sampson-chen


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 
like image 25
pmgarvey Avatar answered Sep 18 '22 14:09

pmgarvey