Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH

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.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.