bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Bash/Text Processing
Bash•Text Processing

Bash grep Command - Search Text Using Patterns

Using the grep Command

The grep command is used to search for text patterns within files.

It's a powerful way to find specific text in large files or across many files.

Basic Usage

To search for a pattern in a file, use grep 'pattern' filename :

Example

grep 'shell' file.txt
A shell is a text-based interface that lets you talk to your computer.
There are different types of shells. Bash (Bourne Again SHell)

Options

The grep command has options to change how it works:

  • -i - Search ignoring case differences (uppercase or lowercase)
  • -r - Search through all files in a directory and its subdirectories
  • -v - Find lines that do not match the pattern

Ignore Case

The -i option lets you search without worrying about case sensitivity.

Example: Ignore Case

grep -i 'shell' file.txt
Understanding Shells
A shell is a text-based interface that lets you talk to your computer.
There are different types of shells. Bash (Bourne Again SHell)

Recursive Search

The -r option allows you to search through all files in a directory and its subdirectories.

Example: Recursive Search

grep -r 'search_term' /home/user/my_directory
/home/user/.my_secret_file:A shell is a text-based interface that lets you talk to your computer.
/home/user/.my_secret_file:There are different types of shells. Bash (Bourne Again SHell)
/home/user/.my_secret_file:Bourne Shell (sh): The original Unix shell, developed by Stephen Bourne.
/home/user/copy_of_my_file.txt:A shell is a text-based interface that lets you talk to your computer.
/home/user/copy_of_my_file.txt:There are different types of shells. Bash (Bourne Again SHell)
/home/user/myfolder/my_file.txt:A shell is a text-based interface that lets you talk to your computer.
/home/user/myfolder/my_file.txt:There are different types of shells. Bash (Bourne Again SHell)
/home/user/my_file.txt:A shell is a text-based interface that lets you talk to your computer.
/home/user/my_file.txt:There are different types of shells. Bash (Bourne Again SHell)

Invert Match

The -v option finds lines that do not match the pattern.

Example: Invert Match

grep -v 'shell' my_file.txt
Understanding Shells

Using grep with Regular Expressions

Regular expressions allow you to search for complex patterns.

For example, grep '^[A-Za-z]' file.txt finds lines starting with a letter.

Example: Regular Expressions

grep '^[A-Za-z]' my_file.txt
Understanding Shells
A shell is a text-based interface that lets you talk to your computer.
There are different types of shells. Bash (Bourne Again SHell)
is popular because it's powerful and easy to use.

Chapter

Back to Text Processing

Next

Bash awk - Pattern Scanning and Processing Language

This chapter

Overview
7

Lessons

18m

Read time

1. Bash grep Command - Search Text Using Patterns2. Bash awk - Pattern Scanning and Processing Language3. Bash sed Command - Stream Editor4. Bash cut Command - Remove Sections from Lines5. Bash sort Command - Sort Lines of Text Files6. Bash tail Command - Display Last Part of Files7. Bash head Command - Display the beginning of a file

On this page

Using the grep CommandBasic UsageOptionsIgnore CaseExample: Ignore CaseRecursive SearchExample: Recursive SearchInvert MatchExample: Invert MatchUsing grep with Regular ExpressionsExample: Regular Expressions