Back to DevOps Series
grep Command full explanation :

grep Command full explanation :

Definition

grep is a Linux command used to search for lines in a file or input that match a specific word, text, or pattern.

Syntax

Loading...
  • pattern → The word or text you want to search for.
  • filename → The file in which you want to search.

Example

Suppose students.txt contains:
Loading...
Search for Bob:
Loading...
Output:
Loading...

Another Example

Search for Charlie:
Loading...
Output:
Loading...

Common Options :

Option
Description
-i
Ignore uppercase/lowercase (case-insensitive search).
-n
Show line numbers with matching lines.
-v
Show lines that do not match the pattern.
-c
Count the number of matching lines.
-r
Search recursively inside directories.
-w
Match the whole word only.

Example: Ignore Case

File:
Loading...
Command:
Loading...
Output:
Loading...

Example: Show Line Numbers

Loading...
Output:
Loading...

Example: Count Matches

Loading...
Output:
Loading...

Example: Invert Match

Loading...
Output:
Loading...

Using grep with a Pipe (|)

Since grep can read from stdin, it is commonly used with pipes.
Example:
Loading...
Flow:
Loading...
This is equivalent to:
Loading...

One-Line Definition for Notes

grep is a Linux command used to search and display lines that match a specified word or pattern from a file or standard input (stdin).