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).
-
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).
From Xerxes -
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 deletedDennis Williamson : attachment_fu is a directory, so you'll need another asterisk at the end.: As Dennis mentioned, a slightly modified version of this worked: `rm /var/www/somesite/releases/*/tmp/attachment_fu/*`Lee B : 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.radius : Thanks Dennis, I corrected the missing *From radius -
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
;)
radius : He's talking about removing file, not directory, so -rf il probably not what he wants ! -
rm -rf said path it will remove all dir and files
reinierpost : The question is how to only remove files matchign a certain pattern.From Rajat -
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
From AlberT
0 comments:
Post a Comment