The grep is a powerful and one of the most widely used commands on Linux based operating systems. If you want to find strings within any kind of file in your system then grep is convenient to find it for you.
The grep stands for “Global regular expression print” is extremely to find any kind of regex within the file. Generally grep is used to match patterns within a text file.
For example, if you pass grep a word to search for, it will print out every line in the file containing that word.
You can easily fetch useful information by specifying particular search criteria through the grep command.
In this tutorial, we will explain how to use grep command on Linux with some practical examples.
Install grep
By default, grep is available in the most linux-based operating systems. If you do not have it installed, you can install it by just running the following command:
apt-get install grep -y
Once installed, you can check the version of grep with the following command:
grep --version
You should get the following output:
grep (GNU grep) 3.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Basic Usage
grep is used to match patterns within a text file. For example, lets use a grep to search a word virtual in the file located at /etc/apache2/sites-available/000-default.conf
grep -i virtual /etc/apache2/sites-available/000-default.conf
You should see every word that contains virtual (with upper, lower, or mixed cases) in the following output:
Note: -i option is used to ignore “case” of our search parameter
Next, lets search a word virtual in lower-case in the file /etc/apache2/sites-available/000-default.conf.
grep virtual /etc/apache2/sites-available/000-default.conf
You should see the following output:
If you want to find all the lines that do not contain a word virtual, you can use a grep command with -v option:
grep -v virtual /etc/apache2/sites-available/000-default.conf
You should see the following output:
You can also use -i with -v to ignore the case as shown below:
grep -iv virtual /etc/apache2/sites-available/000-default.conf
Output:
You can find the number of matching patterns appears in the file 000-default.conf with the following command:
grep -ic virtual /etc/apache2/sites-available/000-default.conf
Output:
6
Search a string Recursively in all Directories
If you want to search a string for specified directory and all of the subdirectories, then you can use grep with -R option.
For example, search a word virtual in the directory /etc/apache2/sites-available with all of the subdirectories:
grep -R "virtual" /etc/apache2/sites-available
You should see all the files that contains word virtual:
You can also use -n option with grep to precede each line of output with the number of the line in the text file from which it was obtained:
grep -R -n virtual /etc/apache2/sites-available
You should get the following output:
Use grep command with pipes
You can use grep command with pipes to filter the output.
For example, find the wireless network interface of your system with the following command:
ifconfig | grep wlan0
You should see the following output:
If you want to find the Apache packages installed on your system, run the following command:
dpkg -l | grep apache
You should see the following list:
Find Multiple Words
You can also search multiple words within a specified file using the egrep command:
Run the following command to search word virtual and server in the file /etc/apache2/sites-available/000-default.conf:
egrep -w 'virtual|server' /etc/apache2/sites-available/000-default.conf
You should see the following output:
Use grep when Matching any Character
You can also use the period character “.” to search and produce output when any single character can exist at the specified location.
For example, if you want to search a word that has two characters and then the string “rtual”, run the following command:
grep "..rtual" /etc/apache2/sites-available/000-default.conf
Output:
Use grep to Find Lines Begins with a Capital Letter
You can use grep with ^ option to find lines that begin with a capital letter in a specified file:
For example, find all the lines in the file /etc/fstab that start with capital letter with the following command:
grep "^[A-Z]" /etc/fstab
Output:
For more information about grep syntax and usage, you can run the following command:
grep --help