VMware ESXi and vSphere Cluster Management

Search Text Strings in Linux with grep

Learn how to use Linux grep to find matching text lines in files, directory trees, and command output with recursive and case-insensitive searches.

What grep Does

grep is a command-line utility that searches text input for a specified pattern. It selects and displays the complete input lines that contain a match.

For example, grep can search the contents of a text file for a username, scan a directory tree for a configuration value, or filter the output of another command.

Normally, grep writes matching lines to standard output. Standard output is the normal text output produced by a command. If no input line contains the pattern, grep produces no matching text.

Basic grep Command Structure

The general form of a grep command is:

grep [OPTIONS] PATTERN FILE
  • OPTIONS alter grep's behavior. Options usually begin with a hyphen, such as -i.
  • PATTERN is the text expression grep attempts to match.
  • FILE is the input file grep reads.

A simple search uses a pattern and a file:

grep bob bobs_file.txt

This command searches bobs_file.txt for the text bob. Every line containing that pattern is displayed in full. If the pattern appears on several lines, grep displays each matching line.

Search a Single File

Suppose bobs_file.txt contains the following lines:

Alice manages the project
bob reviews the source code
The team asked Bob for feedback
bob maintains the test server

Running grep bob bobs_file.txt displays the lines containing lowercase bob:

bob reviews the source code
bob maintains the test server

The line containing Bob with an uppercase B is not selected because ordinary grep matching is case-sensitive. Matching is based on the pattern and the input text exactly enough to distinguish uppercase and lowercase letters.

Search Directories Recursively

A recursive search examines a directory and also examines files in nested subdirectories. Use the -r option to enable this behavior.

grep -r bob /home/bob

This searches files below /home/bob, including files in directories such as /home/bob/projects and deeper nested directories. When grep searches multiple files, it normally prefixes each matching line with the source filename so you can identify where the result came from.

/home/bob/notes.txt:bob reviews the source code
/home/bob/projects/todo.txt:bob maintains the test server

The filename prefix is useful because identical text may occur in several files. Without that context, you would see matching content but not know its source.

Case-Sensitive and Case-Insensitive Searches

By default, grep treats uppercase and lowercase letters as different. A search for bob can match lowercase bob, but not necessarily Bob or BOB.

Use -i to ignore letter case:

grep -i bob bobs_file.txt

This can match bob, Bob, BOB, and other capitalization variations.

OptionEffectExample
-rRecursively searches a directory and its subdirectories.grep -r bob /home/bob
-iIgnores uppercase and lowercase differences.grep -i bob bobs_file.txt

Use grep in Pipelines

A pipe is the shell operator |. It sends one command's standard output to another command's standard input. This lets grep act as a filter for lengthy command output.

Filter Group Information

/etc/group is a system file containing local group account information. You can display it and pass the result to grep:

cat /etc/group | grep cdrom

cat commonly displays a file's contents. In this pipeline, grep keeps only the group-file lines containing cdrom. Instead of manually reading every group entry, you inspect only the relevant lines.

Filter a Process List

ps -A displays processes on the system. Its output may be long, so pipe it to grep:

ps -A | grep top

This displays process-list lines containing top. More than one displayed line can mean that several process entries contain the pattern. The results can also include more than one line for other reasons, such as multiple matching input lines.

Common grep Input Sources

Input sourceHow grep receives itExample use
Single fileGive the file as an argument.grep bob bobs_file.txt
Directory treeGive a directory and enable recursive mode with -r.grep -r bob /home/bob
Output from another command through a pipePlace | between the producing command and grep.ps -A | grep top

Interpret grep Results

  • The primary result is the complete matching input line.
  • When multiple files are searched, grep commonly adds a filename prefix.
  • A recursive search can show both filenames and matching lines because it examines many files.
  • Several output lines indicate several matching input lines; in a process search, they may be separate process entries.
  • An empty result means no matching line was found in the supplied input.

For example, searching one file produces content such as:

bob reviews the source code

Searching multiple files adds source context:

/home/bob/notes.txt:bob reviews the source code

Troubleshoot Missing or Unexpected Results

No Output Appears

No output usually means grep did not find a matching line in the supplied input. Check that the pattern is spelled correctly, the file or directory path is correct, and the expected text is actually present.

Case can also explain an empty result. If the input may contain bob, Bob, or BOB, try:

grep -i bob bobs_file.txt

If the expected file is inside a nested directory, a search without recursion may not inspect it. Search the directory tree with:

grep -r bob /home/bob

Only Direct Directory Contents Are Searched

If nested files are not being inspected, recursive mode was probably omitted. Add -r and provide the directory path.

The Output Is Too Long

A broad pattern may occur frequently. Use a more specific pattern, or filter a verbose command through grep. For example, ps -A | grep top is more manageable than reading the full process list when you only need process entries containing top.

Filename Results Are Expected but Matching Text Appears

When only one file is searched, grep normally displays matching lines rather than a filename list. Filename prefixes become especially useful when grep searches multiple files, such as with a recursive directory search.

Exam-Ready Summary

  • grep selects and displays complete input lines containing a pattern.
  • The basic structure is grep [OPTIONS] PATTERN FILE.
  • grep bob bobs_file.txt searches one file for lowercase bob.
  • -r searches a directory and its nested subdirectories.
  • -i makes matching case-insensitive.
  • The pipe operator | sends one command's output into grep.
  • cat /etc/group | grep cdrom filters group information.
  • ps -A | grep top filters process-list output.
  • No output means no matching line was found in the supplied input, though an incorrect path, capitalization, or missing recursion can also explain the result.

For the related file-name search topic, see Search for text strings using grep.