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.

link|improve this question

feedback

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."

link|improve this answer
feedback

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.

link|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
feedback

Your Answer

 
or
required, but never shown

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