Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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).
Last edited by neonsignal; 02-17-2010 at 02:27 AM.
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
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):
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.