Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to find all files matching a certain criteria and delete them - here's a snippet:

/var/www/somesite/releases/{many directories}/tmp/attachment_fu

I'd like to find all files in any tmp/attachment_fu directory and delete them - the problem is that the {many directories} is throwing off my find skills (or maybe find is the wrong command - I also tried locate to no avail).

share|improve this question
Don't use locate, it's not intended for what you're trying to do. – Dennis Williamson Sep 18 '09 at 6:47
up vote 5 down vote accepted

May be I'm missing something but rm /var/www/somesite/releases/*/tmp/attachment_fu/* seems to be what you want. ls /var/www/somesite/releases/*/tmp/attachment_fu can be used to see what will be deleted

share|improve this answer
attachment_fu is a directory, so you'll need another asterisk at the end. – Dennis Williamson Sep 18 '09 at 6:47
As Dennis mentioned, a slightly modified version of this worked: rm /var/www/somesite/releases/*/tmp/attachment_fu/* – user18816 Sep 18 '09 at 6:50
Any and all files also means hidden files. Something like rm -rf /var/www/somesite/releases/*/tmp/attachment_fu/{,.}* is probably what you want. Be careful, as -rf makes this a pretty dangerous command. Put ls in front and |less on the end if you want to test it. p.s.: if you were doing it with find, you'd probably want to limit the depth with min-depth and max-depth options. – Lee B Sep 18 '09 at 10:08
Thanks Dennis, I corrected the missing * – radius Sep 18 '09 at 14:47

ls /var/www/somesite/releases/*/tmp/attachment_fu | xargs rm -rf

something like that?

yeah, or rm -rf /var/www/somesite/releases/*/tmp/attachment_fu


share|improve this answer
He's talking about removing file, not directory, so -rf il probably not what he wants ! – radius Sep 18 '09 at 6:47

I'm not completely clear on what you're trying to do - delete files matching the name attachment_fu recursively, but leaving directories alone?

If so, try this (I've added an echo so you can test-drive it first)...

find /var/www/somesite/releases -type f -name attachment_fu -exec echo rm -f {} \;

If not, please explain further :)

Alternatively, you can use the -regex flag to find if the '/tmp/' is important; something like...

find /var/www/somesite/releases -type f -regex '.*/tmp/attachment_fu$' -exec echo rm -f {} \;

All this is assuming that the files you're after are at various depths in the filesystem tree, otherwise you can just use rm (post by radius).

share|improve this answer

In addition to other answers, you can incur in the "command line too long" error if using * when the number of files you have to delete is huge.

The safest way could be to use the shell expansion to retrieve the list of dirs, loop them and use find to remove the files:

for d in $(ls /var/www/somesite/releases/*/tmp/attachment_fu)
  find $d -type f -exec rm {} \;

This should work quite well, of course it would fail if you have an huge number of dirs, but then you have other kind of problems :P

share|improve this answer

rm -rf said path it will remove all dir and files

share|improve this answer
The question is how to only remove files matchign a certain pattern. – reinierpost Sep 18 '09 at 11:37

Your Answer


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.