Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.