Remove files that contain a specific string
Hi.
How can you remove files containing a specific string? I have... Code:
find |grep 'string' I also have... Code:
ls -l |grep 'string'|awk '{print $9}' 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. |
Code:
find -type f -exec grep -q 'string' '{}' \; -exec rm '{}' \; 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). |
Quote:
I have tried something Code:
ls -l |grep 'omg'|awk '{print $9}'|xargs echo Code:
rm: cannot remove `\033[00momg1\033[00m': No such file or directory |
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 |
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 '{}' \; My original answer assumed that you were looking for the string inside the file... |
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 {} \; 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 |
All times are GMT -5. The time now is 04:08 PM. |