grep Command in Linux and Unix: A Tutorial

The grep command is a tool in Linux and Unix that’s used to search for specific text within files, such as words, phrases or patterns. Learn the code.

Written by K. Jagathish
Published on Aug. 08, 2024
magnifying glass searching text in an open book
Image: Shutterstock / Built In
Brand Studio Logo

The grep (global regular expression print) command is a tool in Linux and Unix that helps you search for specific text within files. You can use it to find words, phrases or patterns in one or multiple files, making it easier to locate and manage information. 

What Is the grep Command?

The grep command, which stands for global regular expression print, is a Linux and Unix tool that allows you to search for text within files. The syntax for the grep command is: 

grep [options] pattern [file...]

For example, if you want to find all lines containing the word “error” in a log file, grep can quickly show you those lines.

 

grep Command Syntax

The basic syntax for the grep command is as follows:

grep [options] pattern [file...]
  • options: Optional parameters that modify the behavior of grep.
  • pattern: The search pattern, which can be a simple string or a regular expression.
  • file: The file or files to search through. If omitted, grep searches the standard input.

When grep finds a match, the matched lines will be printed to the terminal.

More on LinuxHow to Kill a Process in Linux

 

When to Use the grep Command

Use the grep command whenever you need to search for specific text patterns within files. It’s particularly useful for:

  • Searching logs for error messages.
  • Filtering the output of other commands.
  • Finding configuration settings in system files.
  • Extracting data from large text files.

 

Using the grep Command

The grep command can be used in a variety of ways to find specific text within Linux and Unix. Below are different ways to use grep, along with code and examples. 

How to Find Matching Strings With grep

To search for a specific string in a file, use:

grep "search_string" filename

Example:

grep "error" /var/log/syslog

This command searches for the string “error” in the /var/log/syslog file.

Searching for a String in More Than One File

To search for a specific string in multiple files, use a wildcard to specify the files. You can also list multiple files explicitly:

grep "search_string" file1 file2 file3

Example:

grep "error" /var/log/syslog /var/log/auth.log

This command searches for the string “error” in both /var/log/syslog and /var/log/auth.log.

How to Search All Files in Directory and Subdirectories in grep

To search for a string in all files within a directory and its subdirectories, use the -r option:

grep -r "search_string" /path/to/directory

How to Ignore Case-Sensitivity in grep

To perform a case-insensitive search, use the -i option:\

grep -i "search_string" filename

Example:

grep -i "error" /var/log/syslog

This command searches for “error” in a case-insensitive manner.

How to Count Number of Occurrences

To count the number of times a pattern appears, use the -c option:

grep -c "search_string" filename

Example:

grep -c "error" /var/log/syslog

This command counts how many times “error” appears in the file.

How to Find Files with grep

To search for a pattern in multiple files, you can use wildcards:

grep "search_string" /path/to/files/*.log

Example:

grep "error" /var/log/*.log

This command searches for “error” in all .log files within the /var/log/ directory.

Using grep with Regular Expressions (REGEX)

grep supports regular expressions, which allows for more complex search patterns. For instance, to find lines that start with a pattern, use the caret ^:

grep "^pattern" filename

Example:

grep "^error" /var/log/syslog

This command searches for lines that start with “error”.

How to Use Invert grep Search

To invert the search and display lines that do not match the pattern, use the -v option:

grep -v "search_string" filename

Example:

grep -v "error" /var/log/syslog

This command displays all lines that do not contain “error”.

Search for Multiple Patterns

To search for multiple patterns, you can use the -e option for each pattern:

grep -e "pattern1" -e "pattern2" filename

Or, if you prefer, you can use a single -E option with an extended regular expression:

grep -E "pattern1|pattern2" filename
A tutorial on the grep command in Linux. | Video: Learn Linux TV

More on Data ScienceA Guide to Reading and Writing CSV Files and More in Apache Spark

 

13 grep Options to Know

grep also comes with a variety of options to customize its behavior:

  1. -i: Ignore case distinctions.
  2. -v: Invert the match to select non-matching lines.
  3. -c: Count the number of matching lines.
  4. -l: List filenames containing the match.
  5. -L: List filenames that do not contain the match.
  6. -n: Prefix each line of output with the line number.
  7. -H: Print the filename for each match.
  8. -r or -R: Read all files under each directory, recursively.
  9. -w: Match whole words only.
  10. -x: Match whole lines only.
  11. -E: Use extended regular expressions (ERE).
  12. -F: Interpret pattern as a list of fixed strings (fgrep).
  13. -q: Quiet, do not write anything to standard output.

Frequently Asked Questions

The grep command is used to search for specific patterns within files. It stands for global regular expression print, and it’s useful for filtering and finding specific text strings in files or output from other commands. Its versatility and powerful pattern matching make it indispensable for text processing and data analysis.

The grep command searches files for patterns using regular expressions, while the fgrep (or grep -F) command searches for fixed strings, or literal text, and doesn’t interpret any special characters. fgrep is faster than grep for simple string searches because it doesn’t parse regular expressions.

Explore Job Matches.