Delete old directories

Delete all directories more than a week old

This simple linux bash script will delete subdirectories in a directory based on when the subdirectory was last modified.
In my sample script, it looks in the directory /home/backup and deletes any directories older than 7 days. Replace 7 with the number of days of your choosing!

for i in `find /home/backup/ -maxdepth 1 -type d -mtime +7 -print`; do echo -e "Deleting directory $i";rm -rf $i; done

Just to explain:

* -maxdepth 1 = list only files/directories in 1 level from main search directory
* -type d = list only directories
* -mtime +7 = modified time of more than 7 days
* -print = print out list; so we can then process the list with our bash script

This is useful for backup programs which do not clean up their old backups.

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.