Unix grep Command Examples – How to Use grep command in Unix
Unix grep command search text patterns from files and return the matching lines and the filename. In This tutorial we will look at some useful Unix grep command examples you should be familiar with as a Unix/Linux Administrator.
Search Text Pattern from a given file
Basic Usage of grep command in Unix is to search given text pattern from a given file.
grep 'text pattern' filename
grep 'unix admin' file.txt
The grep command will search for the ‘unix admin’ in the file.txt and will return the every line that matched.
grep case insensitive search
By default, grep command in Unix is case sensitive. If you want to do a case insensitive search use the -i option with the grep command so the case will be ignored.
grep -i 'unix admin' file.txt
Again the grep command will search for the lines contains the word ‘unix admin’ but this time grep command will ignore case, because we have used -i option. So Lines that match Both Simple and Capital unix will be returned.
Search Multiple Strings Patterns
Using -e option we can specify multiple text patterns or strings.
grep -e 'string1' -e 'string2' filename
grep -e 'linux' -e 'unix' file.txt
The Command will return the lines containing either ‘unix’ or ‘linux’. This is similar to logical OR operator.
Also we can add -i option so case will be ignored
grep -i -e 'linux' -e 'unix' file.txt
Read the result with less command.
If you are searching a Large file, then the search result could be very long. In That case send the output to the less command to make it easier to read. Using the unix grep with less command will be very useful especially if you are reading a large log file.
grep -i kernel /var/log/messages | less
As above example, I searched for the word ‘kernel’ in the /var/log/messages file. There are too many matching lines, but with the less command I can scroll through the result using the arrow keys.
Invert match - Returns non matching lines
Option -v in unix grep command returns all lines that do not match the given text pattern.
grep -v '/sbin/nologin' /etc/passwd
Above example will output the all lines in /etc/passwd file that do not contain the pattern ‘/sbin/nologin’.
Just like in other Unix commands, we can send grep search result to a text file instead of printing to the terminal window.
grep 'text' file.txt >> outputfile
Return Line Count
With the -c option grep command in unix will return line counts instead of the lines. With the -v option count non-matching lines.
grep -c '/bin/bash' /etc/passwd
Above example will print a count of matching lines that contains /bin/bash in /etc/passwd file
Display Line Numbers
It will be very helpful if we print the line number of the each matching lines. Unix grep -n option will display the line number of each matching line.
grep -n root /etc/
grep all files in directory recursively
All the examples we looked at until now, we search text patterns in a single file. But grep command in unix can also search text pattern on all files inside a given folder as well as the subfolders of that folder using -R option. This we called as recursive grep search.
grep -R unix /etc/
grep command will search for word unix recursively on all files inside /etc directory. Both lines and the filename will be returned.
Display only the file name
When does a recursive grep search on directories, output will return both the matching lines and the name of the file that lines belong. But if you just want the file name, then use the -l option.
grep -R -l unix /etc/
The Command will return all files in /etc directory that match the text pattern Unix.
Return Files that contains not matches
Another useful option is -L option, which returns the all files that do not match the given text pattern.
grep -R -i -L error /var/log/
The Command will return all files that does not contains the string error inside the /var/log directory.
Search Start of a line
Using caret(^) symbol we can search for the beginning of a line.
grep "^soap" /etc/php.ini
The Unix grep command will search for the lines start with soap in php.ini file.
Search End of a line
Using dollar ($) symbol end of the text pattern we can search for the given text pattern end of a line.
grep "nologin$" /etc/passwd
Grep command will search for the lines ends with the string ‘nologin’ in /etc/passwd file.
Search Start of a Word
Using Backslash less-than(\<) before the text part ten we can search for the start of a word.
grep '\<dir' /etc/php.ini
As per above example, The grep command in unix will return any line php.ini file if the line contains a one or more word start with dir as below screenshot shows.
Search End of a Word
Using Backslash greater-than(\>) at the end of the text pattern we can search for the end of a word.
grep 'un\>' /etc/passwd
Grep will return any line passwd file if the line contains a one or more word ends with un as below screenshot shows.