Using grep, you can quickly find text matching a regular expression in a single file, a group of files, or text coming from stdin.

black and gray compass on top of map
Photo by Alexander Andrews on Unsplash

Searching for patterns of text in files or text streams is one of the most common tasks you’ll perform in your sysadmin career. This is a valuable skill that allows you to check a variety of system configurations, analyze data, troubleshoot logs, and perform many other activities.

The most common way to find text in a Linux system is using the command-line utility grep. This utility was originally developed for the Unix operating system in the early 1970s. Grep evolved over the years, and the most common version available today for Linux, GNU grep, has additional features such as colored output. However, its main functionality is still the same.

Using grep, you can quickly find text matching a regular expression in a single file, a group of files, or text coming from stdin using the shell pipe operator.

This article covers how to use the grep command to find text.

Find text in a file

The most basic way to use grep is searching for text in a single file. To do this, type grep followed by the text pattern to search for and the file name to search in. For example, to find which port the Secure Shell (SSH) daemon uses, search for Port in file /etc/ssh/sshd_config:

Notice that grep finds all lines that match the text pattern regardless of where the pattern is located.

Extend grep with regular expressions

In the previous example, when you searched for Port in the SSH configuration file, grep returned two lines. The line you were looking for, Port 22, and an additional line containing the search pattern. In some cases, that’s exactly what you want. In other cases, grep could find too many entries that you’re not interested in, requiring you to sort through them to find the desired information.

To avoid that, you can use regular expressions to be more specific about what you’re looking for. For example, to find only lines that start with the word Port, you can use the regular expression operator ^, like this:

This time grep returned only the line that started with Port since, in the second line, the expression Port is in the middle.

You can also use extended regular expressions with the command-line parameter -E. For example, to search for a pattern that contains the word Port followed by numbers, use this regular expression:

You can also look for lines that end with a text pattern by using the $ operator. For example, to find all lines that end with none in sshd_config, use grep like this:

Regular expressions are a big part of grep, making it powerful and flexible. However, regular expressions are a huge topic. For additional information, look at Regular expression on Wikipedia or Regular expressions 101.

Find text in multiple files and directories

Similar to finding text patterns in a single file, you can use grep to find text in multiple files or directories. To find text in multiple files simultaneously, specify which files to search from after the first file name, or use a shell wildcard such as * for all files. For example, to search for a configuration in two files:

When you use multiple files, grep shows the name of the file where it found a match before showing the matched line.

To run the search recursively in multiple subdirectories, use the command line flag -R:

The grep command is fast and returns results quickly, but it may take a long time if you specify too many files or subdirectories to search.

Find text in another command’s output

Similar to other Unix utilities, grep also acts on stdin when you pipe the output of another command into it. This is a fast and useful way to filter a command’s output to match the text pattern you’re looking for.

For example, if you want to check whether the package openssh is installed in your Fedora or Red Hat Enterprise Linux (RHEL) operating system, you can pipe the output of command rpm -qa, which lists all installed packages, into grep to search for the pattern:

You can filter long command outputs with grep, making finding useful information easier.

Additional useful options

The grep command provides many options to change how it searches for patterns or displays results. So far in this article, you’ve seen some of them. While I can’t list all options, here are some other useful examples:

  • Use option -i for a case-insensitive search.
  • Use option -v to invert the search and display lines that do not match the pattern.
  • Use option -w to search for entire words only instead of patterns in the middle of other words.
  • Use option --color for colored output, making it easier to spot the matched pattern.

For a complete list of grep options, consult the man pages.

What’s next?

The GNU grep utility is flexible and useful, helping you accomplish many tasks in your daily sysadmin activities. The more you use grep, the more comfortable you will become, and soon you’ll notice you’re relying on it all the time.

For more information about grep, look at some of these links:

You can also find more information about grep in your Linux system by using man grep or quick, valuable examples with the tldr tool.


This article was originally published at https://www.redhat.com/ on September 23, 2022.

Author

  • Ricardo Gerardi

    Ricardo Gerardi is a geek, writer, and technical professional passionate about technology and writing. His current career focus on IT automation with Ansible and containers with Podman, Kubernetes, and OpenShift. Ricardo has been a Linux and open source enthusiast and contributor for over 20 years. He is currently interested in hacking stuff using the Go programming language, and he's the author of Powerful Command-Line Applications in Go: Build Fast and Maintainable Tools. Ricardo also writes regularly about Linux, Vim, and command line tools for many online publications. On his free time, Ricardo enjoys spending time with his daughters, reading science fiction books, and playing video games.


Ricardo Gerardi

Ricardo Gerardi is a geek, writer, and technical professional passionate about technology and writing. His current career focus on IT automation with Ansible and containers with Podman, Kubernetes, and OpenShift. Ricardo has been a Linux and open source enthusiast and contributor for over 20 years. He is currently interested in hacking stuff using the Go programming language, and he's the author of Powerful Command-Line Applications in Go: Build Fast and Maintainable Tools. Ricardo also writes regularly about Linux, Vim, and command line tools for many online publications. On his free time, Ricardo enjoys spending time with his daughters, reading science fiction books, and playing video games.

1 Comment

Seth Kenlon · 2023-05-26 at 00:30

I don’t know why it’s never occurred to me to grep in two files at once. Great tips in this article, thank you!

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *