The grep Command in Linux: Explore 10 Interesting Use Cases

Try this guide with our instant dedicated server for as low as 40 Euros

grep linux

How do you find a phrase in hundreds of files?

If you are smart, you will find a way to read the contents of all those files and see all the instances of the phrase. Automating the process and extending the idea of finding the phrase is a better approach to the problem.

Fortunately, *nix-based systems offer grep, a powerful utility that you can use to find out specific patterns in the files and directories.

This article will cover the basics of the grep utility and go into several important use cases where you can use grep with an option to accomplish specific tasks.

Let’s start with a short introduction to the grep command in Linux.

Table Of Content

  1. The grep Command in Linux
  2. Prerequisites to Using grep
  3. Using the grep Command in Linux
    1. Find a String in the File
    2. Force Highlighting the Instances of the Search String
    3. Search All Directories for a String
    4. Count the Number of Instances of the Search String
    5. Find the Lines that DO NOT Contain the Search Term
    6. Print the Line Numbers of the Lines that Contain the Search Term
    7. Find Specific Words in the File
    8. Use grep with Pipes
    9. Limit the Number of Lines Displayed Before OR After a Search Pattern
  4. Getting Help With the grep Command
  5. Conclusion
  6. FAQ’s

The grep Command in Linux

grep is a popular command line tool for looking up patterns in files and directories. You can use this effective tool to search a file for particular words, phrases, or patterns. The grep command is available on macOS, most Unix-like systems, and almost all popular Linux distros.

The grep command in Linux stands for “global regular expression print.” It searches for exact or partial patterns that appear in the selected files and directories. If found, the utility prints them to the standard output. grep can search for patterns in a single file, multiple files, or directories.

Prerequisites to Using grep

Before you start using grep, you need to fulfill the following requirements:

  • Most Linux distributions frequently include grep as a command-line tool. So, we prefer you have Linux on your machine.
  • You need a shell or terminal to run the grep utility. While you can use the standard terminal emulator, such as GNOME Terminal, KDE Konsole, or Xterm, you can opt for a custom terminal or shell.
  • Alternatively, you need access to the command line interface. For this, you can launch a terminal window or a virtual console.
  • Finally, you must know how to explore directories, list files, and run commands. Basic knowledge of the Linux command line is necessary to use grep efficiently.

Once you’ve satisfied these prerequisites, you can use the grep tool to look for patterns within files and directories. You can count on the incredible flexibility of the grep command in Linux to perform various text search and modification operations.

Using the grep Command in Linux

We’ll create a sample file with the title redswitches.txt to demonstrate the capabilities of the grep command in Linux environment. Once you’ve created the file, add some text and save it.

Using grep Command in Linux

Now, we’ll demonstrate a few grep commands and modify the output to obtain the desired outcomes.

Find a String in the File

We’ll start with the simplest operations – finding a string in the file.

Run the following command to look for the string “Redswitches” in the sample file.

$ grep "Redswitches" redswitches.txt

As you can see, grep found the string “Redswitches” twice and highlighted all instances.

Find string in grep Command in Linux

If the file is located at a different path, you need to include the full path of the file with the grep command. Consider the following example:

$ grep "string" /path/to/file

Force Highlighting the Instances of the Search String

Some terminals don’t highlight the instances of the search string by default. In such cases, the grep utility may appear not to be working correctly.

If that’s the case with your terminal, you can use the –color option to make the instances stand out in a distinct color. Here’s how to use this option on the sample file:

$ grep --color "hosting" redswitches.txt

Here’s the output of the command:

Search string grep Command in Linux

Search All Directories for a String

You can scale the search process to cover all the directories of the system. For this, grep offers the -r option. This option forces the grep command to start with the current subdirectory and then continue the process with all the other subdirectories.

Here’s the sample command:

$ grep -r "hosting"*

As you can see in the following screenshot, the grep command in Linux terminal started with the current subdirectory and continued throughout the subdirectories and files.

Search directories grep Command in Linux

Count the Number of Instances of the Search String

If you want just to count the number of instances the search term occurs in the files, you can simply use the -c option to display the count instead of the actual occurrences of the term.

