Find Files containing specific Text in Linux

In this article, we will discuss different ways to find files that contain a specific text.

When we want to find the files containing specific text in our Linux system instead of searching for every directory, we can achieve this with the grep command. This article will guide you on how to find the files containing the text with the grep command.

The grep command in Linux:

The syntaxs for finding the files containing specific text.

Syntax 1:

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

Syntax 2:

grep [option] directorypath -e "string pattern to search" 

Possible values of option,

-r or -R: tells the grep to read the files recursively under each directory
-w: matches the whole word
-n: specifies the line number
-l: specifies the filenames of matching files
-e: specifies the string pattern to be searched
-i: ignore the case while searching

Let’s see some examples,

Example 1:

I am searching for the CFBundleType string in the path downloads.

  • rl as the option it will search recursively and gives the filename path.
grep -rl 'CFBundleType' Downloads

Output:

Downloads/Sourcetree/Contents/Info.plist

It looked inside all the files in directory “Downloads” and its sub-directories and printed the paths of all the files which contain the text “CFBundleType”.

Example 2:

  • With the rn as the option it will search recursively and gives the file name path along with the line number.
grep -rn 'CFBundleType' Downloads

Output:

Downloads/Sourcetree/Contents/Info.plist:12:<key>CFBundleTypeName</key>
Downloads/Sourcetree/Contents/Info.plist:14:<key>CFBundleTypeOSTypes</key>
Downloads/Sourcetree/Contents/Info.plist:18:<key>CFBundleTypeRole</key>

It looked inside all the files in directory “Downloads” and its sub-directories for the files that contains the text “CFBundleType”.

Here it printed the paths of all the files which contain the text “CFBundleType”. Along with the path it also printed the line number at which text is found in the file.

Example 3:

  • With the r as the option it will search recursively.
grep -r 'CFBundleType' Downloads

Output

Downloads/Sourcetree/Contents/Info.plist:<key>CFBundleTypeName</key>
Downloads/Sourcetree/Contents/Info.plist:<key>CFBundleTypeOSTypes</key>
Downloads/Sourcetree/Contents/Info.plist:<key>CFBundleTypeRole</key>

Example 4:

  • rw as the option it will search recursively and will check for the whole matched word (not a substring)
grep -rw Downloads -e 'CFBundleType'

It looked inside all the files in directory “Downloads” and its sub-directories for the files that contains the whole word “CFBundleType”. Here, no matched whole word was found.

Example 5:

  • With the Rnw as the option, It will search recursively,w will check for matched word and n will give the line number of that whole matched word.
grep -Rnw Downloads -e 'CFBundleTypeName'

Output

Downloads/Sourcetree/Contents/Info.plist:12:<key>CFBundleTypeName</key>

It looked inside all the files in directory “Downloads” and its sub-directories for the files that contains the whole word “CFBundleType”.

Here it printed the paths of all the files which contain the whole word “CFBundleType”. Along with the path it also printed the line number at which text is found in the file.

Example 6:

  • rni as the option, It will search recursively,n will give the line number of that matched string, and i will ignore the case.
grep -rni 'cfbundleType' Downloads

Output

Downloads/Sourcetree/Contents/Info.plist:12:<key>CFBundleTypeName</key>
Downloads/Sourcetree/Contents/Info.plist:14:<key>CFBundleTypeOSTypes</key>
Downloads/Sourcetree/Contents/Info.plist:18:<key>CFBundleTypeRole</key>

It looked inside all the files in directory “Downloads” and its sub-directories for the files that contains the text “CFBundleType” by ignoring the case of text.

Here it printed the paths of all the files which contain the text “CFBundleType” in any case basically by ignoring the case. Along with the path it also printed the line number at which text is found in the file.

Summary:

In this guide, We have learned how to find the files containing specific text in our Linux system.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top