15 Powerful grep Tricks to Master Text Searching on Linux
This guide presents fifteen practical grep examples that demonstrate how to locate specific strings, perform case‑insensitive searches, recurse directories, count matches, list filenames, show line numbers, invert matches, display context, use regular expressions, exclude files, and combine grep with other commands for advanced text processing on Linux systems.
Overview
The grep command is a versatile tool for searching and filtering text in Linux. Below are fifteen concrete examples that illustrate common and advanced usages, each with the command syntax and sample output.
1. Find a specific string in a file
Search for all lines containing the word error in sample.log: grep "error" sample.log Sample output:
2023-10-13 09:32:22 - Error: Cannot connect to database
2023-10-13 10:02:45 - Error: Missing configuration file2. Case‑insensitive search
Use the -i flag to ignore case when matching error or ERROR: grep -i "error" sample.log Sample output:
2023-10-13 09:32:22 - Error: Cannot connect to database
2023-10-13 10:02:45 - ERROR: Disk space is full
2023-10-13 10:02:45 - Error: Missing configuration file3. Recursive search in a directory
Search all files under /path/to/logs for the word function: grep -r "function" /path/to/logs Sample output:
/path/to/logs/file1.log:35:Found function main()
/path/to/logs/file2.log:12:function add(a, b)4. Count matching lines
Count how many times the word total appears in sample.log: grep -c "total" sample.log Sample output:
35. List only filenames containing a match
Show names of .txt files that contain the word config: grep -l "config" *.txt Sample output:
file1.txt
file2.txt6. Show line numbers
Display matching lines together with their line numbers for the pattern main in app.py: grep -n "main" app.py Sample output:
10:def main():
65:# Call main function7. Invert match (show non‑matching lines)
Show all lines in /etc/nginx/nginx.conf that do **not** contain the character #: grep -v "#" /etc/nginx/nginx.conf Sample output (excerpt):
server {
listen 80;
server_name example.com;
}8. Exact word match
Use -w to match the whole word error only: grep -w "error" sample.log Sample output:
2023-10-13 09:32:22 - Error: Cannot connect to database
2023-10-13 10:02:45 - Error: Missing configuration file9. Show context lines
Display the matching line plus two lines after it ( -A 2), one line before it ( -B 1), or one line before and after ( -C 1).
# After context
grep -A 2 "error" sample.log
# Before context
grep -B 1 "error" sample.log
# Both sides
grep -C 1 "error" sample.logCorresponding outputs illustrate the surrounding lines.
10. Show only the matching portion
Use -o to print only the part of each line that matches the pattern: grep -o "ERROR.*" sample.log Sample output:
ERROR: Disk space is full
Error: Missing configuration file11. Search inside compressed files
Search for the word critical inside a .gz archive: zgrep "critical" log.gz Sample output:
[CRITICAL] System failure at node-112. Use regular expressions
Enable extended regex with -E (or egrep) to match lines starting with error or fail: grep -E "^(error|fail)" sample.log Sample output:
error: Configuration missing
fail: Could not start service13. Exclude certain file types while recursing
Search recursively but skip .log files: grep -r --exclude="*.log" "DEBUG" /path/to/logs Sample output:
/path/to/logs/script.py:DEBUG: Starting process
/path/to/logs/setup.py:DEBUG: Initializing configurations14. Fixed‑string match (no regex)
Use -F to treat the pattern as a literal string: grep -F ".*" sample.log Sample output (assuming the literal string .* appears in the file):
This is a test.*
error: Configuration missing.*15. Combine with other commands via pipelines
Pipe the output of ifconfig to grep to show the eth0 interface and the two lines that follow: ifconfig | grep -A 2 "eth0" Sample output:
eth0: flags=4163<UP,BROADCAST> mtu 1500
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255Mastering these grep patterns enables efficient text searching, log analysis, and data extraction in everyday Linux workflows.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
