LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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

Reply
 
Search this Thread
Old 02-17-2010, 02:45 AM   #1
poymode
LQ Newbie
 
Registered: Dec 2007
Distribution: Slackware 13
Posts: 9

Rep: Reputation: 0
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.
 
Old 02-17-2010, 03:13 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,364
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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).

Last edited by neonsignal; 02-17-2010 at 03:27 AM.
 
1 members found this post helpful.
Old 02-17-2010, 03:49 AM   #3
poymode
LQ Newbie
 
Registered: Dec 2007
Distribution: Slackware 13
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by neonsignal View Post
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
 
Old 02-17-2010, 03:53 AM   #4
poymode
LQ Newbie
 
Registered: Dec 2007
Distribution: Slackware 13
Posts: 9

Original Poster
Rep: Reputation: 0
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.
 
Old 02-17-2010, 03:57 AM   #5
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,364
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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...

Last edited by neonsignal; 02-17-2010 at 04:00 AM.
 
Old 02-17-2010, 04:01 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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...

Last edited by colucix; 02-17-2010 at 04:02 AM.
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 06:41 AM
Setting time with specific format string MS3FGX Linux - Software 5 09-21-2009 05:50 PM
Selectively moving files from a tree with a specific string anywhere in the path waterdragon900 Linux - Newbie 8 08-18-2008 10:57 AM
How to find those files having specific string??? saeed Red Hat 4 07-19-2006 12:50 PM
Remove ALL files from specific Directory bianchi Programming 9 11-24-2005 12:27 AM


All times are GMT -5. The time now is 01:02 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration