How do I find all files containing specific text on Linux?

I have been using Linux for a while on my server and have a large collection of the text file everywhere. I’m interested in learning about searching a text string. Can you tell me – how do I find a file containing a particular text string on my Linux server?

I have to admit that there are tens and thousands of text files on any Linux or Unix based server. Finding and locating those files can be done with the find command. Unfortunately find command cannot look inside a text file for a string.

You need to use the grep command. The grep command searches the given input FILEs for lines containing a match or a text string.

grep command syntax

The syntax is:

grep "text string to search" directory-path

OR

grep [option] "text string to search" directory-path

OR

grep -r "text string to search" directory-path

OR

grep -r -H "text string to search" directory-path

OR

egrep -R "word-1|word-2" directory-path

OR

egrep -w -R "word-1|word-2" directory-path

Examples

In this example, search for a string called ‘redeem reward’ in all text (*.txt) files located in /home/tom/ directory, use:

$ grep "redeem reward" /home/tom/*.txt

OR

$ grep "redeem reward" ~/*.txt

Example 2

find ./ -type f -exec grep -Hn "Text To Find" {} \;

Use -A and -B flags to print lines before and after the match:

find ./ -type f -exec grep -Hn -A1 -B1 "Text To Find" {} \;

also you can just use grep:

grep -R -Hn -A1 -B1 "Text To Find" *

More Examples

TASK: SEARCH ALL SUBDIRECTORIES RECURSIVELY

You can search for a text string all files under each directory, recursively with -r option:

$ grep -r "redeem reward" /home/tom/

OR

$ grep -R "redeem reward" /home/tom/
TASK: ONLY DISPLAY FILENAMES

By default, the grep command prints the matching lines. You can pass -H option to print the filename for each match:

$ grep -H -r "redeem reward" /home/tom

Sample outputs:

filename.txt: redeem reward 
foobar.txt: redeem reward 
...

To just display the filename use the cut command as follows:

$ grep -H -R vivek /etc/* | cut -d: -f1

Sample outputs:

filename.txt
foobar.txt
...
TASK: SUPPRESS FILE NAMES

The grep command shows output on a separate line, and it is preceded by the name of the file in which it was found in the case of multiple files. You can pass the -h option to suppress inclusion of the file names in the output:

$ grep -h -R 'main()' ~/projects/*.c
TASK: DISPLAY ONLY WORDS

You can select only those lines containing matches that form whole words using the -woption. In this example, search for word ‘getMyData()’ only in ~/projects/ dirctory:

$ grep -w -R 'getMyData()' ~/projects/
TASK: SEARCH FOR TWO OR MORE WORDS

Use the egrep command as follows:

$ egrep -w -R 'word1|word2' ~/projects/
TASK: HIDE WARNING SPAM

grep command generate error message as follows due to permission and other issues:

No such file or directory
No such device or address
Permission denied

To hide all errors or warning message spam generated by the grep command, append 2>/dev/null to grep command. This will send and hide unwanted output to /dev/null device:

$ grep -w -R 'getMyData()' ~/projects/ 2>/dev/null

TASK: DISPLAY MATCHED TEXT IN COLOR

Pass the --color option to the grep command display matched text/words in color on the terminal:

grep --color 'word' file
grep --color -R 'word' /path/to/dir
grep --color -R "192.168.1.5" /etc/
grep --color -R -h "192.168.1.5" /etc/
grep --color -R -h "192.168.1.5" /etc/ 2>/dev/null

TASK: IGNORE CASE

Our final example ignore case distinctions in both the search PATTERN and the input files:

grep -i -R 'word' /path/to/dir
grep -i -r 'income tax' ~/accounting/

References