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

I have a backup script that creates files named for the day of creation in the format backups_YYYYMMDD.tar.gz

I currently purge everything over 30 days old with a command like this:

find /backups -mtime +30 -name "backups_????????.tar.gz" -delete

How can I alter this to purge only files that were NOT made on the first of the month? i.e. anything matching "backups_??????01.tar.gz" remains.

share|improve this question
up vote 2 down vote accepted

You can do it with simple globbing in the same way you're using it now:

find /backups -mtime +30 -name "backups_????????.tar.gz" ! -name "backups_??????01.tar.gz" -delete

"Find the files that are over 30 days old and are named FOO and not named BAR and delete them."

share|improve this answer

Assuming you have noting but backup files in all the directories below you could do something like:

find . -type f -regextype posix-egrep ! -regex ".*backups_[0-9]{6}01.tar.gz" -name '*.tar.gz' -print0 | xargs -0 rm 

Hard to say exactly without knowing all the files in these directories so run it without rm first! You might want to modify the last *.tar.gz glob to something closer to your original glob.

share|improve this answer
Oh I don't think I read it quite right. But basically "!" before the rule negates it, and then everything is and'd together. I made this a little more complicated than need be. – Kyle Brandt Aug 20 '10 at 1:02
Not or'd... and'ed :P You have to use the -or operator for them to be or'd (as you say) – Khai Aug 20 '10 at 1:27

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.