Skip to main content
In this lesson, you’ll learn how to use the grep utility to search for specific text within files. Grep is particularly useful when working with large files containing thousands of lines, as it helps you quickly filter and locate the information you need.

Basic grep Usage

The general syntax for the grep command is: grep [options] [search pattern] [file] For example, to search for the term “password” inside the SSH daemon configuration file, run:
Always enclose your search pattern in single quotes to prevent Bash from misinterpreting special characters, such as asterisks used in more complex search patterns.

Case Sensitivity

By default, grep performs a case-sensitive search. This means that searching for ‘password’ will only match lowercase occurrences. Searching for ‘Password’ (with an uppercase P) produces different results. Consider the following examples:
To perform a case-insensitive search, use the -i option:

Recursive Search in Directories

Grep allows you to search through all files within a directory and its subdirectories using the -r (recursive) option. For example:
The matched files are highlighted by default (using color) to help you quickly identify the search term. You might also see “Permission denied” messages for files your user does not have permission to read. Combine recursive search with case-insensitivity using both -r and -i options:

Using sudo for Restricted Files

If you need to search through files that are only accessible by the administrator (root), prepend the grep command with sudo. Note that this might disable colored output, so the --color option can be used to force it:
When using sudo with grep, be aware that forcing colored output is necessary if you want to visualize matches in color.
If you need to display lines that do not contain a specific pattern, use the -v option. This inverts the search results to exclude the matched pattern.

Matching Whole Words

Sometimes it’s important to match only the exact word “password” rather than substrings (like “PasswordAuthentication” or “passwords”). The -w option restricts grep to whole word matches. Compare the outputs below:
Notice that using the -w option excludes lines where “password” is part of a longer word.

Displaying Only the Matched Portions

By default, grep prints the entire line containing a match. If you need to extract just the text that matches your pattern, use the -o option. This is useful when you want to isolate specific data. The following example compares two approaches:
Using the -o option outputs only the matching portions of each line, simplifying further data processing.

Conclusion

Grep is a versatile and powerful tool for searching text within files. Its many options—such as case insensitivity, recursive searching, whole word matching, and output customization—make it an essential utility for efficiently filtering and extracting information. Happy grepping! For more technical guides and tips, check out our Comprehensive Unix/Linux Tutorials.

Watch Video