Find and delete files with specific content

Sometimes you get bad files uploaded to your website. Files might be the same or might just contain a specific line of code and you might want to search for all the files and delete any file, that would contain a string. It is easy and simple with a single command!

cd /home # or any other folder from where you want to start your search
find . -type f -name \*.php -exec grep -l 'c99 shell' '{}' \; -delete

This command will search for files (-type f) which end with .php (-name \*.php)  – search for text “c99 shell” (without quotes, of course), list filename (-l) and delete it (-delete). Easy and simple.

Make sure you have a backup. 🙂

Adjust and enjoy!