Link to home
Start Free TrialLog in
Avatar of dennisdominic
dennisdominicFlag for Hong Kong

asked on

Delete Lists of Files after grep in Linux

Linux Environment.

I have a command that lists all files containing the text '***SPAM***', take the list, and delete All of them.

this command lists all files containing text 'some text'.. but I'm not sure if it applies for '***SPAM***'
grep -lir "some text" *
Avatar of Tintin
Tintin

Use single quotes if you want to match any wildcards, eg:

grep -lir '***SPAM***'
Whoops, forgot the * at the end of the grep

grep -lir '***SPAM***' *
fgrep -lir '***SPAM***' *
Avatar of dennisdominic

ASKER

Sorry, I mean
take the result list, and delete All of them.

your command gives the list.. so how do I get the system delete all those listed?


ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It goes into a very long pause when I run it on the main directory containing 100 Subdirectories of email accounts with over 2000 emails each. But it works very fast when I run it  in each of the the email account subdirectories.

I was trying to delete backup emails that are listed as SPAM.

rm -vf `fgrep -lir '***SPAM***' *`




I'm not surprised you get a pause when trying to run it over >200000 files.
I'm trying to make it work by not timing itself out, by going into the Sub Directory first before running the command... so I came up with this..

ls -1 *2 | while read DIR
do
cd "$DIR"
rm -vf `fgrep -lir '***SPAM***' *`
cd ..
done

result:
bash: cd: a2:: No such file or directory

what did I do wrong again?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It still goes into a very long pause..

for dir in /path/to/dir/*2
do
  rm -vf `fgrep -lir '***SPAM***' *`
done

But I can always go back to the original statement and run it manually on a few bigger subdirectories.

rm -vf `fgrep -lir '***SPAM***' *`
I notice you didn't copy/paste my script correctly.

You had

  rm -vf `fgrep -lir '***SPAM***' *`

instead of

  rm -vf `fgrep -lir '***SPAM***' $dir`

the first version would be additionally slow as it would grep through all the files for as many directories existed.
Thanks!!
I just realised I have to change /path/to/dir into the actual directory.. now it works perfectly!!

for dir in /xvmailhome/domains/MyDomains/*2
do
rm -vf `fgrep -lir '***SPAM***' $dir`
done