How to use grep command on Debian 10

Grep stands for global regular expression print. It is a useful command and widely used by Linux system engineers while searching a string or patterns against regular files and in the system.

In this article, I am going to demonstrate the usage of grep command with a large number of examples. I have tested all the commands and examples on a Debian 10 machine.

Prerequisite

You need to have a single Debian 10 machine with root privileges.

Installing grep Command on Debian 10

By default, grep is installed on most of the system including Debian 10. If it is not installed, open up the terminal and issue the following command with root privileges.

apt-get install grep

When you are asked for a confirmation, press y and then enter from the keyboard. Wait for the installation to finish.

Install grep command

Since grep was already installed on my machine, check the above screenshot. Let us check its version by running the following command on the terminal.

grep --version

It should also return the version along with other detail as shown below.

Check grep command version

Using grep command

Once we have grep command available on our machine, we can play with it.

Searching a specific file or directory in your system

When you want to search or locate a specific file in your system, the syntax of the command should be as follows.

ls -l <pathofdirectorytosearch> | grep -i “file or directory name”

I want to search for a network directory in /etc/. The complete command should look like the following.

ls -l /etc/ | grep -i "network"

The ‘i’ option ignores case sensitivity. Therefore, it should treat network, Network or NETWORK as similar.

Below is the sample output.

Search for file names with grep

Suppose I want to search an “interfaces.d” file located at /etc/network/, you have to run the following command.

ls -l /etc/network/ | grep -i "interfaces.d"

Search for folder names with grep

It is necessary to include the word you want to find in double-quotes if it contains spaces. Suppose we are searching “network daemon”, the above command should look like the following.

ls -l /etc/network/ | grep -i “interfaces daemon”

Searching for a complete word with grep

You may have noticed, grep returned all sorts of results which include “network” e.g. networks, networked, networking or abcnetworking, etc. If you want to limit your search to include that specific word, you have to use -w option as follows.

ls -l /etc/ | grep -i -w network

Below is the sample output.

Searching for a complete word with grep

Searching a specific text in a file

We have a case when you have a large file and you want to search a specific text. The syntax of the command should be as follows.

grep – i “textyouwanttosearch” “filenameandpath”

Suppose I want to search the word “fox” in test.txt which is located inside my current directory. Run the following command on a terminal.

grep -i "fox" test.txt

Below is the sample output which returns only those lines of the file containing the word “fox”.

Search for text in files with grep

Performing a recursive search (Searching in multiple files)

If you want to search a text from a large number of files and subdirectories inside a  directory, you can perform recursive research by using -r option.

grep -i -r "fox"

Below is the sample output which shows the word fox is present in both test.txt and tree.txt files in the line shown.

Recursive search

You can also provide a directory path and it will search all files in that directory and its subdirectories.

Suppose I want to perform recursive research for the text “interfaces” in /etc/ and its subdirectories. The command should be executed as follows.

grep -i -r interfaces /etc/

Below is the sample output.

Example for an recursive search

Searching two different words with a single grep command

You can search two different words with a single egrep (which is a variation of grep) command as follows. Suppose I want to search a complete words fox and lazy in multiple files using -r option. You have to run the following command on the terminal.

egrep -w -r "fox|lazy"

Below is the sample output.

Searching two different words with a single grep command

Numbering the line which matches the text

Another useful option is the -n which number the lines matching the text. Following is the example illustrating how to use -n option.

grep -i -n "fox" test.txt

Below is the sample output which numbers the lines match the word “fox”.

Numbering the line which matches the text

Inverting the search

This is the opposite of what we have done above. If you want to return a text that does not include the word you specify, you can use the -v option.

Below is the example demonstrating the use of -v option.

grep -v -i "fox" test.txt

Below is the sample output.

Inverted grep search

All of the above options (-n etc) can also be applied with -v option.

Counting the matches

If you just want to count the number of matches against a specific text, you can use the -c option.

Let us count the word “fox” in test.txt located inside the current directory. Run the following command on the terminal.

grep -i -c fox test.txt

Below is the sample output after executing the above command which shows that the word fox has matched thrice in test.txt file.

Count matches with grep

Displaying the file names that match the specific text

If you want to find out the files which contain your specific word, you can use the -l option along with -r  as follows. Assuming all the files are located inside your current directory and the specific word you are searching or matching is ‘fox’.

grep -i -r -l fox

Below is the sample output which shows that the word fox is present inside test.txt and in a subdirectory and file asif.txt

Displaying the file names that match the specific text

Displaying only the matched text

By default, grep shows the entire line which matches your desired text or word. If you want grep to show you the matched words, you can use the -o option as follows.

grep -i -o fox test.txt

Below is the sample output.

Displaying only the matched text

Displaying the lines which start with a specific word(s)

If you want to retrieve all those lines which start with a specific word(s), you can use the ^ operator as follows.

Assuming you want to return all those lines which starts with “unix” and the file is log.txt located inside your current directory.

Run the following command on the terminal.

grep -i "^unix" log.txt

Below is the sample output.

Displaying the lines which start with a specific word(s)

Displaying the lines which end with the specific word(s)

If you want to return all those lines from a file that ends with the specific word(s), you can use the $ operator as follows. Assuming the word is “linux” and the file you want to search is rev.txt located inside your current directory.

Run the following command on the terminal.

grep -i "linux$" rev.txt

Below is the sample output.

Displaying the lines which end with the specific word(s)Conclusion

So this was my tutorial about the usage of grep command. I have demonstrated most of the grep options which are mostly used and you may require in your day to day job. There are some variations of grep command including zgrep etc. You can explore them on your own.