Operations 8 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
15 Powerful grep Tricks to Master Text Searching on Linux

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 file

2. 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 file

3. 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:

3

5. 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.txt

6. 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 function

7. 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 file

9. 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.log

Corresponding 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 file

11. Search inside compressed files

Search for the word critical inside a .gz archive: zgrep "critical" log.gz Sample output:

[CRITICAL] System failure at node-1

12. 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 service

13. 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 configurations

14. 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.255

Mastering these grep patterns enables efficient text searching, log analysis, and data extraction in everyday Linux workflows.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxregexShell scriptingGreptext search
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.