Delete all directories more than a week or X days old

If you are taking the backup via script, you may need to delete your old backups on regular basis to avoid disk space issue. The following command will remove directories from “/home/mydirectory/” which are older more than a month:

for i in `find /home/mydirectory/ -maxdepth 1 -type d -mtime +30 -print`; do echo -e “Deleting directory $i”;rm -rf $i; done

/home/mydirectory/ = Replace with actual path
maxdepth 1 = list only files/directories in 1 level from main search directory
-type d = list only directories
-mtime +30 = Adjust your days here
-print = print out list

Kailash Aghera

Leave a Reply