$ grep -c "hosting" redswitches.txt

In the following screenshot, you can see that instead of the highlighted location of the search, the grep command displays the count of the occurrences.Count number of instances of grep Command in Linux

Find the Lines that DO NOT Contain the Search Term

There are scenarios where you want the parts of content that don’t contain the search term. This is a pretty common requirement where you want to see the data excluding specific patterns.

We’ll use the -v option with the grep command on our sample file to demonstrate this.

$ grep -v "Redswitches" redswitches.txt

As you can see in the following screenshot, grep prints the lines without the search pattern we mentioned in the command.

Search term grep Command in Linux

If you want the line numbers alongside the search occurrences, you can use the -n option to print line numbers along with the lines.
Let’s use this option with our sample file to find the line numbers and the instances of a search term.

$ grep -n "hosting" redswitches.txt

As you can see, the grep command printed line numbers alongside the occurrence of the search term.

Print line number grep Command in Linux

Find Specific Words in the File

The grep command in Linux is a great tool for finding specific words (or letter patterns) in a file or directory. For this, we’ll use the -w option with the command. Note that the command can find patterns in the middle of the individual words.

$ grep -w "hosting" redswitches.txt

The following screenshot demonstrates that the command highlighted all the instances of the complete word in the sample file.

Find specific term grep Command in Linux

However, if you attempt to use the -w option to search a partial word with the following command:

$ grep -w "RED" redswitches.txt

You’ll find that the command returns nothing because the sample file doesn’t contain any instance of the search terms as an independent word.

Use grep with Pipes

The grep command in Linux is incredibly flexible and can work with other interesting features to speed up workflows. A simple way of demonstrating this is to use pipes to connect grep with other commands.
In this combination, you can use the search and pattern-finding capabilities of the grep command to search through the output of the other commands.

Here’s an example:
You can use the following command to find out if a specific package is available on an Ubuntu machine.

$ dpkg -L | grep "package-name"

Limit the Number of Lines Displayed Before OR After a Search Pattern

You can control the output volume of a command by using the -A or -B options with the grep command. With these options, you can mention the search term that would be the starting or ending point of the output.

Here’s the first example:

$ ifconfig | grep -A 4 eno1

As you can see in the following screenshot, the output starts at the search term because of the -A option.

Limit number in grep Command in Linux

On the other hand, check out the following example of the -B switch.

$ ifconfig | grep -B 4 ether
You can see that the output contains the line with the search string and the three lines preceding it.

$ ifconfig | grep -B 4 ether

Getting Help With the grep Command

If you need detailed help with the options and switches available for the grep command, you can run the command with the –help option.

$ grep --help

As you can see, the command prints the available options along with a brief description.

Get help with grep Command in Linux

Conclusion

The grep command in Linux is a feature-rich and flexible search tool that enables users to look for specific strings and patterns in files, directories, and text streams. It has an extensive feature set, including pattern matching, filtering, and command integration. As a result, it has become an essential tool for programmers, system administrators, and power users. Becoming proficient with grep’s syntax and options increases your productivity and efficiency when you work with text on your Linux or Mac machine.

FAQ’s

Q: Can grep look for patterns in binary files?

A: The grep command in Linux can look for patterns in binary and text files. The command usually behaves similarly for all file formats because it can be forced to consider all files as text files with the -a option.

Q: How can I have a grep search without considering the case?

A: You may search without taking the case using the -i option. When matching patterns, this option instructs grep to disregard the case.

Q: Can grep simultaneously search for patterns across several files?

A: grep can look for patterns across multiple files at once. For this to work, you need to specify the required files or indicate a group of files using wildcards.

Q: How can I find patterns in compressed files using grep?

A: The grep command in Linux can look for patterns in compressed files like gzip or zip archives. To handle compressed files transparently, use the -z option.

Q: Can grep look for patterns in the command output, or is it restricted to searching within files?

A: grep has the ability to look for patterns in command and file output. You can feed the output of a command to grep for additional processing and pattern matching by using pipe (|) and redirecting output.

Try this guide with our instant dedicated server for as low as 40 Euros