ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Every file that matches will give you filename:line that contains myword
The awk -F: says to break the output of the previous xargs command into fields delimited by the colon. The '{print $1}' prints only the first field which will be the filename.
The sort -u says to to only list each filename once (otherwise you could see the same file more than once if it had myword on more than one line).
The xargs rm says to remove the filenames that were listed via the preceding pipes.
Having said all that I'd suggest the above is fairly dangerous unless you're absolutely sure myword appears ONLY in files you want to remove. You can make rm prompt you with the filename and ask if you want to delete it by changing the last part to:
In a directory where I have multiple files, I want to be able to delete only files containing a certain word.
For now, I'm able to pickup those files doing this ;
find . |xargs grep myword
I would like to add rm -f. Tried piping at the end and beginning but gives me errors.
Any suggestions? I'm sure it's pretty easy.
Thanks
I'm not at home, so I can't test it out right now, but I would think you might be able to do what you're trying using wildcards (*). This wouldn't work for hidden files, though (I could be wrong about that, too). I could be wrong about everything, come to think of it. :]
EDIT: The post above me answered your question much better than I, whilst I was typing away this halfway idea. :]
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.