fbpx Mastering grep in Linux: 4 Practical Examples | Certificação Linux

Mastering the grep Command in Linux: 4 Practical Examples

📅 11/19/2021 ⏱ 6 min ✍️ Uira Ribeiro
commands
Mastering the grep Command in Linux: 4 Practical Examples
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The grep command on Linux is the best way to search for files and patterns through text lines within the operating system. Knowing how to manipulate this command is essential to facilitate the routine of users and professionals who work with Linux on a daily basis. 

But, when it comes to the grep command, many questions arise, such as: what is this control for? How to use it to solve everyday problems? And what are some practical examples of use? 

Thinking about helping you with these questions, we developed this complete article, with everything you need to know about the subject, including code line examples that you can use for various situations. Follow along!

What is the Grep command?

Grep, Global Expression Regular Print, in simple terms, refers to the command that searches for patterns or expressions within text files present in the system, according to the user's request. 

In more technical terms, grep can be considered an application that performs searches for specified strings (words), within all data content, through regular expressions (called regex). 

Syntax

The syntax of the Grep command is summarized as: 

  • grep [options] regex [files];
  • or also: command | grep [options] regex.

In this structure, it is important to identify what each segment means:

  • grep: the command;
  • options: the filters of the command;
  • regex (or pattern): what needs to be found;
  • files: the location where you want to find the searched term. 

Through these command syntaxes, grep reads all lines of the files and, when it finds the information corresponding to the regular expression, it selects it and "prints" or displays it on the screen as a result. 

Within each command, there are countless options, each with a specific purpose, in order to filter searches for information.

To illustrate, be aware that there are options that display the number of lines, perform recursive searches, or even invert the search mode, showing only the lines of files that do not match the searched pattern. 

And what is it used for?

Like all Linux functionalities, the Grep command serves to facilitate user searches and filter information in the best possible way. 

Thus, even in huge text files, with many lines, expressions and data, it is possible to find only the desired expression. 

The opposite is also true: if a term repeats a lot, it is possible to do a filter, in order to return only the different results from it, through commands like -v (minus symbol and letter V), responsible for this function of returning information that does not match the regex. 

But don't worry: we will discuss each of the main commands below, so that you can better understand how to use them in your daily life, in order to optimize time, processes and resources. 

And how to use the Grep command on Linux?

grep how it works
Photo: Pexels | How to use this function?

You can use the Grep command on Linux whenever you want and need to identify a specific term in some file of your operating system.

If you want to identify, for example, all the places where your name appears, grep can make this possible. 

Or, in a file that needs modification, for example, the programmer or developer could apply the filter of the searched name or term, in addition to activating the option that shows the line number where the term appears. 

To better understand the varieties of options present in the Linux system, just open the command terminal and search for "grep -help". This way, all available options and explanations will be shown. 

This simple search will serve as a kind of glossary, so that you can learn about all the options available in the grep system on Linux.

Examples of using the Grep command

grep command
Photo: Freepik | Understanding Grep is totally useful. 

Now that you know how Grep works, and how it can be applied, let's connect theoretical knowledge to practical, through interesting examples of using the Grep command. 

You can reproduce the examples discussed here on your own Linux terminal, for study and testing purposes. This way, it will be much easier to memorize the commands and truly understand them. 

Search for word in text file

To identify a specific word in a text file, just reproduce the grep command including the desired word and the location where the file is. 

Here, as an example, we will use the word "command" in the location "grep":

<code>grep command grep</code>

Search for word ignoring uppercase and lowercase letters

To ensure that both words starting with uppercase and lowercase letters can be found, just include the "-i" option (without quotes), like the template:

<code>grep -i solicitation file</code>

Following the example above, of the word "command", the structure would be like this:

<code>grep -i command grep</code>

This way, all lines that contain the word "command", regardless of whether they are written in uppercase or lowercase letters, will be shown in a different color, to highlight. 

Word counter

If you want to find out how many times the desired word or term appears in the text files present in the chosen directory of your operating system, just activate the -c command. 

With it, you can check the accounting of the searched words in a simple and effective way. Just follow the structure below:

<code>grep -c solicitation file</code>

Search for word in multiple files 

In cases of broader searches, where you need to consider where the searched word appears, regardless of the file location, just include the -l option, as shown in the structure example below:

<code>grep -l solicitation ./*</code>

These are just some examples of how grep options and commands can be customized according to your file search and identification needs. 

There is an infinity of content waiting for you, to make your routine with Linux system handling simpler and more efficient. 

Learn Linux with the most complete Teaching Platform on the market!

Conclusion

Did you see how useful the grep command is? Like this command, there are several other utilities that you only discover when you start studying the peculiarities of the Linux system. 


If you want to learn more, the best option is to browse our Linux Certification course platform. You can choose the course that best meets your current needs and have the guarantee of learning with the most experienced professor in the field!

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp