3

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

1
  • Don't use locate, it's not intended for what you're trying to do. Sep 18, 2009 at 6:47

5 Answers 5

5

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

3
  • 1
    attachment_fu is a directory, so you'll need another asterisk at the end. Sep 18, 2009 at 6:47
  • As Dennis mentioned, a slightly modified version of this worked: rm /var/www/somesite/releases/*/tmp/attachment_fu/*
    – user18816
    Sep 18, 2009 at 6:50
  • 1
    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, 2009 at 10:08
2

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

;)

1
  • He's talking about removing file, not directory, so -rf il probably not what he wants !
    – radius
    Sep 18, 2009 at 6:47
2

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

0

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)
do
  find $d -type f -exec rm {} \;
done

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

-2

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

1
  • The question is how to only remove files matchign a certain pattern. Sep 18, 2009 at 11:37

You must log in to answer this question.

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