bash script; delete all files from grep result


Active Member
Nov 10, 2012
cPanel Access Level
DataCenter Provider
i am using bash script to find all php shell scripts

grep -RPnDskip "(base64_decode) *\(" /home/domain/public_html >> /home/1.txt

and this is the output

is there any way so i can delete all output file automatically?
i mean when the shell script find the word "base64_decode" in "qaqa.php", it will delete the file from the server.


Well-Known Member
Oct 20, 2009
cPanel Access Level
DataCenter Provider
I usually use xargs. Also, the "l" flag on grep is useful as it makes a list of matching files.

Say for instance you know every file containing "badstring==' is malicious. You could do this:

find /home/USERNAME/public_html/ -type f -exec grep -Rl 'badstring==' {} \; > results.txt

Examine results.txt and MAKE SURE you want to remove the files. A good idea is to chmod 000 them first and make sure your site still functions:

cat results.txt | xargs chmod 000

Once you're SURE it's OK to remove the files, then you can

cat results.txt | xargs rm -f

If there are spaces in file names in the list, it could cause problems. I normally remove any files with spaces manually, remove those paths from results.txt, and then use the xargs command.

As always, make sure you have a working backup before you go nuking a bunch of files.
Last edited: