Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

First, I wasn't sure if belonged to StackOverflow or here, but figure this would be more applicable to server administration.

We have a .tar backup file going to an offsite backup server each day as apart of our daily processing. These are relatively large files averaging between 25-30 Gigs. As result, we are only able to keep about 2 weeks of backups. Every Friday (yes, this is Friday), one will ssh to the remote the server, OpenSuse 12.1, and manually delete the files the week prior and keep the current week's data. There is a consistent naming scheme within the file with something like BACKUP<YYYYMMDD><Time stamp>, for example.


and so on.

Most of the time I would use the command rm *20121022* in this example. Sometimes, like in this example, im even able to get away with using rm *201210[22-26]*. Doing a quick google search on muddied the water for me. I'm basically trying set this up so that an intern can log in, and run script from the user directoy.

Is there an easy way to do this? Where do I start here? I'm familiar with using date functions in other languages, but I seem be at an impass as where to start here. I don't do alot of Bash scripting. Thanks.

share|improve this question
Any reason you are not compressing the tar files using gzip? – kernelpanic Nov 2 '12 at 20:29
@kernelpanic Technicly, it's a vendor's machine that's being backed up. They keep the hood tightly shut with little control over the process. It's application built on top of AIX. If you only knew the world I live in :( – hydroparadise Nov 2 '12 at 20:37
@hydroparadise: You said the 'A' word. I think that says it all.... – Scott Pack Nov 2 '12 at 20:38
Any reason this is not in a cron job ? – adaptr Nov 5 '12 at 14:40
up vote 1 down vote accepted

Assuming the timestamps of the files stay consistent you can do this with find.

find /path/to/backups -type f -maxdepth 1 -mtime +7 -delete

This will list all of the files within the directory, without recursing, /path/to/backups that were last modified more than 7 24 hour periods ago and delete them. I would recommend running without the -delete first until you know it'll work for you.

If you instead want to operate on calendar days use the -daystart option. I find this most useful when I have something that rotates files hourly, but I want to base my retention schedule on a calendar. Sometimes the distinction between "day" and "24-hour period" actually matters and using -daystart certainly makes the retention schedule easier to explain.

share|improve this answer

You can delete files older than 1 week using the following command:

find /path -type f -name "BACKUP*" -mtime +7 -exec rm {} \;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.