• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Linux Command to remove files with specific text?

AllLiving

Gawd
Joined
Sep 26, 2002
Messages
696
Does anyone know any commands? I'm trying to remove thousands of emails containing a certain text, or address, but I can't do it via command line.

Have tried...

for i in `ls -1 | head -1000`; do grep -H "Mail delivery failed: returning message to sender" * | awk -F: '{print $1}'`; do rm -f $i; done
 
NEVER!!! parse the output of ls

Code:
for f in *; do [[ grep -H "Mail delivery failed: returning message to sender" "$f" ]] && rm "$f"; done
 
Let me guess - it starts a thermonuclear war!?

Please NEVER parse, pipe, grep, capture, read, or loop over the output of 'ls'. Unlike popular belief, 'ls' is NOT designed to enumerate files or parse their statistics. Using 'ls' for this is dangerous (word splitting) and there's always a better way; eg. globs: files=(*).

$ ls
The best song in the world.mp3
$ for file in $(ls *.mp3)
> do rm "$file"
> done
rm: cannot remove `The': No such file or directory
rm: cannot remove `best': No such file or directory
rm: cannot remove `song': No such file or directory
rm: cannot remove `in': No such file or directory
rm: cannot remove `the': No such file or directory
rm: cannot remove `world.mp3': No such file or directory
You should already know to quote the $file in the rm statement; but what's going wrong
here? BASH expands the command substitution ($(ls *.mp3)), replaces it by its
output, and as a result executes
for file in The best song in the world.mp3. BASH splits that up into
words by using spaces and tries to rm each word. Boom, you are dead.
You want to quote it, you say? Let's add another song:
$ ls
The best song in the world.mp3 The worst song in the world.mp3
$ for file in "$(ls *.mp3)"
26 of 50 29/01/2008 09:17
BashGuide - Greg's Wiki http://wooledge.org:8000/BashGuide
> do rm "$file"
> done
rm: cannot remove `The best song in the world.mp3 The worst song in the world.mp3': No
such file or directory
Quotes will indeed protect the whitespace in your filenames; but they will do more than
that. The quotes will protect all the whitespace from the output of ls. There is no way
BASH can know which parts of the output of ls represent filenames; it's not psychic. The
output of ls is a simple string, and BASH treats it as such. The for puts the whole
quoted output in i and runs the rm command with it. Damn, dead again.

Taken from irc.freenode.net#bash greybot
 
I suppose those emails are seperate files?
#find . -type f -name "*.msg" -exec grep failed -print0 | xargs rm
(incomplete because the syntax for xargs is needed, and proper syntax for before-pipe still must
be tested).. but you can probably expand on it.
 
It's easier than you're all making it
cd to the directory, and run the following:
grep -l "Mail delivery failed: returning message to sender" * | xargs rm
 
Back
Top