PDF download Download Article
Quick and easy ways to search for patterns using Grep
PDF download Download Article

If you want to search for a string of text in a file on Linux, the grep command will come in handy. Grep works the same in all versions of Unix, including the Mac Terminal and Windows Subsystem for Linux. This wikiHow article will teach you how to use grep in any version of Linux or Unix, including syntax tips and useful examples.

Things You Should Know

  • The syntax to use grep on Linux is kbd|grep [options] "string of text" /path/to/file .
  • The grep command is case-sensitive, but you can use grep -i to ignore case.
  • To search for text in all files within a directory and its subdirectories, use grep -R to perform a recursive search.
Section 1 of 2:

How to Use Grep to Search Files

PDF download Download Article
  1. To find a word or pattern in a file , use the basic grep syntax:
    • grep "string of text" filename
      • This command returns the full text of each line containing the text string you entered.
      • If you only want to see the matching words or strings, add the -o option.
      • The quotation marks are optional if you're only searching for a series of connected characters, but mandatory if you're searching for a string that contains spaces.
  2. By default, grep is case sensitive, meaning that grep "wikiHow" file.txt and grep "wikihow" file.txt will yield different results. To ignore case and return results having any variation of capital and lowercase letters, add the -i option:
    • grep -i "string of text" filename
      • You can combine -i with all other grep commands. [1]
    Advertisement
  3. For longer files, it can be helpful to see the line number of each instance of a string. Using the -n option displays both the line number and the full contents of each line that contains the string:
    • grep -n "string of text" filename
  4. When you search for a string of text, such as a partial word (e.g., grep -i "ikih" filename.txt ), you'll see matches from all instances of "ikih" together—even if they occur in a longer word (e.g., "w ikiH ow"). If you only want to see a result if it's a word on its own—not within a longer word—use the -w option.
    • grep -w "ikih" filename.txt
      • This search will not return the word "wikiHow" if it exists in your file. It will only return the word "ikih."
  5. If you want to know how many times certain text appears in a file, use the -c option.
    • grep -c "string of text" filename
      • Instead of displaying the text you're searching, you'll see a number, e.g., 5 .
    • This can be helpful when looking for specific IP addresses in firewall logs . For example, to see how many times the IP address 8.8.8.8 appears in your UFW log, you could use grep -cE "8.8.8.8" /var/log/ufw.log .
      • We must include the -E option when grepping for an IP address due to the periods in the address. Typically, grep interprets a period as a wildcard for a single character. Adding the -E option tells grep that the string is a regular expression. [2]
  6. Using the -v option with grep is like a reverse search. Instead of displaying all instances of a string of text in a file, you'll see a list of all lines that don't contain that string.
    • grep -v "string of text" filename
  7. To find lines that contain two specific words or patterns, you can use the pipe symbol to perform a second grep command. For example, let's say you want to find all lines that contain the words "wikiHow" and "Google:"
    • grep -i "wikiHow" filename.txt | grep -i "Google"
      • Note that we use single quote marks around our search terms.
      • For example, let's say we want to see all connections from IP address 8.8.8.8 in our firewall log from Nov 11 but no other dates: grep -E "8.8.8.8" /var/log/ufw.log | grep "Nov 11"
    • Another way to do this is by using regular expressions and the -E option. This command will accomplish the same result as the previous command:
      • grep -E "wikiHow.*Google|Google.*wikiHow" filename.txt
    • Alternatively, you can search for lines that contain either one word or the other:
      • grep -E 'wikiHow|Google' filename.txt .
  8. If the string you're searching for needs to be at the beginning or end of a line, use grep this way:
    • Search for text at the beginning of a line: grep "^string of text" filename.txt
      • With this command, you will only see lines that begin with the text you entered.
    • Search for text at the end of a line: grep "string of text$" filename.txt
      • Only lines ending with "string of text" will appear.
  9. Advertisement
Section 2 of 2:

Use Grep in Directories

PDF download Download Article
  1. To find matching text in any file within a directory (and its subdirectories), use the -R option to search recursively:
    • grep -R "string of text" /home/myusername
      • Note that -R is different from -r . If you use a lowercase -r instead, your recursive search will not follow symbolic links.
      • Remember, you'll need to use -i to do a case-sensitive search with grep, e.g., grep -iR "string of text" * will search the current directory (and all subdirectories) for all instances of "string of text" regardless of case.
  2. Let's say you want to find all instances of the word "wikiHow" in your Python files , but not in other files in a directory. In this case, instead of entering a filename, you can use a wildcard:
    • grep wikiHow *.py
      • This search will display all instances of the word "wikiHow" in any file in the current directory that ends with *.py .
  3. If you want to know the name of each file containing a certain word or series of characters, use the -l option.
    • grep -l "string of text" /home/myusername
      • This command does not display the text—only the names of files that contain the text.
  4. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips

      • You can use grep with the pipe symbol | to parse the results of other commands. For example, if you're using Ubuntu or another Debian-based Linux, apt list --installed | grep -i python will display a list of all installed packages containing the word "python" in their names or paths.
      • When using grep, most shells will display matching terms in a different color than the other characters from the line. If yours doesn't, you can use grep --color -i "string of text" filename.txt .
      • To see a list of all grep options, use the command man grep .
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 8,609 times.

      Is this article up to date?

      Advertisement