The Grep command in Linux

For anyone getting started with Linux, learning how to effectively navigate through the system is key. One of the most versatile and powerful tools at your disposal is the ‘grep’ command. The ‘grep’ command in Linux is a command-line utility used to search text or files for lines that contain a match to a specified pattern. It’s an essential tool for any Linux user, no matter their level of expertise.

In this blog post, we will delve into the practical use of the ‘grep’ command and explore some of its numerous functionalities.

The Grep command in Linux

The term ‘grep’ stands for ‘Global Regular Expression Print’. As the name implies, ‘grep’ is used to search text or files for lines that match a specific pattern. Since its inception in the early Unix systems, ‘grep’ has been celebrated for its efficiency and precision in searching and filtering textual data.

Whether you’re searching for a specific error in a log file, finding all instances of a specific word in a text, or even looking through the system processes, ‘grep’ proves to be an indispensable tool. Its power comes from its simplicity for basic usage, while still offering a deep set of features and options for advanced users.

Understanding and mastering ‘grep’ is an important step in gaining proficiency in Linux or Unix-like systems. With ‘grep’, you hold the capability of easily sifting through vast amounts of data, an ability that empowers you to find exactly what you’re looking for, and perhaps even discover things you didn’t know you were looking for. In essence, ‘grep’ is a lot more than just a command; it’s a fundamental part of the Linux user’s toolkit.

 According to the GNU website

“grep prints lines that contain a match for one or more patterns.”

Also, they add in the documentation:

“When it finds a match in a line, it copies the line to standard output (by default), or produces whatever other sort of output you have requested with options.”

Therefore grep has become a useful tool for sysadmin. It is also easy to use but quite powerful and flexible in its search criteria.

To complement the tutorial and make it easier to understand, common examples of the use of the grep command will be added.

How to use the grep command in Linux

The basic syntax of the command is as follows

grep [options] pattern [FILE]

Where grep is the command in question; Options refers to the possible options we can add; pattern is the string we want to search and finally we specify the file where we want to search.

If you want to get the help module of the command just execute it:

grep --help

Output:

Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE.
Example: grep -i 'hello world' menu.h main.c

Pattern selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression
-F, --fixed-strings PATTERN is a set of newline-separated strings
-G, --basic-regexp PATTERN is a basic regular expression (default)
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN for matching
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline

Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit

Output control:
-m, --max-count=NUM stop after NUM selected lines
-b, --byte-offset print the byte offset with output lines
.
.
.

As you can see there are many options that give us grep, but the most important and common are

  • -i: Of the most useful options because it allows us to ignore between capital and small letters.
  • -c: It will only show us the number of lines that match with what we have asked to search.
  • -r: This option enables the recursive option of the search.
  • -n: print line number with output lines.
  • -v: select non-matching lines.

So let’s start with the examples.

Using the grep command

1.- Find a word in a text file

One of the most common and easiest uses of grep is to find a given word in a text file. For example, suppose we have a text file called file.txt and we want to search for the word "nulla", to do so, run the following command:

grep nulla file.txt
Using the grep command
Using the grep command

It’s that simple. As you can see, the text of the test file is shown, and the matches are highlighted.

On the other hand, If the command does not return any output it is because it does not appear in the file. You can also, if the file is in another location, define the absolute path.

2.- Searching without taking into account upper and lower case letters

The text files, although not very long, tend to have many words. Some of these may be capitalized, and this may affect the search results. A special emphasis should be placed on configuration files where there are variables. So in this case, it is best to use the -i option, which tells grep to ignore these distinctions. For example:

grep -i ex file.txt
Perform the search without taking into account upper and lower case letters
Perform the search without taking into account upper and lower case letters

3.- Count how many times the search word appears

Now if you want to know how many times the word you want to search is found, you only need to use the -c option

grep -c nulla file.txt

Sample output

6

This option of the grep command is very useful because you can quickly evaluate the file and use it effectively in scripts and configurations.

4.- Using the grep command to search several words

And, what if I want to search for several words? Well, we can do it with the help of a pipe. In this case we will search the file for the words nulla and ex

grep 'nulla\|ex' file.txt
Searching for multiple words with Grep
Searching for multiple words with Grep

As you can see, it is quite simple, and its operation is even simpler. First, the grep command searches for the first keyword and then for the second keyword continuously.

5.- Searching for a word in several files

The Grep command is quite powerful and also allows you to search several files simultaneously.For example

grep ex file1.txt file2.txt
Multiple words searching
Multiple words searching

Similarly, you may notice that the grep command does its job efficiently even on multiple files.

Remember that you can also specify the absolute path to these files.

6.- Using grep with other commands

You can combine the grep command with other Linux commands to perform deeper and smarter searches. For example, you can use it with ls and search for a folder or file with the search criteria. In this case, I will search for the pix command

ls -l | grep pix

Sample output:

drwxrwxr-x 14 user user 4096 Dec 25 2022 pix

This will display all files and folders that you start or that contain images. This same formula can be used with any command.

7.- Recursive search in a directory

Occasionally it is not possible to remember the exact location of a file. However, the `-r` option of the grep command allows you to do a recursive search within the files in a directory.

grep -r nulla

Note that in this case, you do not specify a specific file, since the program will do it on every file in the current directory.

8.- Make output more readable using highlighting

In some Linux distributions, the output of the grep command is completely basic, with no highlighting. This can make it somewhat confusing.

To solve this, there is the `–color` option, which makes it more readable, and, above all, easier to understand.

grep config file.txt --color

So grep is quite useful.

Conclusion

There are quite useful commands that can help with the heavy lifting in Linux. One of them is grep because it allows you to search inside a text file or even combine it with another command to perform a unique filtering. This is appreciated by sysadmin and all those who work with configuration files and the terminal on a recurring basis

Now we want to hear from you. Have you used grep? are you still using it? leave us a comment and share this post with your friends.

Scroll to Top