H
10Corp Premium Hosting

The grep Command in Linux

Last Updated: 2025-01-01 3 min read

Overview

grep (Global Regular Expression Print) searches files or input for lines matching a text pattern. It’s indispensable for finding configuration values, troubleshooting error logs, and filtering command output.

Basic Syntax

grep [options] "pattern" [file(s)]

Simple Searches

Search for a string in a file

grep "error" /var/log/syslog
grep -i "warning" /var/log/apache2/error.log

Show line numbers

grep -n "404" /var/log/apache2/access.log

Search through all files in a directory tree:

grep -r "database_name" /home/username/public_html/

Recursive with specific file types

grep -r --include="*.php" "DB_PASSWORD" /home/username/public_html/
grep -r --exclude-dir=".git" "TODO" /home/username/project/

Useful Flags Reference

FlagDescription
-iCase-insensitive matching
-nShow line numbers
-r / -RSearch recursively in directories
-lShow only filenames with matches (not the matching lines)
-cShow only a count of matching lines per file
-vInvert match — show lines that do not match
-wMatch whole words only
-A NShow N lines after each match
-B NShow N lines before each match
-C NShow N lines of context (before and after)
--includeOnly search files matching a pattern
--excludeSkip files matching a pattern
--exclude-dirSkip directories matching a pattern
-EExtended regex (same as egrep)

Using Regular Expressions

# Lines starting with "Error"
grep "^Error" logfile.txt

# Lines ending with a number
grep "[0-9]$" data.txt

# Match IP address pattern
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" access.log

# Match multiple patterns (OR)
grep -E "error|warning|critical" /var/log/syslog

Piping with grep

One of grep’s most powerful features is filtering the output of other commands:

# Find a running process
ps aux | grep "apache"

# Filter active network connections
netstat -tuln | grep ":80"

# Check environment variables
env | grep "PATH"

# Find specific entries in command history
history | grep "mysql"

Practical Hosting Examples

Find the database name in a WordPress config

grep "DB_NAME" /home/username/public_html/wp-config.php

Search error logs for 500 errors

grep -c "500" /var/log/apache2/access.log

Find which files contain a hardcoded domain

grep -rl "old-domain.com" /home/username/public_html/

View context around an error

grep -C 3 "Fatal error" /var/log/apache2/error.log

This shows 3 lines before and after each match, helping you understand the context of the error.

grep -rI "config" /home/username/public_html/

The -I flag skips binary files, keeping output clean.

Tips

  • Combine -rn for recursive search with line numbers — the most frequently used combo.
  • Use -l when you only need to know which files contain a match, not the matching lines themselves.
  • When piping ps aux | grep something, you’ll often see the grep process itself in the results. Add | grep -v grep to filter it out.
Tags: ssh linux grep text-search

Still need help?

Our support team is available 24/7 to assist you.