How To Use The Grep Command in Linux
Grep is a powerful Linux command that lets you search for strings and expressions, across files and directories. It can be used to match patterns in log files, filter out useful information from the output of other commands, and perform recursive searches on nested directories. View our Linux server offers.
In the following guide, we’ll talk about the syntax of grep, and share some of its most handy use-cases. Don’t forget to download our Linux commands cheat sheet.
What is grep?
Grep is short for global regular expression print. Its name is inspired from the ed editor command g/re/p, which offers similar functionalities. Grep searches for a sequence of characters in a file or directory and outputs the matching lines. It is a handy tool for parsing log files and finding the occurrence of patterns across the entire file system.
Basic grep syntax and examples
The grep command has the following syntax:
grep [options] pattern [files]
Some of the available [options] are:
Command
Description
-i
-n
-c
-r
-R
-r but also includes symlinks.-H
-o
-l
-w
Now let’s look at a few basic grep examples:
Command
Description
grep 'wordtosearch' filename
grep 'ERROR' file1 file2
grep 'wordtosearch' *
grep -R 'ERROR' .
grep -i 'error' *
grep -c 'ERROR' logfile
More use-cases of the grep command
You can format the grep command in endless ways to execute your desired search. Let’s look at a few handy use-cases:
Matching a regular expression
Regex search is one of the most powerful features of grep. It allows you to specify a search pattern to match against. For example, you can use the following command to search for lines that contain the word error.
grep '^error$' logfile
Or to find lines that end with the word here:
grep 'here$' logfile
To match whole words only
If you want to look for a whole word, instead of a pattern or a substring, you can use the -w option. E.g. the following command searches the exact word arizona across all the files in the current directory.
grep -w 'arizona' *
To print lines that don’t match
grep also allows you to perform an inverse search, i.e. display the lines that don’t match. E.g. the following command will output all the lines that don’t contain the word arizona.
grep -v 'arizona' filename
Print lines before and after matching lines
Sometimes you may want to see the lines that appear close to the matching lines. This can be particularly useful while debugging. The -B and -A options can be used to display the lines before and after all the matching lines respectively. The following command will print 3 lines after the matches.
grep -A 3 "pattern" logfile
…and the following command displays 3 lines before the matched line:
grep -B 3 "pattern" logfile
Piping output into grep
The grep command enables you to filter through the output of another command, using piping. For example, you can check whether a program is running by piping the output of the ps -ef command to a grep filter.
ps -ef | grep 'processname'
Here’s a command to filter out only the CPU model name from the output of the cat /proc/cpuinfo command.
cat /proc/cpuinfo | grep 'model'
To display only the *.txt files created in the month of May, you can use this command:
ls -l *.txt | grep May
Display only the names of the matching files
If there are too many matching lines, and you only want to see the names of the files that contain the matched lines, you can use this command:
grep -Rl 'foo' /path/to/directory
Redirect grep output to a file
If the expected grep output is too large, you can redirect it towards a file. See the following command as an example:
grep 'error' filename > output.txt
To see line numbers along with the matches
To speed up your debugging, you may want to see the line numbers of the matched lines. Use the following command to do so:
grep -n -C 2 'arizona' filename
It will print the numbers of all the lines that contain the word arizona in the file.
Limit output lines in a grep search
If you only want to see the first x matches of a pattern in a file, you can use the -m command line option. For example, using the following command stops the grep search after the first 3 matches of the word arizona are found.
grep -m3 'arizona' filename
Finding special characters using grep
You can use the -F option to make grep search for special characters, like ' or [ or ^. For example, the following command will look for [abc] in the file.
grep -F '[abc]' filename
Display the position of the match
Grep also gives you the ability to print the position where it found a match. Note that this isn’t the line position, but the byte offset of the entire file. The -o and -b options will be used for this purpose.
grep -o -b "error" logfile
Display colored output
Sometimes it makes sense to add some colour to the output, and make it easier to understand. The --color option enables you to view colorized search results.
grep --color 'pattern' filename
Searching for multiple patterns
egrep is a variant of the grep command that offers extensive regex-based searching functionalities. For example, you can use it to search for multiple patterns at once.
egrep 'foo|bar|xyz' *
The above command will match all three strings, across all the files in the current directory.
Combining grep and find
You can combine the grep and find commands to execute advanced searches. For example, the following command displays the names of all files present in the current directory that contain the word bar, case-insensitive.
find . -type f -exec grep -il 'bar' {} \;
Searching through gzip files
The grep command can be used to execute zgrep searches on gzip files.
zgrep foo arch.gz
The above command will output all matching files present inside arch.gz.
Related posts
Need Assistance?
Our local Ghanaian support team
is ready to help.