VMware ESXi and vSphere Cluster Management
Search Text Strings Within Files with grep
Learn how to use grep on Raspberry Pi OS to search text files, directories, and command output with practical examples and beginner-friendly options.
grep is a Linux command-line tool that searches plain-text input for lines matching a supplied pattern. A matching line is printed to standard output, which is the normal text output shown by a terminal command.
grep can read from named files or from another command through a pipe. This makes it useful for locating words in configuration files, source code, logs, and command output.
Prerequisites
You should know how to open a terminal on Raspberry Pi OS and use basic commands such as pwd, cd, ls, cat, and less. It also helps to understand relative paths, absolute paths, and the shell wildcard *.
Basic grep syntax
grep [OPTIONS] PATTERN FILE
- PATTERN is the word, phrase, or regular expression to find.
- FILE is one file, several files, or paths expanded from a filename wildcard.
- Input can also come from another command through a pipe, in which case a file argument is not needed.
- OPTIONS, also called flags, begin with a hyphen and change grep's behavior.
Quoting the pattern is a good habit. It protects spaces and shell-special characters from being interpreted by the shell before grep receives them.
grep 'system ready' status.txt
Without quotes, the shell would treat system and ready as separate arguments. Quoting is especially important when a pattern contains spaces, wildcards, or punctuation.
Ways to provide input to grep
| Input source | Example | When to use it |
|---|---|---|
| Single file | grep 'text' textfile.txt | Search one known plain-text file. |
| Several files or wildcard expansion | grep -H 'text' ./* | Search paths in the current directory. |
| Directory with recursive option | grep -r -n 'text' /home/pi/files/ | Include files in nested subdirectories. |
| Output from another command through a pipe | ps -A | grep 'nano' | Filter terminal output instead of reading a file directly. |
Search one text file
Suppose textfile.txt contains these lines:
This is a text file.
It contains sample text.
A different line has no target word.
Run:
grep 'text' textfile.txt
The result is:
This is a text file.
It contains sample text.
grep prints the complete lines containing the pattern, not only the matching word. Each matching line is normally printed once, even when the pattern occurs multiple times on that line.
For example, a line such as text appears twice: text is still printed as one output line.
Search several files in a directory
A shell wildcard, also called a glob, is filename-matching syntax. The * wildcard expands before grep runs.
grep -H 'text' ./*
Here, ./* means paths in the current directory whose names match the wildcard. The -H option makes grep display the filename with each matching line.
You can use a named directory in the same way:
grep -H 'text' /home/pi/files/*
This searches items directly inside /home/pi/files/. It does not automatically search nested subdirectories. Also, ./* can expand to directories as well as files. If grep receives a directory without a recursive option, it may report a directory-related message instead of searching inside it.
Search recursively
A recursive search includes files in the selected directory and its nested subdirectories. Use -r when this is required.
grep -r -n 'text' /home/pi/files/
This searches below /home/pi/files/ and shows line numbers for matches. The related -R form also performs a recursive search; for beginner searches, -r is commonly sufficient.
To list only the names of files containing a match, use -l:
grep -rl 'text' /home/pi/files/
Filter command output with a pipe
A pipe is the | operator. It sends the standard output of one command to the standard input of another command.
ps -A
ps -A displays a list of running processes. The list can be long, so send it to grep:
ps -A | grep 'nano'
In this pipeline, ps -A produces the process list, the pipe sends that list onward, and grep 'nano' prints only lines containing nano. The pipeline reduces a large listing to relevant lines.
A process search can sometimes display the grep command itself because the command line contains the search text. For a more deliberate process lookup, you can use:
pgrep -a nano
Case-sensitive and case-insensitive searches
Ordinary grep matching is case-sensitive: uppercase and lowercase letters are treated as different.
grep 'text' textfile.txt
This searches for lowercase text. It does not match Text or TEXT. Use -i for a case-insensitive search:
grep -i 'text' textfile.txt
With -i, text, Text, and TEXT are treated as equivalent for matching.
Useful beginner grep options
| Option | Purpose | Example |
|---|---|---|
-i | Ignore uppercase and lowercase differences. | grep -i 'text' textfile.txt |
-n | Show the line number before each matching line. | grep -n 'text' textfile.txt |
-c | Count matching lines rather than print their content. | grep -c 'text' textfile.txt |
-v | Print lines that do not match the pattern. | grep -v 'text' textfile.txt |
-w | Match a complete word. | grep -w 'text' textfile.txt |
-r | Search recursively through directories. | grep -r 'text' /home/pi/files/ |
-l | List filenames containing a match. | grep -rl 'text' /home/pi/files/ |
-H | Always display filenames with matching lines. | grep -H 'text' ./* |
-F | Treat the pattern as a literal fixed string, not a regular expression. | grep -F 'version 1.2.3' notes.txt |
Line numbers, counts, and inverted results
grep -n 'text' textfile.txt
grep -c 'text' textfile.txt
grep -v 'text' textfile.txt
-n adds line numbers. -c counts matching lines, not every individual occurrence of the pattern. -v reverses the test and prints non-matching lines.
Whole-word matching
By default, a pattern can match part of a longer word. The -w option asks grep to match a complete word:
grep -w 'text' textfile.txt
This can match text as a separate word while avoiding a match inside a longer word such as textfile.
Patterns, regular expressions, and fixed strings
grep patterns can be interpreted as regular expressions. A regular expression is a pattern language for describing text structures. This is powerful, but it means that punctuation may have special meaning.
Characters such as ., *, [, ], ^, $, and ? may be interpreted as regular-expression operators or have special behavior. If you want literal text, use -F:
grep -F 'version 1.2.3' notes.txt
With -F, the periods in 1.2.3 are ordinary characters. You can also escape special characters when using regular-expression mode, but fixed-string mode is usually clearer when the search target is literal text.
Quote patterns containing spaces or shell-special characters:
grep -F 'error: disk full' system.log
Understanding output and exit status
| Situation | Terminal output | Meaning |
|---|---|---|
| One or more matching lines | Matching lines are printed. | grep found the pattern. |
| No matches | Usually no output. | The selected input contained no matching line. |
| Permission or file error | An error message is printed. | grep could not read a file, directory, or argument. |
| Directory passed without recursive handling | A message identifies an item as a directory. | Use -r, or limit the wildcard to suitable files. |
grep also returns an exit status that scripts can test:
0means at least one match was found.1means no match was found.- A different nonzero status, commonly
2, indicates an error such as an invalid option or an unreadable input.
A no-match result is therefore different from a command failure, even though both can produce a nonzero status.
Troubleshooting grep
No output appears
- The pattern may not occur in the selected input.
- The capitalization may differ. Try
grep -iwhen case should not matter. - The wrong file or directory may have been selected.
Check your location and inspect the file:
pwd
ls
cat textfile.txt
Use less instead of cat for a large file:
less textfile.txt
grep says an item is a directory
A wildcard may have expanded to include a subdirectory, or a directory may have been supplied directly. Search recursively with -r:
grep -r 'text' /home/pi/files/
If recursion is not wanted, limit the wildcard to a suitable file type:
grep -H 'text' ./*.txt
The search matches unexpected text
The pattern may be acting as a regular expression, or the search word may occur inside longer words. Use -F for literal text and -w for complete-word matching:
grep -Fw 'text' textfile.txt
Expected matches are missed because capitalization differs
Default grep matching is case-sensitive. Add -i:
grep -i 'text' textfile.txt
Permission denied during recursive searching
Your account may not be allowed to read one or more files or directories. Search locations that your user can access, adjust permissions when appropriate, or use elevated privileges only when necessary and understood. Avoid blindly searching the entire system as an administrator.
Practical command checklist
grep 'text' textfile.txt
grep -i 'text' textfile.txt
grep -n 'text' textfile.txt
grep -c 'text' textfile.txt
grep -w 'text' textfile.txt
grep -H 'text' ./*
grep -r -n 'text' /home/pi/files/
grep -rl 'text' /home/pi/files/
ps -A | grep 'nano'
grep -F 'version 1.2.3' notes.txt
Once you can choose the correct input source, quote the pattern, and select the needed option, grep becomes a quick way to inspect files and filter Linux command output.