LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Remove files that contain a specific string (https://www.linuxquestions.org/questions/linux-general-1/remove-files-that-contain-a-specific-string-789635/)

poymode 02-17-2010 01:45 AM

Remove files that contain a specific string
 
Hi.

How can you remove files containing a specific string?

I have...

Code:

find |grep 'string'
This may return several results and I wanted to rm the results.

I also have...

Code:

ls -l |grep 'string'|awk '{print $9}'
which also may return results.

But point is, I can't supply the results as a parameter to rm

I was thinking of looping but I don't know how to access the results as if they were an array or something.

neonsignal 02-17-2010 02:13 AM

Code:

find -type f -exec grep -q 'string' '{}' \; -exec rm '{}' \;
The -exec executes a command, the -q flag makes the grep run quietly (since we are only interested in the return status of match/nomatch, and the {} is the file currently being examined. If the first exec fails, it does not continue to the second exec (since it doesn't need to), so the effect is to only run the second command when the grep matches.

I'd suggest testing this with an echo in place of the rm first (because if you get the grep wrong, it could delete every file in the tree).

poymode 02-17-2010 02:49 AM

Quote:

Originally Posted by neonsignal (Post 3866524)
Code:

find -type f -exec grep -q 'string' '{}' \; -exec rm '{}' \;
The -exec executes a command, the -q flag makes the grep run quietly (since we are only interested in the return status of match/nomatch, and the {} is the file currently being examined. If the first exec fails, it does not continue to the second exec (since it doesn't need to), so the effect is to only run the second command when the grep matches.

I'd suggest testing this with an echo in place of the rm first (because if you get the grep wrong, it could delete every file in the tree).

Hmm..unfortunately it doesn't to anything.

I have tried something

Code:

ls -l |grep 'omg'|awk '{print $9}'|xargs echo
and it does echo the results but replacing echo with rm yields an error.

Code:

rm: cannot remove `\033[00momg1\033[00m': No such file or directory
rm: cannot remove `\033[00momg2\033[00m': No such file or directory
rm: cannot remove `\033[00momg3\033[00m': No such file or directory


poymode 02-17-2010 02:53 AM

Hmm..Fortunately solved my problem.

Maybe my solution is specific to my problem here but here it is:

Code:

find |grep 'string'|xargs rm -f
Thanks buddy for replying.

neonsignal 02-17-2010 02:57 AM

If you just want to find the string in the filename, then that is exactly what find is great at doing:
Code:

find -type f -name '*string*' -exec rm '{}' \;
Your xargs solution is reasonable, though it will have problems if there is a space in the filename.

My original answer assumed that you were looking for the string inside the file...

colucix 02-17-2010 03:01 AM

If I understand well you want to remove files that contain "string" in their name, don't you?! If this is the case, just do a search by name and remove them (a way similar to that one suggested by neonsignal):
Code:

find . -type f -name '*string*' -ok rm {} \;
I deliberately put -ok in place of -exec in order to force find to ask you confirmation before executing the rm commands. You can also previously use find without -exec or -ok just to check the result of the search, then run it again adding -exec.

Regarding the error you got using the ls and xarg solution, most likely it is due to the --color option applied to the aliased ls. If you want to try the "not-aliased" ls in order to skip the problem, just put a leading backslash in front of it (or just use the full path /bin/ls):
Code:

\ls *omg* | xargs echo
Edit: A bit too late... ;)


All times are GMT -5. The time now is 04:08 PM.