Search Text Strings Within Files with grep on Raspberry Pi
Learn how to use grep on Raspberry Pi OS to search text files, multiple files, and command output, including case-insensitive searches and troubleshooting.
grep is a Linux command-line utility that searches plain-text input for lines containing a specified text pattern. A matching line is printed in the terminal, making grep useful for inspecting files and filtering long command results on Raspberry Pi OS or Raspbian.
This lesson assumes that you can open a terminal, use cd to change directories, and use ls to list files. For a broader introduction to the Raspberry Pi command line, see Terminal in Raspbian.
What grep searches
grep searches the contents of files or other text input. It does not search for filenames. To search for a file by its name, use a file-finding command such as Search for Files.
Plain text is human-readable text stored without binary formatting. Configuration files, source code, logs, and notes are common examples. grep examines this input one line at a time and prints each line that matches its pattern.
Basic grep command structure
grep [OPTIONS] PATTERN FILE- grep is the command name.
- OPTIONS change how matching works. For example,
-iignores letter case. - PATTERN is the text expression that grep attempts to match.
- FILE is a filename, a filename pattern that expands to several files, or input received through a pipe.
Quote patterns that contain spaces or shell-special characters. Quoting keeps the shell from interpreting parts of the pattern before grep receives them.
grep "Raspberry Pi" notes.txtSearch one text file
Suppose the current directory contains a plain-text file named textfile.txt. Search it for the string text with:
grep text textfile.txtEvery line containing text is displayed. If the string appears on several different lines, grep prints each matching line separately.
The basic search is not limited to complete words. For example, searching for text can match a line containing textbook, because the requested string occurs inside that longer word.
Search files in the current directory
You can pass several filenames to grep by using a shell wildcard:
grep text ./*Here, . means the current working directory: the directory in which the command is being run. The * is a wildcard. Before grep runs, the shell expands ./* into the names of entries in the current directory.
For example, if the directory contains file.txt, new-file.txt, and textfile.txt, the shell supplies those paths as multiple file arguments. Because grep is searching more than one file, it identifies the source of each matching line.
file.txt:This line contains text.Use ls first when you are unsure which files are in the working directory:
ls
grep text ./*Use grep with command output
grep can filter the standard output of another command. Standard output is the normal text produced by a command.
The pipe operator, written as |, sends the standard output from one command to the standard input of another command:
first-command | grep PATTERNps -A lists processes across the system. Pipe its output into grep to look for a process associated with the nano editor:
ps -A | grep nanoInstead of reading the complete process list, you see only lines containing nano. Filtering long output makes relevant lines easier to locate.
Case-sensitive and case-insensitive searches
By default, grep is case-sensitive. This means uppercase and lowercase letters are treated as different characters. A search for text does not normally match Text or TEXT.
Use the -i option to ignore letter case:
grep -i text textfile.txtThis search can match lines containing text, Text, TEXT, or other capitalization combinations.
Read and validate grep results
- Displayed output is evidence that a supplied input line contains the pattern.
- A matching line is not necessarily an exact-word-only match. The pattern may occur as part of a longer word.
- No displayed lines normally means that no match was found in the supplied input.
- No result can also indicate a capitalization difference, the wrong file or directory, a spelling error, or different punctuation.
When a result matters, inspect the complete line and confirm the filename. grep tells you that a line matches; it does not automatically prove that the line has the exact meaning you intended.
Common grep scenarios
Understanding the command components
Troubleshooting
No results appear
Check the most common causes:
- Try the case-insensitive form, such as
grep -i text textfile.txt. - Run
pwdto confirm the current working directory andlsto confirm the filenames. - Check the spelling, spaces, and punctuation in the pattern.
- Confirm that the expected content is in the file being supplied to grep.
grep says that a path is a directory
A directory was supplied where grep expected a regular text file, often because a wildcard expanded to include a directory. Supply individual files or a filename pattern that selects only the intended text files.
The process search shows grep itself
With ps -A | grep nano, the command line used to run grep can contain nano. This can produce an additional matching line. Review the process identifier and command details before deciding which entry is the target process.
A longer word matches
Basic grep matching searches for the pattern anywhere in a line. Therefore, a short pattern can match a substring inside a longer word. This differs from whole-word matching. Whole-word searches and regular expressions are useful next steps when substring matches are too broad.
Practice checklist
- Use
lsto identify files in the current directory. - Search one file with
grep text textfile.txt. - Repeat with
grep -i text textfile.txtand compare capitalization results. - Search multiple current-directory files with
grep text ./*. - List processes with
ps -A, then filter them withps -A | grep nano.