LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2007, 11:18 AM   #1
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Rep: Reputation: 15
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?
 
Old 06-29-2007, 11:23 AM   #2
Micro420
Senior Member
 
Registered: Aug 2003
Location: Berkeley, CA
Distribution: Mac OS X Leopard 10.6.2, Windows 2003 Server/Vista/7/XP/2000/NT/98, Ubuntux64, CentOS4.8/5.4
Posts: 2,986

Rep: Reputation: 45
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*

Last edited by Micro420; 06-29-2007 at 11:25 AM.
 
Old 06-29-2007, 11:28 AM   #3
farslayer
LQ Guru
 
Registered: Oct 2005
Location: Willoughby, Ohio
Distribution: linuxdebian
Posts: 7,232
Blog Entries: 5

Rep: Reputation: 190Reputation: 190
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

Last edited by farslayer; 06-29-2007 at 11:31 AM.
 
Old 06-29-2007, 11:36 AM   #4
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Original Poster
Rep: Reputation: 15
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.
 
Old 06-29-2007, 04:54 PM   #5
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Original Poster
Rep: Reputation: 15
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?
 
Old 06-29-2007, 05:21 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 06-29-2007, 05:31 PM   #7
dcpatters
LQ Newbie
 
Registered: Mar 2007
Posts: 2

Rep: Reputation: 0
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

Last edited by dcpatters; 06-29-2007 at 05:40 PM.
 
Old 06-29-2007, 05:38 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
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.
 
Old 07-02-2007, 05:53 AM   #9
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Original Poster
Rep: Reputation: 15
thanks for the replies, guys.

ill try those examples and see what happens, thanks again.
 
Old 07-02-2007, 06:37 AM   #10
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Original Poster
Rep: Reputation: 15
grep -l 'cialis' will look for cialis and words with cialis like specialist, how can make it search for the exact match?
 
Old 07-02-2007, 07:02 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
add space on each side eg
grep ' cialis ' $file
 
Old 07-02-2007, 07:12 AM   #12
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Original Poster
Rep: Reputation: 15
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
 
Old 07-02-2007, 08:01 AM   #13
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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
 
Old 07-02-2007, 08:17 AM   #14
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Original Poster
Rep: Reputation: 15
timmeke
i just tried rm -v before you posted your answer , but thanks anyway those other examples helps
 
Old 07-02-2007, 09:15 AM   #15
pinoyskull
Member
 
Registered: Jan 2005
Location: server farm
Distribution: CentOS 5.7
Posts: 59

Original Poster
Rep: Reputation: 15
a follow-up question...

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


Reply

Tags
delete, files


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete mail based on date gquiring Linux - General 8 05-08-2013 09:24 AM
Delete files based on date stefaandk Linux - General 3 06-17-2005 02:20 AM
Sarch files for multiple keywords with AND titopoquito Linux - Software 4 03-06-2005 07:51 AM
cron job to delete files based on attributes alpha21 Linux - General 3 11-09-2004 12:06 PM
How to delete the destination files while the source files deleted in cp -u ? myunicom Linux - General 4 09-26-2003 01:13 PM


All times are GMT -5. The time now is 04:15 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration