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. Let me know if you have any questions or comments!

6 comments
  1. I think you meant to have this ..

    for i in `find /home/backup/ -maxdepth 1 -type d -mtime +7 -print`
    do echo -e “Deleting directory $i” #you have “in” not “i”
    rm -rf $i
    done

    also you can just use a find one liner:

    find /home/backup/ -maxdepth 1 -type d -mtime +7 -exec echo “Removing Directory => {}” ; -exec rm -rf “{}” ;

    Keep in mind that -mtime n is rounded up to the next *full* 24-hour period. So for e.g -mtime +0 would find the directories that have not been modified starting with the day before yesterday.

  2. I think you meant to have this ..

    for i in `find /home/backup/ -maxdepth 1 -type d -mtime +7 -print`
    do echo -e “Deleting directory $i” #you have “in” not “i”
    rm -rf $i
    done

    also you can just use a find one liner:

    find /home/backup/ -maxdepth 1 -type d -mtime +7 -exec echo “Removing Directory => {}” \; -exec rm -rf “{}” \;

    Keep in mind that -mtime n is rounded up to the next *full* 24-hour period. So for e.g -mtime +0 would find the directories that have not been modified starting with the day before yesterday.

  3. That is correct – I actually have it with a n (newline character) but wordpress stripped it out of my post. using $i there is correct.

  4. That is correct – I actually have it with a \n (newline character) but wordpress stripped it out of my post. using $i there is correct.

Comments are closed.

You May Also Like

Apt-get Update GPG Key Errors and Fix

Running sudo apt-get upgrade, I started getting this error: Reading package lists……

HOWTO: Installing ZFS and setting up a Raid-Z array on Ubuntu

Readers should note that this applies to Ubuntu 8.10 Intrepid Ibex only!…

A phpBB 3 iPhone Style Theme With Option to Disable

A forum that I am an adminstrator for has been clamoring for an iPhone theme (style) for a long time now. In the past, I hadn’t seen any usable iPhone template for phpBB3, until now. View this entire post to view my modifications to a theme switcher for mobile devices, so that the end user can disable a mobile theme for their login if they should choose to do so.

Ubuntu Server in Place Network Upgrade From 8.10 to 9.04

It is easy to do an in-place upgrade of Ubuntu Server from…