I'm trying to write a command to help clear up old cache asset files. The files are always either .css,.javascript,.css.gzip or .javascript.gzip and i want to delete all files older than 2 days old.

I started with this command to test before passing to exec rm:

find /home/*/tmp/cache/* -mtime +2 -type f -name '*.css.gzip' -o -name '*.javascript.gzip' -o -name '*.javascript' -o -name '*.css'

This returns all the files i want deleting, so i've added rm making the command:

find /home/*/tmp/cache/* -mtime +2 -type f -name '*.css.gzip' -o -name '*.javascript.gzip' -o -name '*.javascript' -o -name '*.css' -exec rm {} \;

Nothing is actually getting deleted though, i tried making the rm command rm -i and there were no prompts, as if nothing is actually being passed to rm.

Any ideas?

FWIW this is on a CentOS 5 box

link|improve this question

Your command seems ok. I tried it on my system. I got prompted when added -i! – Khaled Jan 18 '11 at 16:07
feedback

4 Answers

up vote 1 down vote accepted

The issue is that adding -exec ... to the end of all those -o flags is tripping up precedence of AND vs OR, eg it's finding everything that is *.css.gzip OR *.javascript.gzip OR *.javascript OR (*.css AND delete it) so it only removes *.css but finds all the other files but does nothing with them. Incidentally it also means that your type and mtime flags are only applying to *.javascript.gzip.

Use

find /home/*/tmp/cache/* -mtime +2 -type f \( -name '*.css.gzip' -o -name '*.javascript.gzip' -o -name '*.javascript' -o -name '*.css' \) -exec rm {} \;

in order to force it to find all files with mtime +2 AND type f AND (all the different filename options) AND delete them.

To see this in action, replace -exec ... with -print in both your version and my version with ().

link|improve this answer
thanks a lot, makes sense now! – seengee Jan 18 '11 at 16:40
This is a good argument for the xargs approach too. – Phil Hollenback Jan 18 '11 at 16:47
@Phil: even then, without the parentheses it would have found directories named *.javascript or files less than 2 days old. – DerfK Jan 18 '11 at 20:39
feedback

Try changing rm to echo and see what it's doing. If nothing is returned, remove the echo and you'll probably remember changing the command slightly before adding the rm :).

link|improve this answer
switching the rm to echo it just displays a single filename, are you implying you've spotted something wrong though or just suggesting that as a debug? – seengee Jan 18 '11 at 16:06
feedback

Take your first command, pipe to xargs rm. I generally like to account for filenames with spaces as well so try this:

find /home/*/tmp/cache/* -mtime +2 -type f -name '*.css.gzip' -o -name '*.javascript.gzip' -o -name '*.javascript' -o -name '*.css' -print0 | xargs -0 rm
link|improve this answer
none of the filenames have spaces so not sure what i gain from this approach? – seengee Jan 18 '11 at 16:13
in that case you don't get anything above just using xargs. – dotplus Jan 18 '11 at 19:16
feedback

Try with xargs instead:

find /home/*/tmp/cache/* -mtime +2 -type f -name '*.css.gzip' -o -name '*.javascript.gzip' -o -name '*.javascript' -o -name '*.css' -print0 | xargs -0p rm

the -p argument to xargs will prompt you on each batch of files to be deleted. If that works ok, you can remove the -p to just rm the entire delete at once.

The -print0 / -0 combo uses nulls instead of spaces for delimiters to ensure that odd filenames (such as filenames with spaces) are handled correctly.

link|improve this answer
Not sure what counts as an odd filename - all are similar to this type of format cache-1289825172-4e1d9990b3fb6da7da81175b33b6097e.javascript – seengee Jan 18 '11 at 16:14
What do you see when using -p in xargs? That will tell you if your find command is passing the list of filenames you expect. – Phil Hollenback Jan 18 '11 at 16:28
output is: /bin/echo /home/user/tmp/cache/front/assets/cache-1294923239-5802bb1a1cefeb43a797258e1f6ce‌​ef9.css ?... – seengee Jan 18 '11 at 16:32
feedback

Your Answer

 
or
required, but never shown

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