Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

trying to create a scripts that deletes files older than X days. It seemed straight forward enough but all files seem to be getting removed?

find /Volumes/Groups/Projects/530_BFAMI/test/ -ctime +30 -print -exec rm {} \;

any ideas welcome!?

Also would be good to print file information for matches as maybe my dates are misleading in finder?

share|improve this question
    
agh apologies. Is working. Can i have multiple parameters? Ie include a size? -ctime +30 AND -size +10000000c –  daniel Crabbe Jun 13 '11 at 14:16
2  
There's a significant difference between ctime and mtime and in most cases people mean to use mtime (the time the contents of the file last changed) and not ctime (the time the inode-related information for the file changed). Very rare to see ctime used this way, so I mention this as a caution to you OP. –  Ian C. Jun 13 '11 at 14:45
add comment

4 Answers

Never suggesting -exec rm ....

rm is simply too dangerous command. Always dry-run first. IMHO it is better using this:

find /Volumes/Groups/Projects/530_BFAMI/test/ -ctime +30 -print0 | xargs -0 -n1

this will show the dry-run and only when you satisfied you can add the rm to the end

find /Volumes/Groups/Projects/530_BFAMI/test/ -ctime +30 -print0 | xargs -0 rm

If you start using this piped to xargs version, you can always add more filters, like:

find /some/where -mtime +3d -print0 | grep -ziE '\.(jpg|png|gif)$' | xargs -0 rm

and similar (more) powerful combinations.

share|improve this answer
add comment

To answer the question asked in your comment: You can join separate criteria just by listing them, one after the other. If you prefer, you can use -a or -and in between them, but these are redundant. You can also use -o or -or for the OR operator, and you can use parentheses – which must be quoted to protect them from the shell, as in \( and \) for grouping.

For the full details, run: man find

share|improve this answer
add comment

It is considered dangerous to use -exec rm without first ls. You should probably check that first. Reverse check is also a good hint to tell you if your timing is correct.

find . -maxdepth 1 -type f -mtime -30d -exec ls -latr {} \;

OR

find . -maxdepth 1 -type f -atime -30d -exec ls -latr {} \;
  1. Constrain your find result. Make sure what you explicitly ask for.
  2. Compare to see if the opposite result is what actually has been excluded from your original command.
  3. Probably -mtime or -atime are more appropraite but again compare the result and see if they are actually what you need.
share|improve this answer
add comment

Try this: find . -ctime +30 | xargs rm -r 2> /dev/null

For some reason, this runs rm twice. So I just redirect the errors to the black hole /dev/null

Enjoy.

share|improve this answer
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.