Grep stands for global regular expression printing. This is a useful command that is widely used by Linux system engineers when searching for strings or patterns in regular files and on the system.
In this article, I am going to demonstrate using grep with a lot of examples. I tested all the commands and examples on a computer with Debian 10.
necessary condition
You must have one Debian 10 machine with root privileges.
Install grep command in Debian 10
By default, grep is installed on most of the system, including Debian 10. If it is not installed, open a terminal and enter the following command with root privileges.
apt-get install grep
When asked to confirm, press y and then enter from the keyboard. Wait for the installation to complete.
Since grep has already been installed on my machine, check out the screenshot above. Let’s check its version by running the following command on the terminal.
grep --version
You should also return the version along with other details, as shown below.
Using grep
As soon as we have the grep command, we can play with it.
Search for a specific file or directory on your system
If you want to find or find a specific file on your system, the command syntax should be as follows.
ls -l
I want to find the network directory in / etc /. A complete team should look like this.
ls -l /etc/ | grep -i "network"
Option “i” ignores case sensitivity. Therefore, it should refer to a network, network or network as similar.
The following is an example output.
Suppose I want to find the “interfaces.d” file located in / etc / network /, you need to run the following command.
ls -l /etc/network/ | grep -i "interfaces.d"
You must include the word you want to find in double quotes if it contains spaces. Suppose we are looking for a “network daemon”, the above command should look like this.
ls -l / etc / network / | grep -i “Interface daemon”
Finding a full word with grep
You may have noticed that grep returns all kinds of results, including “network”, for example, networks, network, network or abcnetworking, etc. If you want to limit your search to that specific word, you should use the -w option as follows.
ls -l /etc/ | grep -i -w network
The following is an example output.
Search for specific text in a file
We have a case where you have a large file and you want to find specific text. The command syntax should be as follows.
grep – I am “text search” “file name and path”
Suppose I want to find the word “fox” in test.txt, which is inside my current directory. Run the following command on the terminal.
grep -i "fox" test.txt
The following is an example of output that returns only those lines of a file that contain the word “fox”.
Performing a recursive search (multiple file search)
If you want to search for text from a large number of files and subdirectories within a directory, you can perform a recursive exploration using the -r option.
grep -i -r "fox"
The following is an example output that shows that the word “fox” is present in the test.txt and tree.txt files in the line shown.
You can also specify the path to the directory, and it will search for all files in this directory and its subdirectories.
Suppose I want to do a recursive study of textual “interfaces” in / etc / and its subdirectories. The command should be executed as follows.
grep -i -r interfaces /etc/
The following is an example output.
Finding two different words with the same grep command
You can search for two different words with one egrep command (which is a variation of grep) as follows. Suppose I want to find the full words fox and lazy in multiple files using the -r option. You must run the following command on the terminal.
egrep -w -r "fox|lazy"
The following is an example output.
Numbering a line that matches the text
Another useful option is -n, which numbers the lines matching the text. The following is an example illustrating the use of the -n option.
grep -i -n "fox" test.txt
The following is an example output in which line numbers correspond to the word “fox”.
Invert Search
This is the opposite of what we have done above. If you want to return text that does not contain the word you have specified, you can use the -v option.
The following is an example demonstrating the use of the -v option.
grep -v -i "fox" test.txt
The following is an example output.
All of the above options (-n, etc.) can also be applied with the -v option.
Match count
If you just want to count the number of matches for a particular text, you can use the -c option.
Let’s 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
The following is an example of the output after executing the above command, which shows that the word “fox” occurs three times in the file test.txt.
Display file names matching specific text
If you want to know the files containing your specific word, you can use the -l option along with -r as follows. Assuming all the files are in your current directory, and the specific word you are looking for or match is “fox”.
grep -i -r -l fox
The following is an example output that shows that the word fox is present in test.txt, as well as in the subdirectory and asif.txt file.
Display only matching text
By default, grep displays the entire line that matches your desired text or word. If you want grep to show you matching words, you can use the -o option as follows.
grep -i -o fox test.txt
The following is an example output.
Display lines starting with a specific word
If you want to get all the lines that start with a specific word, you can use the ^ operator as follows.
Assuming you want to return all the lines that begin with “unix” and the log.txt file is inside your current directory.
Run the following command on the terminal.
grep -i "^unix" log.txt
The following is an example output.
Display strings that end with a specific word (s)
If you want to return all these lines from a file that ends with a specific word (s), you can use the $ operator as follows. The word linux and the file you want to find are supposed to be rev.txt, located inside your current directory.
Run the following command on the terminal.
grep -i "linux$" rev.txt
The following is an example output.
Conclusion
So this was my lesson about using the grep command. I demonstrated most of the grep options that are mostly used, and they may be needed in your daily work. There are several variations of the grep command, including zgrep, etc. You can learn them yourself.
How to use grep command in Debian 10