Is there a way to do a ' ls -R | grep "aname"* | rm; to remove multiple files in multiple directory
Solution 1
If 'aname' shall be the starting part of the filename, it would be, from the current directory:
find -name "aname" -delete
Btw.:
grep "bla"* somewhere
is almost always false, since grep already makes partial matches, which means, it finds bla, blafasel and xybla with simply
grep bla somewhere
Deleted older part of answer, because of misunderstanding:
From the current directory, if you look for the word 'aname' in all files, you can just use:
find -exec grep "aname" {} ";" -delete
Solution 2
What you are looking for is a combination of xargs
, find
, and rm
.
find
will make a list of all the files matching your conditions, and then write them (null terminated) to stdout
, which will be piped to xargs
. xargs
will take the null terminated strings and use them as arguments to rm
.
find -L /path/to/dir -name "*name*" -print0 |xargs -0 -r rm
source: http://linuxcommando.blogspot.com/2007/10/find-xargs-pipe.html
Cyclic
Updated on September 18, 2022Comments
-
Cyclic over 1 year
I have installed (compiled) emerald. The command
make uninstall
didn't work (there is no makefile as it is just a script). So I decided to remove the file manually. But there were a lot of files.I've tried some things but it didn't work. So I came here to ask for some advice.
- Is there a command with
ls -R | grep blabla*
that I can add to display file directory (it only displays filename) - Is there a true
ls -R | grep bla* | rm
?
- Is there a command with
-
enzotib over 12 yearsThe
-delete
find action could be of help. -
Cyclic over 12 yearsThanks for your help, but i didn't help me. I'm looking for a command that recursively find "myfilename*" into child folders. Just like 'ls -R | grep name*'... and delete them.
-
Cyclic over 12 yearsOk it's good, i didn't notice that find recursively look into directory... Case resolved ;) thanks. I was just looking for : sudo find -name bla* -delete; :D so simple
-
datacarl over 12 years@Cyclic: Don't forget to mask
"bla*"
with quotes; else it won't work. May I ask you to upvote helpful posts and to accept one of the helpful answers? (clicking the ^ signs before the answers and the symbol, similar to √ ) -
Huckle over 12 yearsI can, but if the OP is looking for a general solution that can be used with more than just rm then he should look into this form.