Find files with specific string using Linux find command


Do you want to find files from thousand of files in script with specific string, For example which page exactly is showing copyright or from which included file specific function is being called.

This can easily be done by using linux find command

More specific use can be cleaning code of any malicious content or even scanning script for malicious content.


1) SSH Into server

cd into folder where your script reside
cd /home/user/public_html

2) Use Find Command to find string
Use this Find command to search for string in all files recursively
find . -type f -name "*" -exec grep -il string {} \;

Above command can be explained as
find . : dot after find means current path, you can also specify specific folder for example find js
-type : specify that you are searching for files thus f flag
-name : * suggest wildcard but you can search for specific extensions, For example if you want to only search php files then command would be
find . -type f -name "*.php" -exec grep -il string {} \;
-exec grep: this run regex search in file passed by find command for string.
string: replace this with actual string you want to search.

Leave a comment

Your email address will not be published. Required fields are marked *