Search for Text Strings with grep in Linux
Learn how to use Linux grep to search file contents, recursively search directories, filter command output with pipes, and handle case-sensitive matches.
grep is a Linux command-line utility that searches input text for lines matching a pattern. It is useful for finding text in files and for filtering long command output.
grep searches the contents of files, not filenames. To locate files by their names, use a file-searching tool such as Linux file structure commands together with an appropriate search command.
By default, grep prints matching lines to standard output, the normal text stream displayed in the terminal.
Basic grep Syntax
grep [OPTIONS] PATTERN FILEThe parts of this command are:
- OPTIONS: optional switches that change how grep searches, such as
-ror-i. - PATTERN: the text or expression grep should match.
- FILE: one or more files whose contents grep should search.
Quote patterns that contain spaces or shell-special characters. Quoting prevents the shell from interpreting those characters before grep receives the pattern.
grep 'error message' application.logSearch One File
To search a named text file, put the pattern before the filename:
grep 'bob' bobs_file.txtThis command prints every line in bobs_file.txt that contains the pattern bob. A matching line is an input line containing text that matches the requested pattern.
A line is normally printed only once, even if the pattern occurs several times on that line. For example, a line containing bob bob produces one output line, not two.
If no line matches, grep normally prints nothing. If the file cannot be read or does not exist, grep reports an error instead.
Search a Directory Tree Recursively
A recursive search includes a directory and all of its subdirectories. Use the -r option:
grep -r 'bob' /home/bobgrep examines searchable files under /home/bob, including files in nested directories, and prints lines containing bob.
When grep searches multiple files, it normally prefixes each matching line with the source filename. This lets you identify where each result came from.
/home/bob/notes.txt:Bob called about the meeting
/home/bob/projects/readme.txt:bob's project notesThe exact output can vary depending on file contents, permissions, and the grep implementation. Binary files may be reported differently from ordinary text files.
Core grep Options Used in This Lesson
Filter Command Output with a Pipe
A pipe is the | shell operator. It sends one command's standard output to another command as input instead of displaying all of that output directly.
producer-command | grep 'pattern'Piping is useful when a command produces many lines but you only want lines containing a particular word or expression.
Filter Group Information
/etc/group is a system file containing local group account information. The following command sends the file's output to grep and displays the entry containing cdrom:
cat /etc/group | grep 'cdrom'The result is the matching group entry, which can include the group name, numeric group ID, and listed members. In this example, grep is searching piped input rather than opening a filename itself.
Filter the Process List
ps -A displays processes across the system. Pipe its output to grep to select process-list lines containing a process name:
ps -A | grep 'top'This may print more than one line. Each matching line represents a separate process-list entry whose text contains top. Multiple lines do not necessarily mean that the word appeared multiple times in one line; they can indicate multiple matching processes.
Ways to Provide Text to grep
Case-Sensitive Matching
grep is case-sensitive by default. The pattern bob matches lowercase bob, but not necessarily Bob or BOB.
grep 'bob' bobs_file.txtUse -i when capitalization should not matter:
grep -i 'bob' bobs_file.txtThis command can match bob, Bob, BOB, and other capitalization forms in the file.
How to Interpret grep Results
- If grep prints a line, that line matched the supplied pattern.
- If grep prints nothing, no matching line was found in the input, or the input was empty.
- Filenames commonly appear during recursive or multi-file searches so each result can be associated with its source.
- Several output lines can represent several matching lines in one file, matches in multiple files, or several matching process entries.
- Repeated occurrences of a pattern on one line normally still produce only one printed line.
For example, the output from ps -A | grep 'top' may contain several lines because several processes match the text. Read the complete process-list columns to determine which process each line describes.
Troubleshooting grep Searches
No Output Despite Expecting a Match
Check capitalization first. If the file contains Bob but you searched for lowercase bob, repeat the search with -i:
grep -i 'bob' bobs_file.txtAlso confirm the filename, path, spelling, and that the expected text is actually in the file.
Nested Directories Are Missing
A search without -r does not walk through a directory tree. Add recursive mode:
grep -r 'bob' /home/bobResults Do Not Show a Filename
A search of one file or piped input may show only matching text because there is no need to distinguish among multiple named files. Recursive and multi-file searches normally include filenames with their matches.
A Process Search Returns Several Lines
More than one line from ps -A | grep 'top' means more than one process-list entry contains the requested text. Inspect each complete line rather than treating the results as repeated occurrences on one line.
Long Output Is Difficult to Read Manually
Pipe the producing command into grep so irrelevant lines are removed before you inspect the output:
ps -A | grep 'top'
cat /etc/group | grep 'cdrom'Exam-Relevant Notes
- grep searches text lines and prints matching lines; it does not primarily search for filenames.
- The general form is
grep [OPTIONS] PATTERN FILE. -renables recursive directory searching.-imakes matching case-insensitive.|passes one command's standard output to grep as input.- One matching line is normally printed once, even when the pattern occurs multiple times on that line.
- Filenames in recursive output identify the file that supplied each matching line.
Related Linux Skills
After learning grep, explore how to show the full path of shell commands, inspect file types with file type detection, and learn more about Bash shell syntax.