Fundamentals 9 min read

Master grep: Essential Linux Text Search Commands and Tips

This guide explains how to use the Linux grep command for powerful text searching, covering common options, file content searches, recursive scans, exclusion patterns, case‑insensitive matching, context lines, rule‑file searches, and regular‑expression techniques with practical examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master grep: Essential Linux Text Search Commands and Tips

Introduction

The article introduces grep , a fundamental Linux command for searching text within files and command output, complementing earlier tutorials on other Linux utilities.

Common Usage

Typical usage combines grep with a pipe ( |) to filter the output of previous commands. Example: $ ps -ef | grep redis This filters the process list to show only lines containing redis. The -v option excludes matching lines, and -c returns a count:

$ ps -ef | grep redis | grep -v auto
$ ps -ef | grep redis -c

Searching File Contents

To search within a specific file, use: $ grep "test" aaa/bbb/linux_command_debug.md Adding -n shows line numbers:

$ grep -n "test" aaa/bbb/linux_command_debug.md

Searching Multiple Files or Excluding Files

Search all .md files: $ grep -n "test" *.md Exclude files with --exclude or directories with --exclude-dir:

$ grep -rn "test" --exclude=*.txt
$ grep -rn "test" --exclude-dir=aaa

Multiple exclusion patterns can be stored in a file and referenced with --exclude-from:

$ grep -rn "test" --exclude-from=skip.txt

Finding Files Containing a Keyword

Recursively list files that contain a pattern using -l (or --file-with-matches):

$ grep -rln "int main(void)"

Finding Files Not Containing a Keyword

Use -L to list files that do not match the pattern:

$ grep -rLn "int main(void)"

Case‑Insensitive Search

Apply -i (or --ignore-case) to ignore case:

$ grep -rni "int MAIN(void)"

Showing Context Lines

Show lines before and after a match with -B and -A (or --before-context / --after-context):

$ grep -rn "int main(void)" -A 1 -B 1

Using a Rule File

Store multiple patterns in a file (e.g., key.txt) and search with -f:

$ cat filename | grep -f key.txt

Regular‑Expression Search

grep

supports regular expressions. For example, to find lines that start and end with t: $ grep -rn ^t.*t$ Extended regular expressions can be used via egrep.

Summary of Frequently Used Options

-v Show lines that do NOT match the pattern.

-l List files containing matches.

-L List files without matches.

-r Recursive search.

-i Ignore case.

-n Show line numbers.

-A n Show n lines after a match.

-B n Show n lines before a match.

--exclude Exclude specific files.

--exclude-dir Exclude specific directories.

-f Read patterns from a file.

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.

LinuxShellcommand-lineTutorialregextext 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.