Operations 7 min read

Mastering Grep: 12 Powerful Linux Search Techniques

This guide walks through twelve practical grep and related command‑line techniques for Linux, covering how to list installed Python packages, filter configuration files, locate specific media files, display context lines, count matches, perform recursive and compressed‑file searches, and use extended or fixed‑string patterns with clear examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Grep: 12 Powerful Linux Search Techniques

1. List installed Python packages

List all Python‑related RPM packages on the system.
# rpm -qa | grep -i python
Sample output includes entries such as python-kitchen-1.1.1-5.el7.noarch , python2-pip-8.1.2-14.el7.noarch , and many others.

2. Search and filter files

Remove all comment lines from an Nginx configuration file.
# grep -v "#" /etc/nginx/nginx.conf
The -v option inverts the match, printing only lines that do not contain the # character.

3. Find only .mp3 files with multiple filters

Locate all .mp3 files, keep only those containing "jaychou" in the path, and exclude those containing "七里香".
# find . -name "*.mp3" | grep -i jaychou | grep -vi "七里香"

4. Show lines before or after a match

Use -A to display a number of lines after a match and -B for lines before a match.
# ifconfig | grep -A 4 eth0
# ifconfig | grep -B 2 UP

5. Print surrounding lines

The -C option prints a symmetric number of context lines around each match.
# ifconfig | grep -C 2 lo

6. Count matches

Count how many times a pattern appears without piping to wc .
# ifconfig | grep -c inet6

7. Search files with line numbers

Show the line numbers of occurrences, useful for debugging compilation errors.
# grep -n "main" setup..py

8. Recursive search in all directories

Search for a string throughout the current directory tree.
# grep -r "function" *

9. Search whole‑word patterns

Match only whole words using -w . For example, searching for "RUNNING" returns matching lines, while searching for "RUN" does not because it is not a whole word.
# ifconfig | grep -w "RUNNING"
# ifconfig | grep -w "RUN"

10. Search inside compressed files

zgrep works like grep on .gz files, using the same options.
# zgrep -i error /var/log/rumenz.gz

11. Use extended regular expressions

egrep (or grep -E ) enables additional metacharacters such as + , ? , | , and parentheses.
# grep -E "pattern" file

12. Search fixed‑string patterns

fgrep (equivalent to grep -F ) searches for literal strings without interpreting regular expressions, offering faster performance for exact matches.
# fgrep strcpy *.c
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.

SysadminSearchGrepcommand-line
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.