Operations 8 min read

Mastering Linux grep: Essential Options and Practical Examples

This guide explains the Linux grep command, detailing its purpose, all available options, and practical examples for searching text, counting matches, handling case sensitivity, recursive searches, and more, helping users master efficient text processing in the terminal.

Raymond Ops
Raymond Ops
Raymond Ops
Mastering Linux grep: Essential Options and Practical Examples

Introduction

The Linux grep command is a widely used text‑search tool that scans files for matching strings and prints the matching lines. Its name stands for “global regular expression print” and it supports regular expressions for powerful searching.

Options

-a, --text

: Treat binary files as text. -c, --count: Show the number of matching lines instead of the lines themselves. -e pattern, --regexp=pattern: Specify a pattern; multiple patterns are allowed. -f file, --file=file: Read patterns from a file, one per line. -i, --ignore-case: Ignore case differences. -l, --files-with-matches: Print only the names of files with matches. -n, --line-number: Prefix each matching line with its line number. -r, --recursive: Recursively search files in subdirectories. -v, --invert-match: Output lines that do **not** match the pattern. -x, --line-regexp: Match only whole lines. -w, --word-regexp: Match only whole words.

Pattern and Files

pattern

is typically a regular expression used to match the desired text. files can be one or more files or directories.

Common grep examples

Search for a specific string in a file or folder:

grep "hello" file.txt
grep "hello" folder/file.txt

Search for multiple strings on the same line: grep -E "hello|world" file.txt Ignore case differences: grep -i "hello" file.txt Show line numbers of matches: grep -n "hello" file.txt Perform a reverse search (show non‑matching lines): grep -v "hello" file.txt Show a range of lines around a match (e.g., 2 lines before and after):

grep -A 2 "hello" file.txt  # after
grep -B 2 "hello" file.txt  # before
grep -C 2 "hello" file.txt  # both

Search for a whole word: grep -w "hello" file.txt Count the number of matches: grep -c "hello" file.txt Search specific file types using wildcards or --include :

grep "hello" *.txt
grep "hello" --include "*.txt" folder/

Search recursively in subdirectories:

grep -r "hello" folder/
grep -R "hello" folder/

Force grep to treat binary files as text: grep -a "hello" binary_file.bin Exclude specific directories from the search: grep -r "hello" folder/ --exclude-dir=log/ Limit the search to the first N lines of a file:

grep -m 10 'hello' file.txt  # only first 10 lines

Show only the matching part of each line: grep -o 'hello' file.txt Show files that do **not** contain the pattern:

grep -L 'hello' file.txt
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.

LinuxregexGrepcommand-linetext search
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

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.