Knowledgebase

How to Use the grep Command in Linux?

Imagine you have a large log file with thousands of lines, and you only want to find one specific word, error, or email address.

Scrolling manually would waste a lot of time.

That’s where grep comes in!

The grep command in Linux helps you quickly and efficiently search for specific text within files. It is one of the most powerful and commonly used commands for developers, system administrators, and server users.

In this guide, you’ll learn:

  • What grep is
  • Basic syntax
  • Commonly used options
  • Real practical examples
  • Advanced usage

Let’s get started!


Also Read: A Guide to Echo Command in Linux With Examples


What is the grep Command?

grep stands for Global Regular Expression Print.

It searches for a pattern (a word or text) within files and prints the lines where the pattern matches.

Basic Syntax of grep

Copied!
grep [options] pattern filename
  • pattern → The word or text you want to search.
  • filename → The file where you want to search.
  • options → Extra instructions to modify the search.

Also Read: How to Create a User in Linux & Add it to the sudoer File?


Ways to Use the grep Command in Linux

1. Basic Search Using grep

Command:

Copied!
grep “error” logfile.txt

What It Does:

  • This command searches the file logfile.txt and prints all lines that contain the word error.
  • If the word appears 10 times in different lines, all 10 lines will be displayed.

Important Note:

  • By default, grep is case-sensitive.
  • It means that while using this, error and Error are treated differently.

Also Read: How To Access Linux Server Using PuTTY SSH Terminal?


2. Case-Insensitive Search

If you want to ignore uppercase and lowercase differences:

Command:

Copied!
grep -i “error” logfile.txt

What It Does:

  • The -i option tells grep to ignore uppercase and lowercase differences.
  • For this reason, it matches ‘error’, ‘Error’, ‘ERROR’, etc.

It is useful when log files contain mixed-case text.


Also Read: How To Remove a Directory in Linux with a Command?


3. Search in Multiple Files

You can search for the same pattern in more than one file.

Command:

Copied!
grep “failed” file1.txt file2.txt

What It Does:

  • This command searches for the word ‘failed’ in both file1.txt and file2.txt.
  • If matches are found, grep will show the filename along with matching lines.

This is helpful when checking multiple configuration files at once.


Also Read: How to Change Server Time in Linux?


4. Search Recursively in a Directory

To search inside all files of a directory and its subdirectories:

Command:

Copied!
grep -r “database” /var/www/

What It Does:

The -r option stands for recursive.

  • Searches for the database
  • Checks all files inside /var/www/
  • It will also search inside all subfolders

Use Case:

  • This is extremely useful when working on a website project, and you want to find where a specific word (like database credentials) is used.

Also Read: Install & Configure a CSF Firewall in a CenTOS Server


5. Show Line Numbers

To display line numbers of matching results:

Command:

Copied!
grep -n “error” logfile.txt

What It Does:

The -n option tells grep to show the line number along with the matching line.

Example output:

Copied!
45:error occurred while connecting

This means the word was found on line 45.

This option is very helpful when:

  • You are editing files.
  • You are debugging code.
  • You are fixing configuration issues.

Also Read: How to Change Root Password in Linux?


6. Count Matching Lines

If you only want to know how many lines match:

Command:

Copied!
grep -c “warning” logfile.txt

What It Does:

  • The -c option counts the number of lines that contain the pattern.
  • Instead of showing all matching lines, it will only display a number.

Example output:

Copied!
12

This means that 12 lines contain the word ‘warning’.

This is useful when:

  • You are checking how many times an error has occurred.
  • You are monitoring repeated issues.

Also Read: How to Kill a Process in Linux?


7. Invert Match (Show Non-Matching Lines)

Command:

Copied!
grep -v “success” logfile.txt

What It Does:

  • The -v option reverses the search result.
  • Instead of showing lines that contain the word, it shows lines that do not contain the word.

This is useful when:

  • You want to remove specific entries.
  • You want to filter out successful logs and only see failures.

Also Read: How to Rename a Folder in Linux​?


8. Match Exact Words Only

Command:

Copied!
grep -w “cat” file.txt

What It Does:

  • The -w option matches only the complete word.

Without -w:

  • Searching cat may also match a category or educate.

With -w:

It will match only the exact word cat.

This is useful when searching for:

  • Variable names.
  • Specific commands.
  • Exact keywords.

Also Read: How to Check Memory in Linux?


9. Using Regular Expressions (Advanced Search)

grep supports regular expressions (regex), which allow advanced pattern matching.

Example 1: Lines Starting with a Word

Copied!
grep “^Error” logfile.txt

What It Does:

  • ^ means beginning of line
  • This command finds lines that start with “Error.”

It is useful for structured logs where lines begin with labels.

Example 2: Lines Ending with a Word

Copied!
grep “failed$” logfile.txt

What It Does:

  • $ means end of line.
  • This finds lines that end with the word “failed.”

Example 3: Match Any Number

Copied!
grep “[0-9]” file.txt

What It Does:

  • [0-9] matches any digit from 0 to 9
  • This command finds lines containing at least one number

Regex makes grep extremely powerful for advanced searching.


Also Read: How to Check CPU Usage in Linux?


10. Search for Multiple Patterns

Sometimes you need to search for more than one word. In that case, it is quite helpful.

Method 1: Using -e

Copied!
grep -e “error” -e “warning” logfile.txt

What It Does:

  • -e allows multiple search patterns
  • This command shows lines that contain either an ‘error’ or a ‘warning.’

Method 2: Using the OR Operator

Copied!
grep “error\|warning” logfile.txt

What It Does:

  • \| works as OR.
  • Matches either pattern.

Also Read: How to Check OS Version in Linux​?


11. Display Only the Matching Word

Command

Copied!
grep -o “error” logfile.txt

What It Does:

  • The -o option prints only the matched word, not the entire line.
  • If a line contains the word three times, it will show the word three times separately.

This is useful when:

  • Extracting emails.
  • Extracting IP addresses.
  • Counting exact occurrences.

Also Read: How to Check OS Kernel Version in Linux​?


Commonly Used grep Options (Quick Summary)

OptionPurpose
-iIgnore case sensitivity
-rSearch recursively
-nShow line numbers
-cCount matching lines
-vShow non-matching lines
-wMatch exact word
-oShow only matched text

Also Read: How to Search a File in Linux Using a Command?


Conclusion

The grep command is one of the most powerful and practical tools in Linux. It helps you search, filter, and analyze text quickly without having to manually read large files.

In this guide, you learned:

  • Basic usage of grep
  • Important options and their purpose
  • How to search inside directories
  • How to use regular expressions
  • Real-world practical examples

Once you start using grep regularly, it becomes an essential part of your Linux workflow. Practice with real log files and project directories, and you will soon master it.

grep may look simple, but it is extremely powerful when used correctly.