LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   delete files based on keywords (https://www.linuxquestions.org/questions/linux-newbie-8/delete-files-based-on-keywords-565397/)

pinoyskull 06-29-2007 11:18 AM

delete files based on keywords
 
How do I delete files based on a keyword.

Lets say in a directory I have thousands of files and I want to delete those that have "cialis" word on it, how would I do that?

Micro420 06-29-2007 11:23 AM

You could do something like find / *cialis* | xarg rm -f

I might have the expressions wrong. Someone correct it?

Or, you could just go into that directory and do ls *cialis*. If the files show up correctly, you can just do rm *cialis*

farslayer 06-29-2007 11:28 AM

Have you considered something like SpamAssassin ? since it sounds like you are trying to filter spam. from a MailDir


You may find this resource useful as well since they never spell it Cialis or Viagra without trying to disguise it.. http://wiki.castlecops.com/A_list_of_Regex_topics

pinoyskull 06-29-2007 11:36 AM

Quote:

Originally Posted by Micro420
You could do something like find / *cialis* | xarg rm -f

I might have the expressions wrong. Someone correct it?

Or, you could just go into that directory and do ls *cialis*. If the files show up correctly, you can just do rm *cialis*

the word "cialis" is inside the files


Quote:

Originally Posted by farslayer
Have you considered something like SpamAssassin ? since it sounds like you are trying to filter spam. from a MailDir

You may find this resource useful as well since they never spell it Cialis or Viagra without trying to disguise it.. http://wiki.castlecops.com/A_list_of_Regex_topics

you're right, im dealing with emails inside Maildir, We do have spamassassin but it is disabled at the moment.

pinoyskull 06-29-2007 04:54 PM

if i do this
ls | xargs grep viagra |more

it will have a result of like this
1182027917.M532957P11494V0000000000000808I0007E23D_0.server.com,S=4542:2,: CIALIS (super viagra) FOR AS LOW AS $4.38 PER DOSE
1182027917.M532957P11494V0000000000000808I0007E23D_0.server.com,S=4542:2,: <strong>CIALIS</strong> (super viagra) FOR AS LOW AS <strong>$4.38
</stron=
1182097726.M794748P29030V0000000000000808I0011988B_0.server.com,S=6068:2,:Activities ngos capacity effective, starts small. Free lesbians, xxx
air fares generic viagra adipex plavix. Pages go, section, choose national. Hurdle number lofty legal.
1182097726.M794748P29030V0000000000000808I0011988B_0.server.com,S=6068:2,:lesbians, xxx air fares generic viagra adipex plavix. Pages go, sect
ion, =


so what's the next step, I want to delete that result?

Tinkster 06-29-2007 05:21 PM

Code:

find -type f -exec egrep -li "(cialis|viagra)" {} \; | xargs -i rm "{}"
should do the trick... untested, try with echo instead of rm first ;}


Cheers,
Tink

dcpatters 06-29-2007 05:31 PM

Assuming you are in the directory you want to search:

find . -exec grep -l 'cialis' "{}" \;

You could then

find . -exec grep -l 'cialis' "{}" \;|xargs rm

Did not see post above. Practically the same

jschiwal 06-29-2007 05:38 PM

Remember that grep returns TRUE if it has found a match and false otherwise.

You could use
grep '<pattern>' "$file" && rm "$file"
inside a loop.

Since you may be looking for multiple patterns, you could use a file containing the patterns to look for:

patterns:
[vV]iagra
[cC]ialis

and then use
grep -f patterns "$file" && rm "$file"
inside a loop. Now you can simply edit the patterns file to make changes or add additional patterns.

pinoyskull 07-02-2007 05:53 AM

thanks for the replies, guys.

ill try those examples and see what happens, thanks again.

pinoyskull 07-02-2007 06:37 AM

grep -l 'cialis' will look for cialis and words with cialis like specialist, how can make it search for the exact match?

chrism01 07-02-2007 07:02 AM

add space on each side eg
grep ' cialis ' $file

pinoyskull 07-02-2007 07:12 AM

Quote:

Originally Posted by chrism01
add space on each side eg
grep ' cialis ' $file

very nice, it works great


another question

how can I see the files being deleted when executing
find . -exec grep -l ' meds ' "{}" \; | xargs rm

timmeke 07-02-2007 08:01 AM

Use "tee" to write find's output in a file. This will give you a list of files. Alternatively, you can use rm's "-v" option.

Please change "rm" with "echo" or "ls" the first time you run it, just to avoid deleting the wrong files.

Examples:
Code:

find . -exec grep -l ' meds ' "{}" \; | tee listOfDeletedFiles | xargs rm

#or, if you want to monitor the list of deleted files as it grows:
find . -exec grep -l ' meds ' "{}" \; | tee listOfDeletedFiles | xargs rm &
tail -f listOfDeletedFiles

#using rm -v:
find . -exec grep -l ' meds ' "{}" \; | xargs rm -v


pinoyskull 07-02-2007 08:17 AM

timmeke
i just tried rm -v before you posted your answer :D, but thanks anyway those other examples helps :)

pinoyskull 07-02-2007 09:15 AM

a follow-up question...

using grep -f <file> will process all patterns in that file, will it search for the exact pattern or not?


All times are GMT -5. The time now is 05:12 PM.