• About
  • Editorial policy
  • Contact
  • Privacy
  • Legal
  • Cookie settings
Monday, November 2, 2026
  • Login
PacketNebula
  • Home
  • News
  • Tools
    • Network
    • Security
    • Developer
    • Sysadmin
    • SEO
    • Email & DNS
  • Guides
  • Trackers
    • LLM API pricing
    • Open-weight models
    • Retirements calendar
    • Infrastructure deals
  • About
  • Download
No Result
View All Result
PacketNebula
No Result
View All Result
Home Dev

How to search text in files with grep

by Stéphane Cardon
3 September 2026
in Dev
0
Close-up of a monitor displaying colorful JavaScript source code, shallow depth of field

Image: Markus Spiske / Wikimedia Commons, CC0

Share on FacebookShare on Twitter

Somewhere in this project sits the line that sets that value, and we’ve no idea which file. grep 'pattern' file searches one file, and grep -r 'pattern' . walks everything under the current folder, which covers most of what we reach for: where a function is defined, or which config is behind a setting. Logs too. A few flags do the heavy lifting. -i ignores case, -n adds line numbers, -l prints just the filenames that matched, and -v flips it to show the lines that don't match. It's already on every Linux and macOS box, nothing to install. Here's the everyday version, and how to grep a whole project without drowning in noise.

The short answer

grep 'pattern' file searches one file, grep -r 'pattern' . searches everything below the current folder. -i ignores case, -n adds line numbers, -l prints filenames only, -v inverts the match.

grep -rsearch a whole tree
-i / -nignore case / line numbers
-ljust the filenames
Answer card showing grep -r to search text across files, with -i, -n and -l flags.
One command finds where anything lives in code or logs. The flags shape the output.

Search one file, then a whole project

Linux
grep "error" app.log

Every line with error in it prints. Quote the pattern if it has spaces or shell characters in it, quoting never hurts anyway.

Linux
grep -rin "todo" src/

-r recurses into subfolders, -i ignores case (so TODO and todo both hit), -n puts the line number next to each match. This is the one I type without thinking. The rest are variations on it.

Just the filenames

Sometimes we don’t care about the lines at all, just which files to open next:

Linux
grep -rl "API_KEY" .
Terminal showing grep -rin finding TODO comments with line numbers, then grep -rln listing only the filenames.
Lines first, then just the files. -l is the difference.

The flags worth knowing

A short list carries you a long way. -w matches whole words only. -v shows the lines that don’t match, -c counts hits instead of printing them, -C 3 adds three lines of context around each match, and -F searches a literal string when the pattern has regex characters you’d rather have taken at face value. Five flags. That’s most of the grep we type in a normal week.

On a real project the flags that matter most are the ones that keep grep out of places you don’t care about. Searching a repository without excluding .git and node_modules is slow and the results are unusable.

Linux and macOS
grep -rn --exclude-dir={.git,node_modules,dist} 'TODO' .

A few more worth committing to memory. -w matches whole words, which stops id from hitting every width in the codebase. -l prints only the filenames and -L prints the files that don’t match, which is how you find the module somebody forgot to update. And -I skips binary files, so grep stops announcing that a match was found in a compiled artefact you can’t read anyway.

Frequently asked questions

How do I search every file in a folder and its subfolders?

Add -r (recursive): "grep -r pattern .". The dot just means start from here. We usually tack on -n for line numbers and -i to ignore case, so "grep -rin pattern ." ends up being the muscle memory version.

How do I show only the names of files that match?

Use -l: "grep -rl pattern .". You get one filename per file that contains the pattern instead of every matching line. That's the version we want when the next step is opening those files in an editor.

How do I search for a whole word, not a substring?

Use -w. Searching for cat normally also matches category and concatenate; "grep -w cat" only hits the standalone word. Pair it with -i when case doesn't matter.

grep treats my search term like a pattern and matches odd things. Why?

By default grep reads the term as a basic regular expression, so a dot or a star (or brackets) carries special meaning. Add -F (fixed) to search for a literal string: it finds the exact text and treats those characters as plain.

Tags: cliguidelinuxsearch
ShareTweet
Previous Post

How to download a file and test an API with curl

Next Post

How to find files with find on Linux

Stéphane Cardon

Stéphane Cardon

Network cybersecurity engineer: architecture, LAN and WLAN, system administration. He runs PacketNebula, where he publishes the tools and answers he wanted for his own work.

Next Post
Magnifying glass held over an old postage stamp collection, showing a Deutsche Post 1 Reichsmark stamp enlarged

How to find files with find on Linux

Free tools

  • IPv4 subnet calculator
  • DNS lookup
  • SSL certificate checker
  • HTTP headers checker
  • SPF, DKIM and DMARC checker
  • JWT decoder
  • Cron expression builder
  • Chmod calculator
  • Password generator

All 24 tools →

Guides to keep handy

  • Flush the DNS cache on Windows
  • Kill the process using a port
  • Generate an SSH key
  • Secure a new Ubuntu VPS
  • Find files on Linux
  • Extract and create tar.gz

More guides →

Who writes this

Stéphane Cardon, network cybersecurity engineer. Architecture, LAN and WLAN, system administration. PacketNebula holds the tools and answers he wanted for his own work.

About this site →

  • About
  • Editorial policy
  • Contact
  • Privacy
  • Legal
  • Cookie settings

Copyright © 2026 Stephane Cardon.

No Result
View All Result
  • Home
  • News
  • Tools
    • Network
    • Security
    • Developer
    • Sysadmin
    • SEO
    • Email & DNS
  • Guides
  • Trackers
    • LLM API pricing
    • Open-weight models
    • Retirements calendar
    • Infrastructure deals
  • About
  • Download

Copyright © 2026 Stephane Cardon.

Cookies, your call

We use Google Analytics to see which pages get read. Once our AdSense application is approved, articles and guides will carry Google ads. Nothing runs until you say yes, and nothing runs on the tool pages. Details