How to Find Files with Specific Text on Linux

See all GNU/Linux related FAQ
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 I find a file containing a particular text string on my Linux server? What Linux command can find text in files?

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 or egrep command searches the given input FILEs for lines containing a match or a text string.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux terminal
Category Searching
Prerequisites find and grep commands
OS compatibility BSD Linux macOS Unix WSL
Est. reading time 5 minutes

grep command syntax for finding a file containing a particular text string

The Linux syntax to find string in files is as follows:
$ grep "text string to search" directory-path
$ grep [option] "text string to search" directory-path
$ grep -r "text string to search" /directory-path
$ grep -r -H "text string to search" directory-path
$ grep -E -R "word-1|word-2" /path/to/directory
# Find string in files on Linux #
$ grep -E -w -R "word-1|word-2" directory-path

Let us see some common example on how to use grep to search for strings in files.

How to search and find all files for a given text string

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
Let us find text called “redeem reward” in files under Linux in the $HOME (home dir):
$ grep "redeem reward" ~/*.txt

Task: Search all subdirectories recursively to find text in files

You can search for a text string all files under each directory, recursively with -r option:
$ grep -r "redeem reward" /home/tom/
$ grep -R "redeem reward" /home/tom/
Look for all files containing cacheRoot text on Linux:
$ grep -R cacheRoot /home/vivek/

Finding a File Containing a Particular Text String In Linux

Trying to find all files containing specific text on my Linux desktop

I want to search the whole Linux server for a string. In other words, use the following command to search for a word called “barfoo”:
$ sudo grep -R "barfoo" /
The / indicate root file system. The above command may take a lot of time. Hence, it is better to restrict the search to particular directory as per your needs:
$ sudo grep -R "barfoo" /etc/

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:


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 -w option. 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:
$ grep -E -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
The above command searches for all files in the directory ~/projects/ and its subdirectories that contain the function or string named getMyData(). The -w flag tells grep to only match the function name (or string) exactly, and the -R flag tells grep to recursively search subdirectories. The 2>/dev/null redirects any error or warning messages printed on stderr to the null device, which means that they will be discarded.

Task: Display matched text in color

Pass the --color option to the grep command display matched text/words/strings in color on the terminal:
$ grep --color 'word' file
$ grep --color -R 'word' /path/to/dir
$ grep --color -R "" /etc/
$ grep --color -R -h "" /etc/
$ grep --color -R -h "" /etc/ 2>/dev/null

Fig.01: grep command in action with colors and hiding the warnings on screen

Fig.01: grep command in action with colors and hiding the warnings on screen

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/

By “ignore case distinctions”, I mean that the grep command will match the search pattern such as “income tax” regardless of whether the letters are uppercase or lowercase. So, the pattern “income tax” will match the strings “Income Tax“, “INCOME TAX“, “iNcomE tAx” and so on. The -i option tells the grep command that you are searching for a pattern that may be written in uppercase or lowercase or combination of both.

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

The syntax is:
$ grep -E 'pattern' -rnw /path/to/dir/
$ grep -E 'word1|word2' -rnw /home/vivek/backups/

Summing up

You learned how to search and find a file containing a particular text string (words) under Linux using the grep command.

Finding text strings within files using grep

In this example search for lines starting with any lowercase or uppercase letter:
$ grep "^[a-zA-Z]" -rns ~/projects/texts/

  • -r – Recursive search
  • -R – Read all files under each directory, recursively. Follow all symbolic links, unlike -r grep option
  • -n – Display line number of each matched line
  • -s – Suppress error messages about nonexistent or unreadable files
  • -w – Only work on words i.e. search only those lines containing matches that form whole words
  • -l – Show the name of each input file when match found
  • -i – Ignore case while searching (match a pattern that may be written in uppercase or lowercase)

Read the following grep command manual pages using the man command or help command:
$ man grep
$ grep --help

I hope these examples and explanation is helpful to all new Linux and Unix users! Let me know if you have any other questions in the comment section below.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

68 comments… add one
  • vinay Apr 24, 2015 @ 11:31

    thanks a lot, this is good stuff

  • AK Oct 2, 2015 @ 12:28

    I need help in the following situation. Thanks in advance.
    I have a text file named “a.txt” which contains the hash values of few thousand files. Next i have a folder containing few thousand text files and each file must have one hash value from which we can recognize individual text file.Now, i want to read each line of “a.txt” and want to search through the directory to find which file contains the particular hash value. Once found the file should be moved to some other folder.
    Can any body help in how can i perform this task?

  • abbas Aug 7, 2017 @ 9:53

    help me to learn all linux commad

  • Maaza Aug 20, 2020 @ 15:05

    I have group of control scripts with a specific path /apps/dev01/app/xxx/bin/xxx.ctl, where I will be having the entry for port number
    something like, ‘-Dserver.port=8111’.

    Question: Is is possible to list it like a table with results,

    app name        | Port
    xxx             | 8111
    yyy             | 8212

    and so on?

  • Anonymous May 14, 2022 @ 10:52

    Hi can any one help me in this question

    How can you search a string in entire file system in linux?

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite May 16, 2022 @ 5:36

      Run the grep command as root user:
      sudo grep -R "string-to-search" /

  • Munir Nawaz Sep 19, 2022 @ 4:32

    very precise and useful commands

  • Manoj Aug 28, 2023 @ 6:17

    how to find a particular word in particular area

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Aug 28, 2023 @ 7:54

      What do you mean by “particular area”?

  • tannu Apr 28, 2024 @ 17:14

    Thanks for the detailed guide on using grep to search for text strings in Linux files! It’s incredibly helpful for navigating through large collections of files on servers.

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.