Sometimes you may need to find a file that contains a specific string or find a line in the file where there is a word. In Linux, all this is done using one very simple yet powerful utility – grep. With it you can search not only the time limits in the files, but also filter the output of commands, and much more.
In this tutorial, we’ll look at how text is searched for in linux files, detail the possible options for grep, and also give some examples of working with this utility.
What is grep?
The Grep command, (stands for global regular expression print) is one of the most popular commands in the Linux terminal that is part of the GNU project. First of all, because it is a very powerful utility that allows users to sort and filter text based on complex rules.
This allows you to apply it to solve various problems. The grep utility is mainly used to find strings matching a string in the text or the contents of files. It can also be used to search by pattern or regular expressions. With it it is convenient in a matter of seconds to find a file in the file system with the correct line, find the text in the file, or filter only a couple of necessary lines from the output of the command. Now let’s look at how to use the command.
The command syntax is as follows:
$ grep [options] template [filename …]
- Options are additional parameters that specify various settings, search and output, such as the number of rows or the inversion mode.
- A template is any string or regular expression that will be searched
- The file and the command are the place where the search will be conducted. As you will see further, grep allows you to search in several files and even in a directory using recursive mode.
As you can see, grep can not only search in linux files, but it can also filter standard output, this is a very convenient function when you need to select only errors from logs or find the process PID in the numerous output of the ps utility.
Let’s look at the most basic options of the utility, which will help you more efficiently search for text in grep files:
- -b- show the block number before the line;
- -c- count the number of occurrences of the pattern;
- -h- do not output the file name in the search results inside Linux files;
- -i- not case sensitive;
- -l- display only the names of the files in which the template was found;
- -n- show the line number in the file;
- -s- do not show error messages;
- -v- invert the search, output all the lines except those that contain the template;
- -w- search for a pattern, as a word surrounded by spaces;
- -e- use regular expressions when searching;
- -An- show the occurrence and n lines before it;
- -Bn- show the occurrence and n lines after it;
- -Cn- show n lines before and after occurrence;
All the most basic options have been considered, and even more, now let’s move to the examples of the grep linux command.
Examples of using
With the theory over, now let’s move on to practice. Consider a few basic examples of searching within Linux files with grep, which you might need in your daily life.
Search for text in files
In the first example, we will look for the User user in the Linux password file. To search for grep text in the /etc/passwd file, enter the following command:
grep User /etc/passwd
As a result, you will get something like this, if, of course, there is such a user:
And now we will not take into account the register during the search. Then the combinations ABC, abc and Abc from the point of view of the program will be the same:
grep -i «user» /etc/passwd
Print multiple lines
For example, we want to select all the errors from the log file, but we know that in the next post after the error can contain useful information, then with the help of grep we will display a few lines, we will look for errors in Xorg.log using the pattern “EE”:
grep -A4 «EE» /var/log/xorg.0.log
Will print a string with an occurrence and 4 lines after it.
grep -B4 «EE» /var/log/xorg.0.log
Output the target string and 4 lines before it
grep -C2 «EE» /var/log/xorg.0.log
Output two lines at the top and bottom of the entry.
Regular expressions in grep
Regular expressions grep – a very powerful tool at times expanding the ability to search for text in files grep. To activate this mode, use the -e option. Let’s consider some examples:
Search for an entry at the beginning of a line with the help of the special character “^”, for example, we will display all messages for November:
grep «^Nov 10» messages.1
Nov 10 01:12:55 gs123 ntpd: time reset +0.177479 s
Nov 10 01:17:17 gs123 ntpd: synchronized to LOCAL(0), stratum 10
Search at the end of the line, special character “$”:
grep «terminating.$» messages
Jul 12 17:01:09 cloneme kernel: Kernel log daemon terminating.
Oct 28 06:29:54 cloneme kernel: Kernel log daemon terminating.
Find all the lines that contain the numbers:
grep «[0-9]» /var/log/Xorg.0.log
In general, regular expressions grep is a very extensive topic, in this article I just showed a few examples to let you know what it is. As you saw, thus, the search for text in grep files becomes even more flexible. But a full explanation of this topic requires a whole article, so for now let’s skip them and go further.
Recursive use of grep
If you need to search for grep text in several files located in the same directory or sub directories, for example, in Apache configuration files – /etc/apache2/ – use recursive search. To enable recursive search in grep, there is the -r option. The following command will look for text in Linux files in all the subdirectories /etc/apache2 for the occurrence of the string mydomain.com:
grep -r «mydomain.com» /etc/apache2/
In the output you will get:
grep -r «zendsite» /etc/apache2/
/etc/apache2/vhosts.d/zendsite_vhost.conf: ServerName zendsite.localhost
/etc/apache2/vhosts.d/zendsite_vhost.conf: DocumentRoot /var/www/localhost/htdocs/zendsite
/etc/apache2/vhosts.d/zendsite_vhost.conf: <Directory /var/www/localhost/htdocs/zendsite>
Here, before the found line, the name of the file in which it was found is specified. The output of the file name can be easily turned off with the -h option:
grep -h -r «zendsite» /etc/apache2/
Word search in grep
When you search for the string abc, grep will also output kbabc, abc123, aafrabc32, and similar combinations. You can force grep to search for the contents of files in linux only those lines that turn off the search words with the -w option:
grep -w «abc» filename
Search for two words
You can search the contents of the file for more than one word, but several. To search for two different words, use the egrep command:
egrep -w ‘word1|word2’ /path/to/file
Number of occurrences of a string
The Grep utility can tell you how many times a specific string was found in each file. To do this, use the -c (counter) option:
grep -c ‘word’ /path/to/file
With the -n option, you can output the line number in which the entry was found, for example:
grep -n ‘root’ /etc/passwd
Inverted search in grep
The grep linux command can be used to find strings in a Linux file that do not contain the specified word. For example, output only those lines that do not contain the word pairs:
grep -v пар /path/to/file
Output file name
You can specify grep to display only the file name in the cat, the specified word was found using the -l option. For example, the following command will output all file names, when searching for the contents of which the primary entry was found:
grep -l ‘primary’ *.c
Colored output in grep
Also, you can force the program to highlight other occurrences of the output in the output:
grep —color root /etc/passwd
It turns out:
That’s all. We’ve covered everything about using the grep command to search and filter the output of commands in the Linux operating system. With proper application, this utility will become a powerful tool in your hands. If you have any questions, write in the comments!