Serverspace Black Friday
JT
May 27, 2024
Updated October 2, 2024

How to use Grep in Linux?

AD

To work with Linux distributions, you mainly use the terminal or the console, which require knowledge of basic commands to fully work with the environment. For almost every task there is a utility that performs predefined functions. Searching for files and text patterns in them is no exception, the two main utilities that have gained popularity and are still in use today are find and grep. If find is used only to find the path of files by their properties, then grep in addition performs a search by their contents. In this article we will look at its syntax, main options and examples of its use in work!

What is GREP and how do I find it?

As mentioned above, GREP is a multifunctional Linux utility for searching files and text using patterns, regular expressions.

Regular expressions (regexp) is a mechanism for searching for information according to predefined patterns and rules.

In general, many distributions already have a package pre-installed with it. To check if it is available on your machine, enter the command:

grep
 Check installation
Screenshot №1 — Check installation

If the output looks like this then the package is already installed, otherwise install it via your package manager, for Ubuntu, Debian and apt-like:

apt update && apt upgrade && apt install grep

For RPM-like systems, the command to install a package with its dependencies might look like this:

dnf update && dnf upgrade && dnf install grep

After making sure that the package has been installed, let's look at the syntax of the utility and its options!

Syntax and options

The grep command itself is used for searching, as well as the basic options that form the syntax of the command:

grep [options] [pattern] [path to file]

For a simple pattern search in a file, we use the command:

grep 'named Alex' /home/kk.txt
Search in a file
Screenshot №2 — Search in a file

In a given file, the requested pattern was found and highlighted, in this case using the two basic syntax blocks [pattern] and [path to file] without [options]. But what if, we need to perform a more complex search with multiple conditions? There is a whole list of [options] options for that, let's look at the main ones and give an example of their use:

Template mapping configuration:
-E, --extended-regexp Extended regulars for templates;
-G, --basic-regexp Standard regulars for templates;
-P, --perl-regexp Use of regular-perl.

Template sources:
-e, --regexp=[pattern] allows you to specify a pattern directly;
-f, --file=[file] allows you to specify the pattern directly.

Case control:
-i, --ignore-case Case-insensitive match search;
--no-ignore-case Case-sensitive, works by default.

Special matching modes:
-w, --word-regexp Consider only whole words when matching;
-x, --line-regexp Only match whole strings.

Alternate string termination:
-z, --null-data Treat the end of a line as a null byte instead of a newline character.

All these groups of options refer to the configuration of the way to map a template to a data set. For a slightly more complex search than in the first example, we will use ordinary regular expressions. The other options will not be needed now. Let's specify the same template:

grep -e 'named Alex' /home/kk.txt
Full word search
Screenshot №3 — Full word search

There are two words Alex and Alex's parents in the text, the essence of the work of regxp's is that they break the data into characters and look for patterns. Therefore, when searching grep -e ‘named Alex’ /home/kk.txt there will be two matches Alex and Alexes parents. How to find a specific word or string so that other values are not hit? To do this, use the -w option, it will allow you to split sentences by words, not by characters in them:

grep -w -e 'Alex' /home/kk.txt
Filter with regxp
Screenshot №4 — Filter with regxp

As you can see in the example, the word Alexes was not highlighted because the breakdown was word-by-word.

Recursive grep search

But what if there is more than one file and you need to match multiple files in the same directory? For this purpose, grep has the -r option, which allows you to define a folder to search through its contents. Let's implement the query of our user name in the logs:

grep -r "ghn" /var/log
Search in logs
Screenshot №5 — Search in logs

All steps in the tutorial can be performed on powerful cloud servers. Serverspace provides isolated VPS / VDS servers for common and virtualize usage.

Create Server
Screenshot №6 — Create Server

It will take some time to deploy server capacity. After that you can connect in any of the convenient ways!

Filtering command output

Very often in Linux the output of commands in the terminal is huge or contains a lot of unnecessary information, for this we use pipe or redirect to the grep utility. It looks like this, suppose we need to check if the NetworkManager package for network management is installed:

dnf list || grep "NetworkManager"

If you are using the apt manager, replace dnf in the command:

apt list || grep "NetworkManager"
Pipe for filtration
Screenshot №7 — Pipe for filtration

Working with Linux distributions requires terminal skills and knowledge of basic commands. The find and grep utilities occupy an important place in the arsenal of tools, providing efficient search for files and their contents.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.