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

user18816
  • 165
  • 4

5 Answers5

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

radius
  • 9,633
  • 25
  • 45
  • 1
    attachment_fu is a directory, so you'll need another asterisk at the end. – Dennis Williamson Sep 18 '09 at 06:47
  • As Dennis mentioned, a slightly modified version of this worked: `rm /var/www/somesite/releases/*/tmp/attachment_fu/*` – user18816 Sep 18 '09 at 06: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 '09 at 10:08
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).

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
khosrow
  • 4,163
  • 3
  • 27
  • 33
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

;)

user20575
  • 21
  • 1
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

drAlberT
  • 10,949
  • 7
  • 39
  • 52
-2

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

Rajat
  • 3,349
  • 22
  • 29