This article was reviewed by Luigi Oppido
and by wikiHow staff writer, Nicole Levine, MFA
. Luigi Oppido is the Owner and Operator of Pleasure Point Computers in Santa Cruz, California. Luigi has over 25 years of experience in general computer repair, data recovery, virus removal, and upgrades. He is also the host of the Computer Man Show! broadcasted on KSQD covering central California for over two years.
This article has been fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources.
This article has been viewed 7,500 times.
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.
Steps
How to Use Grep to Search Files
-
1Search for a string of text. 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.
- grep "string of text" filename
-
2Search for text regardless of case. 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] X Research source
Advertisement - grep -i "string of text" filename
-
3Search for a text string and display line numbers. 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
-
4Search for a specific word. 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."
- grep -w "ikih" filename.txt
-
5Display how many times a string appears in a file. 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] X Research source
- grep -c "string of text" filename
-
6Display all lines that do not contain the pattern. 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
-
7Search for more than one word or pattern. 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 .
- grep -i "wikiHow" filename.txt | grep -i "Google"
-
8Search for lines that start or end with a string. 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.
- Search for text at the beginning of a line: grep "^string of text" filename.txt
Use Grep in Directories
-
1Search for a string in all files in a directory. 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.
- grep -R "string of text" /home/myusername
-
2Search for a string in all files containing specific characters. 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 .
- grep wikiHow *.py
-
3Display filenames that contain a string. 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.
- grep -l "string of text" /home/myusername
Expert Q&A
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.Thanks
- 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 .Thanks
- To see a list of all grep options, use the command man grep .Thanks