Table of Contents
- Introduction
- How to use grep command
- How to use grep command with ignore case option (-i )
- How to use grep command with invert-match option (-v)
- How to use grep command with max count option (-m)
- How to use grep command with the Line number option (-n)
- How to use grep command with-filename (-H) and no-filename (-h) options
- How to use grep command with only-matching pattern option (-o)
- How to use grep command with suppress errors (-s) and suppress normal output (-q) options
- How to handle binary file while using grep command
- How to handle directory files while using the grep command
- How to use grep command with files-with-matches option (-l) and files-without-match option (-L )
- How to use grep command to count the matching lines on a file using the count option (-c)
- How control context with the Grep command
- How to use Grep command with Option “E“ (extended-regex) or Egrep for pattern matches
- How to use Fgrep or grep -F
- How to use Zgrep
Introduction
Wondering how to use different options in the grep command to get your filtering job done ?
This article explains Grep command with the different options supported in detail
How to use grep command
Using grep we can filter and print the line which matches the pattern we provide as a match condition.
Grep can be used in two ways ,
one way is when we pipe the outputs from another command to grep and it can work to filter based on match condition
For example : In the list of files we are matching the file name with pattern “long“
without case sensitivity and printed it
[root@discoveringsystems-centos test]# ls | grep -i long
LoNGfiLe51.txt
LoNGfiLe52.txt
LoNGfiLe53.txt
LoNGfiLe54.txt
Another way is we can ask grep to match the pattern on a file and it will print all the lines which matches that pattern inside the file
For example: On the file LoNGfiLe58.txt
, we were printing the line which matches the pattern “second ” without case sensitivity
[root@discoveringsystems-centos test]# cat LoNGfiLe58.txt
LoNGfiLe58
this is the second line
[root@discoveringsystems-centos test]# grep -i second LoNGfiLe58.txt
this is the second line
With this way we can also do a recursive way of search , where we can check files within directories recursively , which is very useful
Here in this example , i have the same pattern to match however , the files are now under another folder named “long“
in our present working directory
[root@discoveringsystems-centos test]# ls
file100.txt file26.txt file29.txt file32.txt file35.txt file38.txt file41.txt file44.txt file47.txt file50.txt long
Now we are doing a regular search without the recursive option for all the files under the directory test , we didn’t get any results for our search
[root@discoveringsystems-centos test]# grep -i "second" *
grep: long: Is a directory
While using the recursive option (-r) , grep command was able to recursively search for the pattern “long “ under the directory long.
[root@discoveringsystems-centos test]# grep -ir "second" *
long/LoNGfiLe51.txt:this is the second line
long/LoNGfiLe52.txt:this is the second line of file 52
long/LoNGfiLe53.txt:this is the second line of file 53
Grep command support various options to be used along with it for different use cases , we will be seeing examples on how to use them in the below sections. In order to view all the options supported by the grep command you can use the help option
[root@discoveringsystems-centos test]# grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c
.
.
.
[ Command output trimmed for brevity ]
Let’s explore the use cases of different options available to use with grep command to get our filtering / matching job done
How to use grep command with ignore case option (-i )
If you see the example file names below , we have file names with different cases ( upper and lower cases ), so if we use basic grep to filter it , we won’t be able to see all the file names.
I am looking to print all the filenames which has the pattern “long” on it
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt LoNGfiLe51.txt LoNGfiLe56.txt
file1.text file29.txt file34.txt file39.txt file44.txt file49.txt LoNGfiLe52.txt LoNGfiLe57.txt
file50.txt LoNGfiLe53.txt LoNGfiLe58.txt
file5.text LoNGfiLe54.txt LoNGfiLe59.txt
FiLe5.text LoNGfiLe55.txt LoNGfiLe60.txt
[root@discoveringsystems-centos test]# ls | grep long
[root@discoveringsystems-centos test]#
Now trying the same with the ignore case option (-i) and I was able to see the filtered file names
[root@discoveringsystems-centos test]# ls | grep -i long
LoNGfiLe51.txt
LoNGfiLe52.txt
LoNGfiLe53.txt
LoNGfiLe54.txt
LoNGfiLe55.txt
LoNGfiLe56.txt
LoNGfiLe57.txt
LoNGfiLe58.txt
LoNGfiLe59.txt
LoNGfiLe60.txt
How to use grep command with invert-match option (-v)
If we would like to print all filenames without the pattern “long” from the list of example file names present , we can use the invert-match option
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt LoNGfiLe51.txt LoNGfiLe56.txt
[root@discoveringsystems-centos test]# ls | grep -iv long
file100.txt
file28.txt
file33.txt
file38.txt
file43.txt
file48.txt
How to use grep command with max count option (-m)
Let’s say you would like to stop the search using grep after couple number of matches , then we can leverage the max count option (-m <num> )
#list of files under a directory
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt LoNGfiLe51.txt LoNGfiLe56.txt
file1.text file29.txt file34.txt file39.txt file44.txt file49.txt LoNGfiLe52.txt LoNGfiLe57.txt
file25.txt file30.txt file35.txt file40.txt file45.txt file50.txt LoNGfiLe53.txt LoNGfiLe58.txt
file26.txt file31.txt file36.txt file41.txt file46.txt file5.text LoNGfiLe54.txt LoNGfiLe59.txt
file27.txt file32.txt file37.txt file42.txt file47.txt FiLe5.text LoNGfiLe55.txt LoNGfiLe60.txt
# List of filtered file names
[root@discoveringsystems-centos test]# ls | grep -i file -m 3
file100.txt
file1.text
file25.txt
[root@discoveringsystems-centos test]# ls | grep -i file -m 5
file100.txt
file1.text
file25.txt
file26.txt
file27.txt
How to use grep command with the Line number option (-n)
Sometimes it will be useful to print a line number of the matched pattern while using grep. For those use cases we can use the line number option (-n) with the grep command
# list of files under a directory
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt LoNGfiLe51.txt LoNGfiLe56.txt
file1.text file29.txt file34.txt file39.txt file44.txt file49.txt LoNGfiLe52.txt LoNGfiLe57.txt
file25.txt file30.txt file35.txt file40.txt file45.txt file50.txt LoNGfiLe53.txt LoNGfiLe58.txt
file26.txt file31.txt file36.txt file41.txt file46.txt file5.text LoNGfiLe54.txt LoNGfiLe59.txt
file27.txt file32.txt file37.txt file42.txt file47.txt FiLe5.text LoNGfiLe55.txt LoNGfiLe60.txt
# list of filtered files with the line number [root@discoveringsystems-centos test]# ls | grep -in long
31:LoNGfiLe51.txt
32:LoNGfiLe52.txt
33:LoNGfiLe53.txt
34:LoNGfiLe54.txt
35:LoNGfiLe55.txt
36:LoNGfiLe56.txt
37:LoNGfiLe57.txt
38:LoNGfiLe58.txt
39:LoNGfiLe59.txt
40:LoNGfiLe60.txt
How to use grep command with-filename (-H) and no-filename (-h) options
In the following example , All the files whose filename is “LoNGFiLe”
has one line called “this is a second line”
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt LoNGfiLe51.txt LoNGfiLe56.txt
While using with-filename option (-H) I am able to see the pattern matches along with the file on which the pattern is found. In the below example , second is a pattern inside the file LoNGfiLe51.txt. It is matched and printed
[root@discoveringsystems-centos test]# grep -i second -H *
LoNGfiLe51.txt:this is a second line
LoNGfiLe56.txt:this is a second line
While using without-filename option (-h) , we just print the matching pattern inside the files and wont print the filename on which the matching pattern is found
[root@discoveringsystems-centos test]# grep -i second -h *
this is a second line
this is a second line
How to use grep command with only-matching pattern option (-o)
With only-matching option (-o) used along with the Grep command we can print the pattern number of times it is found in the input and wont print the entire line which has that matching pattern
In the below example we are trying to filter the filenames which have the pattern “long” without case sensitivity and only-matching option. We were able to see that it printed only the matched pattern “LoNG” instead of the entire filename like “LoNGfiLe51.txt”
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt LoNGfiLe51.txt LoNGfiLe56.txt
[root@discoveringsystems-centos test]# ls | grep -i long -o
LoNG
LoNG
How to use grep command with suppress errors (-s) and suppress normal output (-q) options
In the following example , I have a directory named long and all other files are text files. When trying to match the pattern “second” inside those files , I am getting an error mentioning long is a directory and the rest other matches are printed. Then I suppressed the error with the -s ( suppress error option )
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt long LoNGfiLe55.txt LoNGfiLe60.txt
[root@discoveringsystems-centos test]# grep -i second *
grep: long: Is a directory
LoNGfiLe55.txt:this is a second line
LoNGfiLe60.txt:this is a second line
[root@discoveringsystems-centos test]# grep -i second * -s
LoNGfiLe55.txt:this is a second line
LoNGfiLe60.txt:this is a second line
If you want to suppress the normal output , but want to see only the error you can use the -q ( suppress normal output option ). In the below example we can see only the errors and normal outputs are suppressed
[root@discoveringsystems-centos test]# grep -i second * -q
grep: long: Is a directory
How to handle binary file while using grep command
Sometimes when you are trying to match a pattern in a set of files , we can also see some binary files along with that. In the below example , i have a binary file named sed in the set of files
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt long LoNGfiLe55.txt LoNGfiLe60.txt
sed
Now when i try to use regular grep trying to match a pattern “stream”
i will get some errors messages when it is trying to process the binary files
[root@discoveringsystems-centos test]# grep -i stream *
grep: long: Is a directory
LoNGfiLe55.txt:this is a second stream
LoNGfiLe60.txt:this is a second stream
Binary file sed matches
Now to avoid seeing this message you can skip the processing of the Binary files using the option “ –binary-file=without-match “ along with grep , like the example below
[root@discoveringsystems-centos test]# grep -i stream * --binary-file=without-match
grep: long: Is a directory
LoNGfiLe55.txt:this is a second stream
LoNGfiLe60.txt:this is a second stream
If you would like to match a pattern present inside the binary files , then you can use the option “ –binary-file=text “ to treat those files as text files and grep tries to look for the pattern inside that as well
[root@discoveringsystems-centos test]# grep -i stream * --binary-file=text
grep: long: Is a directory
LoNGfiLe55.txt:this is a second stream
LoNGfiLe60.txt:this is a second stream
sed: long stream.
How to handle directory files while using the grep command
While using grep command on all the files in particular directory , you might end up seeing some directories as well present along with that. In that case we can use some handling mechanism for the directories while grep is seeing a directory
To skip processing directory files you can use the option ‘ –directories=skip ‘
We can see the grep is trying to read the Directory long while matching pattern “stream” and raised an error message
[root@discoveringsystems-centos test]# grep -i stream *
grep: long: Is a directory
LoNGfiLe55.txt:this is a second stream
LoNGfiLe60.txt:this is a second stream
Binary file sed matches
Here we skipped processing the directory using the option
[root@discoveringsystems-centos test]# grep -i stream * --directories=skip
LoNGfiLe55.txt:this is a second stream
LoNGfiLe60.txt:this is a second stream
Binary file sed matches
Likewise we can also set the action to recurse using the option ‘ –directories=recurse ‘ incase we need to match for the pattern in the files inside the directory
[root@discoveringsystems-centos test]# grep -i stream * --directories=recurse
long/longestfile.txt:this is a longest stream file in discovering system example
LoNGfiLe55.txt:this is a second stream
LoNGfiLe60.txt:this is a second stream
Binary file sed matches
Or ( both the examples does the similar job “-r = directories action recurse ” )
[root@discoveringsystems-centos test]# grep -ri stream *
long/longestfile.txt:this is a longest stream file in discovering system example
LoNGfiLe55.txt:this is a second stream
LoNGfiLe60.txt:this is a second stream
Binary file sed matches
How to use grep command with files-with-matches option (-l) and files-without-match option (-L )
In the following example we are printing all the file names for which we have the match for the pattern ( option files-with-match -l)
[root@discoveringsystems-centos test]# grep -i stream * -l
grep: long: Is a directory
LoNGfiLe55.txt
LoNGfiLe60.txt
Binary file sed matches
In the following example , we have the filenames for which there is no matches for the pattern ( option Files-without-match -L)
[root@discoveringsystems-centos test]# grep -i stream * -L
file100.txt
file1.text
file25.txt
grep: long: Is a directory
Binary file sed matches
How to use grep command to count the matching lines on a file using the count option (-c)
In the following list of files and folder , i am counting the number of times the pattern long is matched using grep option -c ( count) and we can find it appearing to be 11 times
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt long LoNGfiLe55.txt LoNGfiLe60.txt
file1.text file29.txt file34.txt file39.txt file44.txt file49.txt LoNGfiLe51.txt LoNGfiLe56.txt sed
file25.txt file30.txt file35.txt file40.txt file45.txt file50.txt LoNGfiLe52.txt LoNGfiLe57.txt
file26.txt file31.txt file36.txt file41.txt file46.txt file5.text LoNGfiLe53.txt LoNGfiLe58.txt
file27.txt file32.txt file37.txt file42.txt file47.txt FiLe5.text LoNGfiLe54.txt LoNGfiLe59.txt
[root@discoveringsystems-centos test]# ls | grep -i long -c
11
[root@discoveringsystems-centos test]# ls | grep -i long
long
LoNGfiLe51.txt
LoNGfiLe52.txt
LoNGfiLe53.txt
LoNGfiLe54.txt
LoNGfiLe55.txt
LoNGfiLe56.txt
LoNGfiLe57.txt
LoNGfiLe58.txt
LoNGfiLe59.txt
LoNGfiLe60.txt
How control context with the Grep command
How to use Before-context option with grep command
To print two lines above the matching pattern we can use the option -B 2 ( before-context )
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt long LoNGfiLe55.txt LoNGfiLe60.txt
file1.text file29.txt file34.txt file39.txt file44.txt file49.txt LoNGfiLe51.txt LoNGfiLe56.txt sed
file25.txt file30.txt file35.txt file40.txt file45.txt file50.txt LoNGfiLe52.txt LoNGfiLe57.txt
file26.txt file31.txt file36.txt file41.txt file46.txt file5.text LoNGfiLe53.txt LoNGfiLe58.txt
file27.txt file32.txt file37.txt file42.txt file47.txt FiLe5.text LoNGfiLe54.txt LoNGfiLe59.txt
[root@discoveringsystems-centos test]# ls | grep -i 45
file45.txt
[root@discoveringsystems-centos test]# ls | grep -i 45 -B 2
file43.txt
file44.txt
file45.txt
How to use After-context (-A ) option with grep command
To match 2 lines after the matching pattern we can use -A 2 ( After context ) option
[root@discoveringsystems-centos test]# ls | grep -i 45 -A 2
file45.txt
file46.txt
file47.txt
How to use Context (-C) option with grep command
If we would like to print 2 lines before and after the matching pattern then we can use -C 2 ( context ) option
[root@discoveringsystems-centos test]# ls | grep -i 45 -C 2
file43.txt
file44.txt
file45.txt
file46.txt
file47.txt
How to use Grep command with Option “E“ (extended-regex) or Egrep for pattern matches
To do regular expression based pattern matches in the grep command we can use the option E ( extended-regex). Following examples explains the use case of the regex pattern matches
In the following list of files and folders , we would like to match the pattern long or sed or files40 through 49 , so we used regex based pattern matching with grep
[root@discoveringsystems-centos test]# ls
file100.txt file28.txt file33.txt file38.txt file43.txt file48.txt long LoNGfiLe55.txt LoNGfiLe60.txt
file1.text file29.txt file34.txt file39.txt file44.txt file49.txt LoNGfiLe51.txt LoNGfiLe56.txt sed
file25.txt file30.txt file35.txt file40.txt file45.txt file50.txt LoNGfiLe52.txt LoNGfiLe57.txt
file26.txt file31.txt file36.txt file41.txt file46.txt file5.text LoNGfiLe53.txt LoNGfiLe58.txt
file27.txt file32.txt file37.txt file42.txt file47.txt FiLe5.text LoNGfiLe54.txt LoNGfiLe59.txt
[root@discoveringsystems-centos test]# ls | grep -E "long|sed|file4."
file40.txt
file41.txt
file42.txt
file43.txt
file44.txt
file45.txt
file46.txt
file47.txt
file48.txt
file49.txt
long
sed
Similarly matching for the pattern long or sed or files 40 through 45 we can change the regex like the following example
[root@discoveringsystems-centos test]# ls | grep -E "long|sed|file4[0-5]"
file40.txt
file41.txt
file42.txt
file43.txt
file44.txt
file45.txt
long
sed
Or ( both provide similar results , egrep is equivalent to grep -E )
[root@discoveringsystems-centos test]# ls | egrep "long|sed|file4[0-5]"
file40.txt
file41.txt
file42.txt
file43.txt
file44.txt
file45.txt
long
sed
How to use Fgrep or grep -F
Fgrep is used when dealing with match patterns which has meta characters like $ , ^ , etc we don’t need to use escape each and every characters with the backslash (\)
[root@discoveringsystems-centos long]# cat longestfile.txt
this is a longest file in discovering system example
$50$ ^
Example , using normal grep we were not able to match the following pattern , but with fgrep we should be able to do it
[root@discoveringsystems-centos long]# grep "$50$" longestfile.txt
[root@discoveringsystems-centos long]# fgrep $50$ longestfile.txt
$50$ ^
Or ( both does the same , fgrep is equivalent to grep -F)
[root@discoveringsystems-centos long]# grep -F "$50$" longestfile.txt
$50$ ^
How to use Zgrep
Using zgrep we can directly grep compressed ( gzipped ) files like the example below
[root@discoveringsystems-centos long]# zcat longestfile.txt.gz
this is a longest file in discovering system example
$50$ ^
[root@discoveringsystems-centos long]# ls longestfile.txt.gz
longestfile.txt.gz
[root@discoveringsystems-centos long]# zgrep -i long longestfile.txt.gz
this is a longest file in discovering system example