0

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

1
  • Your command seems ok. I tried it on my system. I got prompted when added -i!
    – Khaled
    Jan 18, 2011 at 16:07

4 Answers 4

1

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 ().

2
  • This is a good argument for the xargs approach too. Jan 18, 2011 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, 2011 at 20:39
0

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 :).

1
  • 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?
    – robjmills
    Jan 18, 2011 at 16:06
0

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
2
  • none of the filenames have spaces so not sure what i gain from this approach?
    – robjmills
    Jan 18, 2011 at 16:13
  • in that case you don't get anything above just using xargs.
    – dotplus
    Jan 18, 2011 at 19:16
0

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.

3
  • Not sure what counts as an odd filename - all are similar to this type of format cache-1289825172-4e1d9990b3fb6da7da81175b33b6097e.javascript
    – robjmills
    Jan 18, 2011 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. Jan 18, 2011 at 16:28
  • output is: /bin/echo /home/user/tmp/cache/front/assets/cache-1294923239-5802bb1a1cefeb43a797258e1f6ceef9.css ?...
    – robjmills
    Jan 18, 2011 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .