Delete files with string found in file - linux cli

2019-01-30 04:20发布

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@domain.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@domain.com

Thank you for your assistance.

7条回答
孤傲高冷的网名
2楼-- · 2019-01-30 04:47

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@domain.com' '{}' \; -delete
查看更多
登录 后发表回答