60

I am trying to delete erroneous emails based on finding the email address in the file via Linux CLI.

I can get the files with

find . | xargs grep -l email@example.com

But I cannot figure out how to delete them from there as the following code doesn't work.

rm -f | xargs find . | xargs grep -l email@example.com

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Spechal
  • 2,634
  • 6
  • 32
  • 48

8 Answers8

85

Solution for your command:

grep -l email@example.com * | xargs rm

Or

for file in $(grep -l email@example.com *); do
    rm -i $file;
    #  ^ prompt for delete
done
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
ajreal
  • 46,720
  • 11
  • 89
  • 119
  • 1
    For several files, you can prevent the wildcard adding too many arguments by using `grep -l -R --include="*" email@domain.com ./` instead – cvsguimaraes May 12 '16 at 13:45
  • `sudo grep -lr '/directory/youd/like/to/delete/from/' -e 'text you would like to search' | xargs rm` This is what I used. I believe 2grit referenced the '-r' for recursive, which was helpful in my case. – JustinP Jul 29 '20 at 19:00
80

For safety I normally pipe the output from find to something like awk and create a batch file with each line being "rm filename"

That way you can check it before actually running it and manually fix any odd edge cases that are difficult to do with a regex

find . | xargs grep -l email@example.com | awk '{print "rm "$1}' > doit.sh
vi doit.sh // check for murphy and his law
source doit.sh
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
19

You can use find's -exec and -delete, it will only delete the file if the grep command succeeds. Using grep -q so it wouldn't print anything, you can replace the -q with -l to see which files had the string in them.

find . -exec grep -q 'email@example.com' '{}' \; -delete
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
  • does `find . -exec grep -q 't-bone@spechal.com' '{}' \; -print` show anything? – OneOfOne Jul 08 '14 at 23:36
  • yeah, but nothing as expected. `find . |grep 't-bone@spechal.com'` at other hand works just fine. I'm on a mac, btw. my answer there solved it for me anyway. ;) – cregox Jul 08 '14 at 23:43
3

Despite Martin's safe answer, if you've got certainty of what you want to delete, such as in writing a script, I've used this with greater success than any other one-liner suggested before around here:

$ find . | grep -l email@example.com | xargs -I {} rm -rf {}

But I rather find by name:

$ find . -iname *something* | xargs -I {} echo {}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
cregox
  • 17,674
  • 15
  • 85
  • 116
3
rm -f `find . | xargs grep -li email@example.com`

does the job better. Use `...` to run the command to offer the file names containing email.@example.com (grep -l lists them, -i ignores case) to remove them with rm (-f forcibly / -i interactively).

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
3

I liked Martin Beckett's solution but found that file names with spaces could trip it up (like who uses spaces in file names, pfft :D). Also I wanted to review what was matched so I move the matched files to a local folder instead of just deleting them with the 'rm' command:

# Make a folder in the current directory to put the matched files
$ mkdir -p './matched-files'

# Create a script to move files that match the grep
# NOTE: Remove "-name '*.txt'" to allow all file extensions to be searched.
# NOTE: Edit the grep argument 'something' to what you want to search for.

$ find . -name '*.txt' -print0 | xargs -0 grep -al 'something' | awk -F '\n' '{ print "mv \""$0"\" ./matched-files" }' > doit.sh

Or because its possible (in Linux, idk about other OS's) to have newlines in a file name you can use this longer, untested if works better (who puts newlines in filenames? pfft :D), version:

$ find . -name '*.txt' -print0 | xargs -0 grep -alZ 'something' | awk -F '\0' '{ for (x=1; x<NF; x++) print "mv \""$x"\" ./matched-files" }' > doit.sh

# Evaluate the file following the 'source' command as a list of commands executed in the current context:
$ source doit.sh

NOTE: I had issues where grep could not match inside files that had utf-16 encoding. See here for a workaround. In case that website disappears what you do is use grep's -a flag which makes grep treat files as text and use a regex pattern that matches any first-byte in each extended character. For example to match Entité do this:

grep -a 'Entit.e'

and if that doesn't work then try this:

grep -a 'E.n.t.i.t.e'
FocusedWolf
  • 1,062
  • 16
  • 19
2
find . | xargs grep -l email@example.com

how to remove:

rm -f 'find . | xargs grep -l email@example.com'
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
marian
  • 21
  • 1
  • 1
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. - [From review](https://stackoverflow.com/review/low-quality-posts/11620068) – Ferrybig Mar 14 '16 at 09:51
  • Find works, remove not (centos 6) – user2455079 Oct 02 '22 at 18:47
1

Quick and efficent. Replace find_files_having_this_text with the text you want to search.

grep -Ril 'find_files_having_this_text'  . |  xargs rm
George Chalhoub
  • 14,968
  • 3
  • 38
  • 61