Raspberry Pi online course

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, -i ignores 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.txt

Search 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.txt

Every 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 PATTERN

ps -A lists processes across the system. Pipe its output into grep to look for a process associated with the nano editor:

ps -A | grep nano

Instead 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.txt

This 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

GoalCommandExpected result
Search one file — grep text textfile.txt — Prints lines in textfile.txt containing text.
Ignore case — grep -i text textfile.txt — Also matches Text and TEXT.
Search current-directory files — grep text ./* — Searches the filenames expanded from the current directory and identifies the source file.
Filter running processes — ps -A | grep nano — Prints process-list lines containing nano.

Understanding the command components

ComponentPurposeExample
grep command name — Starts the text search utility — grep
Option — Changes matching behavior — -i
Search pattern — Text expression to find — text
Single filename — Supplies one file as input — textfile.txt
Multiple-file wildcard — Lets the shell supply several filenames — ./*
Piped command input — Sends another command's standard output to grep — ps -A | grep nano

Troubleshooting

No results appear

Check the most common causes:

  1. Try the case-insensitive form, such as grep -i text textfile.txt.
  2. Run pwd to confirm the current working directory and ls to confirm the filenames.
  3. Check the spelling, spaces, and punctuation in the pattern.
  4. 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

  1. Use ls to identify files in the current directory.
  2. Search one file with grep text textfile.txt.
  3. Repeat with grep -i text textfile.txt and compare capitalization results.
  4. Search multiple current-directory files with grep text ./*.
  5. List processes with ps -A, then filter them with ps -A | grep nano.