Fundamentals 4 min read

Master Linux Grep: Quickly Find Any Text Across Files and Directories

Learn how to use the powerful grep command on Linux to recursively search for specific text, filter results with options like whole‑word matching, case‑insensitivity, include/exclude patterns, and display line numbers, all illustrated with clear examples and command syntax.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Grep: Quickly Find Any Text Across Files and Directories

This guide explains how to use grep, a standard Unix utility, to locate files containing a specific string on a Linux system.

Start with a simple recursive search for the word linuxmi (case‑sensitive) in the current directory and all sub‑directories: grep -r 'linuxmi' The -r flag makes the search recursive; you can also use -R for the same effect.

To match only whole words, add the -w option: grep -rw 'linuxmi' If you want to limit the search to a particular directory, specify the path after the pattern: grep -rw 'linuxmi' /home/linuxmi/linuxmi Additional useful options include: -n – display line numbers for each match. -i – perform a case‑insensitive search. --include=GLOB – include only files matching the given glob pattern. --exclude=GLOB – exclude files matching the given glob pattern. --exclude-dir=GLOB – exclude directories matching the given glob pattern.

Combining these flags, you can, for example, search the ~/Documents tree for the word linuxmi (case‑insensitive), exclude the linuxmi and linuxmi.com sub‑directories, limit results to .txt and .js files, show line numbers, and follow symbolic links:

grep -Rni --exclude-dir={linuxmi,linuxmi.com} --include={*.txt,*.js} 'linuxmi' /home/linuxmi/www.linuxmi.com
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.

Searchtext processingGrepcommand-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.