Skip to content

03. Filtering Stuffs Using grep

Overview

The grep command is used to search for specific patterns or strings within files or command outputs. It is one of the most powerful tools in Linux for:

  • Log analysis

  • Data filtering

  • Bug hunting and reconnaissance

  • Text processing


Basic Syntax

grep "pattern" filename

Explanation

  • grep → command used for searching

  • "pattern" → string or regex to search

  • filename → file to search in


Basic Example

grep "dynamic" file.txt

Explanation
Displays all lines in file.txt that contain the word dynamic.


Case Sensitivity

By default, grep is case-sensitive.

Example

grep "hello" file.txt
  • Matches hello

  • Does NOT match Hello or HELLO


Ignore Case (-i)

grep -i "hello" file.txt

Explanation

  • Matches hello, Hello, HELLO, etc.

  • Useful when case is unknown


Search in Multiple Files

grep "error" file1.txt file2.txt

Explanation
Searches for "error" in multiple files.


Recursive Search (-r)

grep -r "password" /path/to/directory

Explanation

  • Searches inside all files and subdirectories

  • Useful for finding sensitive data or keywords


Show Line Numbers (-n)

grep -n "main" script.sh

Explanation
Displays matching lines along with their line numbers.


Count Matches (-c)

grep -c "failed" log.txt

Explanation
Returns the number of matching lines.


Invert Match (-v)

grep -v "error" log.txt

Explanation
Shows lines that do not contain the pattern.


Exact Word Match (-w)

grep -w "admin" file.txt

Explanation
Matches only the exact word admin, not partial matches like administrator.


Using grep with Pipe (|)

Concept of Piping

Piping (|) allows output of one command to be used as input for another.


Example with cat

cat file.txt | grep "root"

Explanation

  • cat file.txt → outputs file content

  • grep "root" → filters lines containing "root"


Better Alternative

grep "root" file.txt

Note
Direct use of grep is more efficient than piping with cat.


Using grep with locate

Example

locate filename | grep "/path"

Explanation

  • locate filename → finds all matching files

  • grep "/path" → filters results based on path


Using grep with Other Commands

Example with ps

ps aux | grep apache

Explanation

  • Lists running processes

  • Filters processes related to apache


Example with netstat

netstat -tulnp | grep 80

Explanation

  • Shows network ports

  • Filters results for port 80


Regular Expressions (Basic Usage)

grep "^root" file.txt

Explanation

  • ^ → matches start of line

  • Finds lines starting with root


grep "error$" file.txt

Explanation

  • $ → matches end of line

  • Finds lines ending with error


Important Notes

  • Always enclose patterns in quotes " "

  • Use -i when case is uncertain

  • Use -r for directory-wide searches

  • Combine with other commands for powerful filtering


Summary Table

Option Description
-i Ignore case
-r Recursive search
-n Show line numbers
-c Count matches
-v Invert match
-w Exact word match

Conclusion

The grep command is a core tool in Linux widely used in:

  • Log analysis

  • Penetration testing

  • Automation scripts

  • System monitoring

Mastering grep significantly improves your ability to analyze and filter data efficiently in any Linux environment.