LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Search and Delete (https://www.linuxquestions.org/questions/linux-general-1/search-and-delete-13874/)

ryanstrayer 02-09-2002 12:32 AM

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?

neo77777 02-09-2002 09:09 AM

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.

neo77777 02-09-2002 09:12 AM

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.

Malicious 02-09-2002 09:21 AM

"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.

Malicious 02-09-2002 09:27 AM

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

neo77777 02-09-2002 09:27 AM

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.

neo77777 02-09-2002 09:34 AM

Oops, I just posted after your second post, yeah this is the way also.

ryanstrayer 02-09-2002 12:05 PM

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.

neo77777 02-09-2002 06:21 PM

try to put rm -r -f in backquotes ``, I am not sure, if it ain't work try single quotes

taz.devil 02-10-2002 12:27 PM

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.


All times are GMT -5. The time now is 10:42 PM.