• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2898
  • Last Modified:

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" *
0
dennisdominic
Asked:
dennisdominic
  • 5
  • 5
  • 2
2 Solutions
 
TintinCommented:
Use single quotes if you want to match any wildcards, eg:

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

grep -lir '***SPAM***' *
0
 
ozoCommented:
fgrep -lir '***SPAM***' *
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dennisdominicAuthor Commented:
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?


0
 
ozoCommented:
fgrep -lir '***SPAM***' * | xargs rm
or
rm `fgrep -lir '***SPAM***' *`
0
 
dennisdominicAuthor Commented:
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***' *`




0
 
TintinCommented:
I'm not surprised you get a pause when trying to run it over >200000 files.
0
 
dennisdominicAuthor Commented:
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?
0
 
TintinCommented:

#!/bin/bash
for dir in /path/to/dir/*2
do
  rm -vf `fgrep -lir '***SPAM*** $dir`
done
0
 
dennisdominicAuthor Commented:
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***' *`
0
 
TintinCommented:
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.
0
 
dennisdominicAuthor Commented:
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

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now