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.txtThis 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 serverRunning grep bob bobs_file.txt displays the lines containing lowercase bob:
bob reviews the source code
bob maintains the test serverThe 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/bobThis 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 serverThe 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.txtThis can match bob, Bob, BOB, and other capitalization variations.
| Option | Effect | Example |
|---|---|---|
-r | Recursively searches a directory and its subdirectories. | grep -r bob /home/bob |
-i | Ignores 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 cdromcat 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 topThis 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 source | How grep receives it | Example use |
|---|---|---|
| Single file | Give the file as an argument. | grep bob bobs_file.txt |
| Directory tree | Give a directory and enable recursive mode with -r. | grep -r bob /home/bob |
| Output from another command through a pipe | Place | 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 codeSearching multiple files adds source context:
/home/bob/notes.txt:bob reviews the source codeTroubleshoot 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.txtIf 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/bobOnly 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
grepselects and displays complete input lines containing a pattern.- The basic structure is
grep [OPTIONS] PATTERN FILE. grep bob bobs_file.txtsearches one file for lowercasebob.-rsearches a directory and its nested subdirectories.-imakes matching case-insensitive.- The pipe operator
|sends one command's output into grep. cat /etc/group | grep cdromfilters group information.ps -A | grep topfilters 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.