How to Use the Grep Command + Useful Grep Examples

OS-based servers generally do not have a graphical interface for security reasons. In that case, it is very important to know how to manipulate the terminal and some , like grep.

A very useful operation that we can perform with the terminal is to search within a text file. Especially if we work with service configuration files such as NTP.

In this tutorial, we will teach you how to use the grep command in Linux (Unix) and reinforce the learning with useful examples for your daily work.

How to use the grep command in Linux?

The grep command belonging to the Unix family is one of the most versatile and useful tools available. This looks for a pattern that we define in a text file. In other words, with grep on Linux you can search for a word or pattern and the line or lines containing it will be printed.

At first glance, this may seem like an unhelpful command, however system administrators managing many services with multiple configuration files use it to query or search for specific lines within those files.

First, connect to the VPS using SSH. Here’s a showing how to do it using putty ssh.

ssh your-user@your-server

If you are running Linux on your computer, just open the terminal.

The syntax of the grep command when searching for a single file is as follows:

grep-pattern

  • grep: the command statement
  • : command modifiers
  • pattern: the pattern we want to find with the search
  • : the file you are searching on

You can see the documentation and explanations of various options by running this command on the command line:

See also  What is a domain? - Explanatory video

grep –help

As you can see, the command offers us many possibilities. However, the most important and common options are:

  • -Yo: Search will not be case sensitive. That is, if you want to search for the word “auto” it will be the same as “AUTO”
  • -c: will only show the number of lines that match the searched pattern
  • -r: enable recursive search in the current directory
  • -n: finds lines and precedes each matching line with a line number.
  • -v: with this option, we are shown the lines that do not match the pattern we have searched for

Useful examples of the grep command in Linux

Let’s see some practical examples of the grep command in Linux.

Find a word in a text file

To search for a word in a text file, simply type the command:

grep search file

  • search: the word you are looking for
  • File, Archive: the file in which you are looking for the word

In our case, we are looking for the word command in a file called grep:

grep command grep

The result highlights the lines that match the query like so:

Find a word regardless of case

To do this, you need to add the option -Yo.

grep -i search file

That’s it, simple as that!

Count of words that match the search

Using the grep command you can find out how many times a word is used in the text file. Just add the option -c.

grep -c search file

Search multiple keywords

So far we have seen examples where we search for a single word. Grep supports multiple queries in a single command. The command would look like this:

See also  Top 8 PHP Frameworks For Web Developers

grep search1 file | grep search2 file

The command works very simply. First, we search Search1 and then we pass after the slash to a second grep command for the second word: Search2.

Find a word in a set of files

It is also possible to search across multiple files with a single command:

grep -l word_to_find ./*

In the terminal, the files that contain the word you searched for will be displayed.

conclusion

The grep command in Linux can make your job much easier if you’re dealing with a lot of text files. That is why grep is considered a very versatile command with many possibilities of use.

In this tutorial you have learned what are the most common functions of the grep command in Linux. On the other hand, we recommend you consult the official documentation to expand your knowledge.

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...