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? |
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* |
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 |
Quote:
Quote:
|
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? |
Code:
find -type f -exec egrep -li "(cialis|viagra)" {} \; | xargs -i rm "{}" Cheers, Tink |
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 |
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. |
thanks for the replies, guys.
ill try those examples and see what happens, thanks again. |
grep -l 'cialis' will look for cialis and words with cialis like specialist, how can make it search for the exact match?
|
add space on each side eg
grep ' cialis ' $file |
Quote:
another question how can I see the files being deleted when executing find . -exec grep -l ' meds ' "{}" \; | xargs rm |
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 |
timmeke
i just tried rm -v before you posted your answer :D, but thanks anyway those other examples helps :) |
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. |