LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2006, 12:33 PM   #1
Sleb
LQ Newbie
 
Registered: May 2006
Posts: 2

Rep: Reputation: 0
deleting files containing a specific word


Hi all,

Here is what I want to do ;

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
 
Old 05-25-2006, 12:48 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
find . |xargs grep myword |awk -F: '{print $1}' |sort -u |xargs rm

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:

xargs rm -i
 
Old 05-25-2006, 12:49 PM   #3
RAdams
Member
 
Registered: May 2006
Location: Cincinnati, Ohio
Distribution: Ubuntu
Posts: 256

Rep: Reputation: 30
Quote:
Originally Posted by Sleb
Hi all,

Here is what I want to do ;

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. :]

Last edited by RAdams; 05-25-2006 at 12:52 PM.
 
Old 05-25-2006, 12:49 PM   #4
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
hi there,
Code:
grep -l your_word * | xargs rm -rf
if you need it recursive, you can add -r to the grep call.
regards,
slackie1000
 
Old 05-25-2006, 01:01 PM   #5
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Code:
rm -f $(grep -lr myword *)
 
Old 05-25-2006, 01:19 PM   #6
Sleb
LQ Newbie
 
Registered: May 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Wow. Thanks a lot you guys for all these different ways of doing it.

Regards
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting files! a_priebe47 Slackware 6 10-17-2004 09:58 PM
find a word within any files Linh Linux - General 6 06-06-2004 11:15 AM
Domain and specific 'word' blocking in firewall2 hiplainsdrifter Linux - Newbie 5 04-07-2004 03:54 PM
ability to handle MS Word files Maidros Debian 3 03-17-2004 01:58 PM
Viewing word files in linux? scottie Linux - Software 1 11-05-2003 10:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration