10+ useful grep command options with examples – Unix/Linux

The grep command is one of the most widely using Linux command. The name GREP is the abbreviation of Global Regular Expression Pattern. Yep! it’s pattern search tool under Unix.

By-default the grep command lists all matching lines from the input file or files. A lot of switches are available to lists or find matching information from a file using the command GREP.

Here I am listing most commonly using switches with example. After completing this topic you will a lot of idea about grep.

You can print all matching lines highlighted with color using the switch “–color” along with the command grep. Also, there are a lot of other useful options available with grep command 🙂

In short, I can list some commonly using syntax of grep.

grep word filename
grep word file1 file2 file3 etc

cat filename|grep word

grep "string1 string2" filename

To explain the “grep” command I have created a sample file.

# cat crybit_doc1

THIS IS THE FIRST LINE IN UPPER CASE
this is the second line in this file and the first line with all its characters in lower case
This Line Has All its First Character In Upper Case
This is the last line

1. Search for the given string in a file

The basic usage of “grep” is to search for specific string in the specified file

# grep "string" filename

Example:

root@server [~]# grep "last" crybit_doc1 
This is the last line

2. Checking for the given string in multiple files

# grep "string" file_pattern

This is also one of the basic usage of grep command. To illustrate this, I am copying the file crybit_doc1 to crybit_doc2. Both the files have the same pattern.

Example:

root@server [~]# grep "file" crybit_*
crybit_doc1:this is the second line in this file and the first line with all its characters in lower case
crybit_doc2:this is the second line in this file and the first line with all its characters in lower case

3. Case insensitive search

# grep -i "string" file_name

This is used for searching strings insensitively. It will display the string (the string that you are searching for) that are in upper cases and lower cases.

Example:

# grep -i "first" crybit_doc1
THIS IS THE FIRST LINE IN UPPER CASE
this is the second line in this file and the first line with all its characters in lower case
This Line Has All its First Character In Upper Case

4. Match regular expression in files

This is a very powerful feature for searching regular expression.

# grep "regexp" file_name

In the example mentioned below, it searches for all the pattern that starts with “second” and ends with “lower”

Example:

# grep "second.*lower" crybit_doc1
this is the second line in this file and the first line with all its characters in lower case

Note: A regular expression may be followed by one of several repition operators:

- ? The preceding item is optional and matched at most once
- * The preceding item will be matched zero or more times.
- + The preceding item will be matched one or more times.
- {n} The preceding item is matched exactly n times.
- {n,} The preceding item is matched n or more times.
- {,m} The preceding item is matched at most m times.
- {n,m} The preceding item is matched at least n times, but not more than m times.

grep_ex1

5. Checking for full-words not for substring

# grep -w "word" file_name

Here, I am explaining this through two examples.

Example 1: Normal search

# grep -i "is" crybit_doc1

THIS IS THE FIRST LINE IN UPPER CASE
this is the second line in this file and the first line with all its characters in lower case
This Line Has All its First Character In Upper Case
this is a line in lower case
This is the last line

Note: Here, all the lines with pattern “is” are listed. Eventhough, there is no “is” word in “This Line Has All its First Character In Upper Case” it is also displayed since there is “is” pattern in “This”.

Example 2:

# grep -iw "is" crybit_doc1
THIS IS THE FIRST LINE IN UPPER CASE
this is the second line in this file and the first line with all its characters in lower case
this is a line in lower case
This is the last line

Here the lines with the word “is” is only displayed. Here the switch searches for the word and not for the patten. “This Line Has All its First Character In Upper Case” is not displayed.

6. Recursive searching

When you want to search in all the files inside a directory, you can use -r option

# grep -r "string" *

Example:

# grep -r second *
crybit_doc1:this is the second line in this file and the first line with all its characters in lower case
crybit_doc2:this is the second line in this file and the first line with all its characters in lower case

7. Exclude pattern match

If you wish to exclude the pattern match, then you can use -v option

# grep -v "string" file_name

Example:

root@server [~/crybitdocs]# cat crybit_doc1
THIS IS THE FIRST LINE IN UPPER CASE
this is the second line in this file and the first line with all its characters in lower case
This Line Has All its First Character In Upper Case
this is a line in lower case
This is the last line
explore the world
root@server [~/crybitdocs]# grep -iv "this" crybit_doc1
explore the world

Here I am case insensitively excluding all the lines with “this”.

8. Counting the number of matches

For counting the number of matches, we can use the option -c

# grep -c "string" file_name

Example:

root@server [~/crybitdocs]# cat crybit_doc1

THIS IS THE FIRST LINE IN UPPER CASE
this is the second line in this file and the first line with all its characters in lower case
This Line Has All its First Character In Upper Case
this is a line in lower case
This is the last line
explore the world

root@server [~/crybitdocs]# grep -c "explore" crybit_doc1
1
root@server [~/crybitdocs]# grep -c "this" crybit_doc1
2
root@server [~/crybitdocs]# grep -ic "this" crybit_doc1
5

When you want to exclude the number of matches, then you can use -v option with -c

root@server [~/crybitdocs]# cat crybit_doc1

THIS IS THE FIRST LINE IN UPPER CASE
this is the second line in this file and the first line with all its characters in lower case
This Line Has All its First Character In Upper Case
this is a line in lower case
This is the last line
explore the world
root@server [~/crybitdocs]# grep -vc "explore" crybit_doc1
5

9.To display the file names that matches the pattern

# grep -l "string" file_pattern

Example:

root@server [~/crybitdocs]# grep -l "explore" crybit_*
crybit_doc1

10. Show only matched string

It might not be useful if you are starightly giving the string, but it will be useful if you want to show out only the matched string of the pattern

# grep -o "regexp" file_name

Example:

root@server [~/crybitdocs]# cat crybit_doc1 
THIS IS THE FIRST LINE IN UPPER CASE
this is the second line in this file and the first line with all its characters in lower case
This Line Has All its First Character In Upper Case
this is a line in lower case
This is the last line
explore the world
root@server [~/crybitdocs]# grep -io "is.*line" crybit_doc1
IS IS THE FIRST LINE
is is the second line in this file and the first line
is Line
is is a line
is is the last line

Here it displays the string in between “is” and “line”

11. Shows line number while displaying the output

# grep -n "string" file_name

Example:

root@server [~/crybitdocs]# cat crybit_doc1 
THIS IS THE FIRST LINE IN UPPER CASE
this is the second line in this file and the first line with all its characters in lower case
This Line Has All its First Character In Upper Case
this is a line in lower case
This is the last line
explore the world
root@server [~/crybitdocs]# grep -n "explore" crybit_doc1
6:explore the world

Lines and Word Anchor

a) The (^) anchors are used to represent that the pattern showing it should be the start of a line

grep_ex2
Here in the screen-shot,

-E refers to extended-regexp (regular expression)
[a-z] is used to list all the characters in lower case

b) The $ anchor specifies that the pattern before it should be at the end of the line

grep_ex3
Here, it displays only the lines that have “case”(insensitive) at the end. It doesnot displays the line “This case is to test a function”

c) The pattern “\<” represents the start of a word and “\>” represents the end of the word.

grep_ex4

Cool..!! 🙂

Other Unix commands:

groupdel, groupmems, groupmod

useradd , usermod , chgrp, chown

ls, head, tail, top, ps, find, crontab, ftp commands, tar, rpm, Rkhunter

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

One thought on “10+ useful grep command options with examples – Unix/Linux

Leave a Reply

Your email address will not be published. Required fields are marked *