Categories
Linux Server Administration Tips

Linux: How to find all the files containing a particular text string?

At Linux command line, to find a particular text string in all the files from the current directory recursively (that is, including all those files from the child or grandchild directories), use something like this via SSH:

find . -exec grep -l "needle" {} \;

This command searches through all directories from the current directory recursively for the files that contain the string “needle”.

To search only .php files:

find *.php -exec grep -l "needle" {} \;

To search a specific directory:

find dirname -exec grep -l "needle" {} \;

To find all .php files that contain “needle” in all the directories under “somedir”:

find somedir -name *.php -exec grep -l "needle" {} \;

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

3 replies on “Linux: How to find all the files containing a particular text string?”

BTW, if it spits out this error:

find: paths must precede expression

Embrace necessary parameters in double quotes “”. E.g.,

find somedir -name “*.php” -exec grep -l “needle” {} \;

Hi Yang Yang
i wish you are doing good, my name is henry and i am from Lima, Peru; in this moment i am testing soem software made in china but i cant get the chineese strings out from a binary file, do you any idea how to configure taht STRINGS command to get the chinesse characters? your commend will be helpfull
thanks in advance
henry

Comments are closed.