Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to remove some archive files from a folder on a CentOS fileserver with a cronjob. The requirements:

  1. Remove files of the last month
  2. The .tgz-file of the first day of the month should be left intact.

I know I can remove files older then x days with:

find /path -mtime +30 -exec rm {} \;

However, that won't leave alone the archive created at the first day of the month. The internet doesn't seem to be providing an answer (yet).

share|improve this question
    
Is your files named after the date, as in e.g. log-2013-02-27.tgz and such? In that case you could run touch *-01.tgz before the find command. All files from the first of each month should be updated to the current timestamp and thus not matched. –  Daniel Andersson Mar 1 '13 at 10:40
    
Won't using +29 instead of +30 do? –  terdon Mar 1 '13 at 10:58
    
How about find /path -mtime +30 ! -name log-2013-02-01.tgz -exec rm {} \;? –  Scott Mar 1 '13 at 19:44

1 Answer 1

up vote 1 down vote accepted

First a note

On unix file systems you have no information about the creation time of a file. What is often call ctime is the status change time of a file. This may be change by file renaming or permission changes.

See this link for more informations.

So be warned that the following solution may be failing.

Solution with the help of awk

find has no operator for selecting a special date. So the trick is to create an output with the day of month and the file name and using a separator which is not part of the filenames. I assume that # is such a possible separator char.

 find . -mtime +30 -printf "%Cd#%p\n"

In a 2nd step you can filter out the unwanted files.

 find . -mtime +30 -printf '%Cd#%p\n' |awk -F'#' '$1 !~ /^01/{print $2;}'

Solution with the help of grep and cut

Doing the same trick as above.

 find . -mtime +30 -printf '%Cd#%p\n' |grep -v '^01' |cut -d '#' -f 2
share|improve this answer

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.