Grep command examples

grep is a powerful command line utility used for searching plain text data sets for lines that match a regular expression. grep stands for Global Regular Expression Print.

Below are some examples of how to use "grep" on a Linux system.

Search for a file

In the following example, we are going to search for any file names that contain the word "file" within our current working directory.


$ ls -l | grep file
-rw-rw-r-- 1 tux tux    6 May  1 12:21 file1.txt
-rw-rw-r-- 1 tux tux    6 May  1 12:21 file2.txt
-rw-rw-r-- 1 tux tux    6 May  1 19:00 file3.txt
-rw-rw-r-- 1 tux tux    6 May  1 19:00 file3.txt~
-rw-rw-r-- 1 tux tux    5 Apr 30 22:04 visible_file

In the above example, we used the "ls" command to list the contents of our directory and then used "grep" to find any files that contained the word "file".

Ignore Case

In the next example, we are going to use the "-i" parameter to ignore the case. First, we will search for all files that contain the string "upper".

In our current directory, we have two files, one is called "upper.txt", and the other is called "UPPER.txt". On Linux systems all file names are case sensitive. This means that although the files have the same name, they are treated as individual files as their case is different.


$ ls -l | grep upper
-rw-rw-r-- 1 tux tux  183 May 24 20:19 upper.txt

In this example, only one files was found that contained the string"upper". Now if we add the "-i" ignore case parameter, we will see that both files are now included within the results.


$ ls -l | grep -i upper
-rw-rw-r-- 1 tux tux  183 May 24 20:19 upper.txt
-rw-rw-r-- 1 tux tux  183 May 24 20:19 UPPER.txt

Search for text within a file

To search for a text pattern (string) within a text file, we use the standard grep command in conjunction with the file we would like to search for. In the example below, we will search for the userid "tux" within the file "etc/passwd".


$ grep tux /etc/passwd
tux:x:1001:1001:tux:/home/tux:/bin/bash

Search for multiple entries within a file

You can pass multiple strings for grep to search for. In the next example, we will search for the user "john" and the user "tux" entries within the "/etc/passwd" file.


$ grep 'tux\|john'  /etc/passwd
john:x:1000:1000:john,,,:/home/john:/bin/bash
tux:x:1001:1001:tux:/home/tux:/bin/bash

Search all files in current directory for a string

The next use of grep is useful if you are trying to locate a file that contains a specific string within a group of files. In this example, we will search all files within all directories within the current working directory for a string of "ZX80".

We will used the "-r" parameter to instruct our search to recursively search through all directories. The "*" specifies to look in all files. The "-i" instructs grep to ignore the case.


$ pwd
/home/tux

$ ls
bigfolder  demo2  demo5  file1.txt  file3.txt~  sftp_test   touch      visible_file
data       demo3  demo6  file2.txt  rsync_test  ssh_banner  upper.txt
demo       demo4  demo7  file3.txt  scp_demo    test1       UPPER.txt

$ grep -r -i zx80 *
demo4/retro.txt:ZX80

From the above, we can see that "grep" found a file called "retro.txt" in the directory "demo4" that contained the string "ZX80".

Exclude a search pattern

If you pass the parameter "-v", this tells "grep" to ignore any matching lines. In the following example, we can see that when "grep" is run, it displays itself within the results. Using the "-v" parameter we can exclude the term "grep".


# ps -ef | grep chronyd
chrony       968       1  0 19:40 ?        00:00:00 /usr/sbin/chronyd
root        3008    1315  0 20:57 pts/0    00:00:00 grep --color=auto chronyd

# ps -ef | grep chronyd | grep -v grep
chrony       968       1  0 19:40 ?        00:00:00 /usr/sbin/chronyd

Display the line number where string is matched

To find out the line number of a file where a specified string has been matched, you can use the following command:


$ cat distros.txt 
Manjaro
Mint
Pop!_OS
EndeavourOS
Ubuntu
Debian
elementary
Fedora
openSUSE
Rocky
CentOS
Arch

$ grep -n Debian distros.txt 
6:Debian

In the example, we were looking for a string of "Debian". grep has found the string and has indicated that it can be found on line 6 of the specified file.

Count the number of matches found

The following example shows you how to use "grep" to count the number of matches found within a file.


$ cat more_distros.txt 
MX Linux
Manjaro
Mint
Pop!_OS
Puppy
EndeavourOS
Ubuntu
Debian
elementary
Fedora
deepin
Puppy

$ grep -c Puppy more_distros.txt 
2

In the above example, our search string was "Puppy". Two entries were found within the file specified.

Display number of lines before and after a search string

The parameters "-A" and "-B" can be used to show lines Before and After a specified search string.

Tip: A = After and B = Before


$ cat more_distros.txt 
MX Linux
Manjaro
Mint
Pop!_OS
Puppy
EndeavourOS
Ubuntu
Debian
elementary
Fedora
deepin
Puppy

$ grep -A 2 Ubuntu more_distros.txt 
Ubuntu
Debian
elementary

$ grep -B 2 Ubuntu more_distros.txt 
Puppy
EndeavourOS
Ubuntu