Delete files with string found in file - linux cli

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

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

Thank you for your assistance.


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 | awk '{print "rm "$1}' >
vi // check for murphy and his law

@Martin Beckett posted an excellent answer, please follow that guideline

solution for your command :

grep -l * | xargs rm


for file in $(grep -l *); do
    rm -i $file;
    #  ^ prompt for delete

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 '' '{}' \; -delete

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 | xargs -I {} rm -rf {}

But I rather find by name:

$ find . -iname *something* | xargs -I {} echo {}

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" }' >

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" }' >

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

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'

rm -f `find . | xargs grep -li`

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

find . | xargs grep -l

how to remove:

rm -f 'find . | xargs grep -l'

Need Your Help

Questions about Comboboxes

.net winforms combobox

Firstly, is there any way I can set the color of the available choices a combo box will offer me? I can change the background color of the text box part, but I am talking about the Drop Down list p...