LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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-09-2002, 12:32 AM   #1
ryanstrayer
Member
 
Registered: Jan 2002
Location: Kansas City
Distribution: RedHat 7.2 Kernel 2.4.17
Posts: 250

Rep: Reputation: 30
Exclamation Search and Delete


Okay, I know there is a way to do this - how do I search for a specific string/file and then remove that file which contains that instance...?

Say starting from / I want to delete anything in the entire tree starting with say netscape* ... or whatever?
 
Old 02-09-2002, 09:09 AM   #2
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
I know of find but it's only valid for filenames, you won't be able to search the context of a file with it, grep will do, but grep will not go into directories, so I don't have a valid answer, sorry.
 
Old 02-09-2002, 09:12 AM   #3
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
Actually I do have an answer, look at CPAN for a perl script (or write your own) which going into every subdirectory in a current directory and searches files for a particular string, you can modify the script so it will delete particular files.
 
Old 02-09-2002, 09:21 AM   #4
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
"find" is the magic for this trick. See the man page for some more tricks; but given your example of wanting to remove all files that begin with "netscape" do this command:

find / -name "netscape*" -exec rm {} \;

find is the program name
"/" is the location to start "finding"
-name is the path name or reg exp to "find"
-exec defines a command to run on a match
"rm" is the command
"{}" represents the matched path name
"\;" terminates the exec'ed command

I would suggest that you run the command using the -print option instead of -exec to see what files you actually "find".

Note that this command will also "find" directories, but the "rm" won't work on them so they won't be removed and you will get an error.
 
Old 02-09-2002, 09:27 AM   #5
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
Oops... I guess I misread your question. You want to find files that contain a certain string. "find" is still the magic. Try this:

find / -type f -exec grep -l "netscape" \; -exec rm {} \;

This time:

-type f says only find files
-exec grep -l "netscape" {} \;
grep the found file for netscape only show name
-exec rm {} \;
remove the files that grep lists
 
Old 02-09-2002, 09:27 AM   #6
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
To
Malicious
this command will find every file whose filename you specified after the -name tag
so it will not go into the files to search for a particular string, here I found on CPAN
http://www.cpan.org/scripts/index.html
look for keywordsearch-1.00 script
it will do the job of finding the files with a specified keyword, modify it so it will delete them.
 
Old 02-09-2002, 09:34 AM   #7
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
Oops, I just posted after your second post, yeah this is the way also.
 
Old 02-09-2002, 12:05 PM   #8
ryanstrayer
Member
 
Registered: Jan 2002
Location: Kansas City
Distribution: RedHat 7.2 Kernel 2.4.17
Posts: 250

Original Poster
Rep: Reputation: 30
Thanks guys - it worked. Anyway to get that to remove the directories as well? I added on -r -f to the rm command, but didn't seem to help.. like so:

find / -name "searchstring" -exec rm -r -f {} \;

and kept telling me "blah blah is a directory" .. but I can run ' rm -r -f directory' and can remove it.

Last edited by ryanstrayer; 02-09-2002 at 12:17 PM.
 
Old 02-09-2002, 06:21 PM   #9
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 56
try to put rm -r -f in backquotes ``, I am not sure, if it ain't work try single quotes
 
Old 02-10-2002, 12:27 PM   #10
taz.devil
Senior Member
 
Registered: Nov 2001
Location: Wa. State
Distribution: Slackware
Posts: 1,261

Rep: Reputation: 45
Instead of rm -r -f use rm -rf
You don't need to specify them seperately in that way for the remove command. Likewise you can look into the -R command also. To decide, because they are both recursive commands, look at the man page to get an idea of what you want rm to do.
man rm
Also look into shred and see if it looks like something you'd like to try.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to search for and delete empty directories? BrianK Programming 4 06-03-2005 07:24 AM
search default: search post title only slackie1000 LQ Suggestions & Feedback 4 03-10-2005 07:50 AM
how to search and delete Xiangbuilder Linux - Newbie 2 11-30-2004 04:56 AM
Quick VIM question (unhighlighting search terms after search) lrt2003 Linux - Newbie 5 05-08-2004 05:21 PM
How to search and delete files in Unix/Linux puzz_1 Linux - General 5 04-09-2004 10:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:02 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