How to use GREP Command in Linux?

By | March 18, 2022
GREP Command in Linux

The Linux GREP command is often used to return lines and files that perfectly match a pattern or a regular expression. You can also use the grep command along with the output of some other command by pipelining it. GREP abbreviates for General Regular Expression Print and is a very handy command or a tool for pattern matching in large logs of files. This tool is pre-installed in all Linux distributions.

You might think, why exactly do I need to search for patterns in Linux systems. It is extremely helpful in a plethora of ways. Suppose who has created a file ages ago and slightly remembers the file contents but doesn’t know where it resides. You can search for the string to match it with a set of files in your system, and it will tell you where the file exists in the system.

To move ahead with this guide, please ensure that you have access to Linux Machine, a Command-Shell, and sudo privileges as well. Please take utmost care while working with configuration files when you are logged in as a root user.

Syntax of the GREP Command

The GREP command follows the below syntax:

$ grep [options] [pattern] [file-name]

The pattern can be a simple string or a complex regular expression as well. You can include multiple files or even a single file or directory. It comes along with a plethora of options that you might want to include to modify your output result. To look at a detailed description of all the options, you can use the help flag and the command.

$ grep --help

 $ grep --help

Please note that the grep command is, by default, case-sensitive. But you can use the -i option to perform a case-insensitive search on the mentioned files.

Vamware

Searching a simple file

Suppose we want to search a string in a simple file using the grep command. The following command does the same.

$ grep article ~/Documents/message.txt

$ grep article ~/Documents/message.txt

Vamware

You can see that the command checks for the complete words or even substrings that match with the pattern and print the line that contains the pattern. Please note that a line does not mean what we see as in the terminal. A line might be a paragraph as well, and it holds until it encounters a line break where it terminates.

One more point to note is that if your search pattern or string consists of special characters such as inverted commas, spaces, slashes, alphanumerics, etc., you must use either backslash to escape them or put them in between inverted commas. Let’s try to use the case-insensitive (-i) option to search for a pattern irrespective of the case.

$ grep -i article ~/Documents/message.txt

 $ grep -i article ~/Documents/message.txt

Displaying Non-Matching Lines

In case we want to display those lines that do not match with the given pattern, we can use the -v option which stands for invert.

$ grep -iv article ~/Documents/message.txt

$ grep -iv article ~/Documents/message.txt

Vamware

Searching Multiple Files

You can also specify a list of files instead of a single file to perform a grep search on. You just need to mention the paths of all the files separated by spaces.

$ grep -i Article ~/Documents/message1.txt ~/Documents/message2.txt

$ grep -i Article ~:Documents:message1.txt ~:Documents:message2.txt

You can observe that the command will display all the matching lines along with the path of the file that it finds a match with.

Searching every file in a directory

If you want to perform the grep search on all the files in a directory, you can do it in the following way. Instead of the name of all the files, just include an * at the end.

$ grep -i article ~/Documents/*

 $ grep -i article ~/Documents/*

Searching Whole Words

If you want to return the search result only if the pattern matches as a complete word, then you can use the -w option. By default, the grep command searches for the substrings as well to find a match. With this option, it just performs a complete word-to-word match.

$ grep -w article ~/Documents/*

$ grep -w article ~:Documents:*

Searching in Sub-Directories

If you want to search the files along with subdirectories inside a directory for a pattern match, you can use the -r (recursive) option. This option recursively traverses files and sub-directories until it traverses all the contents inside a directory and returns the result.

$ grep -ri article ~/Documents/

$ grep -ri article ~/Documents/

You can see that it searches for the patterns inside the message1 text file, which is just inside the documents folder, as well as the message2 text file, which is inside a subdirectory called a folder.

Lines with Perfect Match

By default, the grep command will return lines that will contain a substring that matches the pattern. If you simply wish to return only those lines that match completely with the pattern, you can use the -x option.

$ grep -ri "Happy Learning!" ~/Documents/

 $ grep -ri "Happy Learning!" ~/Documents/

You can see that the above command has returned only those lines that match exactly with the pattern. Also, please note that if there are any special characters in your pattern, do not forget to escape them or use inverted commas.

Listing the File Names

If you want to display a list of all the files that contain a match with the pattern, you can use the -l option.

$ grep -irl article ~/Documents/*

 $ grep -irl article ~/Documents/*

You can see that the command has displayed the paths of all the files that have an exact match with the pattern.

Counting the number of matches

If you want to return the number of times that the pattern has found a match in the files, you can use the -c option.

$ grep -irc article ~/Documents/*

 $ grep -irc article ~/Documents/*

Wrapping Up!

In this detailed and comprehensive guide, we discussed the basics of one of the most popular and commonly used pattern-matching commands in Linux – the GREP command. We started with a fundamental understanding of the working of the grep command along with its syntax.

We then moved on to discuss several examples and use-cases of the grep command such as searching for patterns in a simple file or multiple files, displaying all the lines that do not match with the pattern using the -v option, using the case-insensitive -i option, searching for complete words and lines, counting the number of matches and listing the files that have a match with the pattern, etc.

We hope that with the help of all the examples that we walked you through, you now have a deeper understanding of how the command works and the different options that can be used to filter out the results.

People are also reading:

Author: Maaz Bin Asad

Maaz is a freelance content writer and an engineering undergraduate who is experienced working in the software development industry. He likes solving algorithmic problems and finding new approaches to optimize given problems.

Leave a Reply

Your email address will not be published.