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.
You need to have a single Debian 10 machine with root privileges.
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.
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.
Once we have grep command available on our machine, we can play with it.
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.
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"
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"
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.
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".
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.
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.
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.
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".
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.
All of the above options (-n etc) can also be applied with -v option.
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.
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
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.
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.
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.
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.
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…