Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 91System · Linux

How to search text in many files

"Where is this setting configured?", "Which service uses the old database host?", "Which logs mention order 4711?" — grep finds text in thousands of files in seconds. This how-to searches whole folders with the right options (only some file types, skip .git, show context), searches gzipped logs and Git history, and shows ripgrep (rg), the faster modern tool.

  • 5 Lessons
  • 8 Commands

$ run as a normal user# run as root (or with sudo)

91.1

The quick answer

grep -rn 'db-old.example.com' /etc /opt/app
grep -rnI --include='*.conf' --exclude-dir=.git 'listen' /etc/nginx
grep -rl 'OLD_API_KEY' .
zgrep -h 'order=4711' /var/log/app/app.log*.gz

-r recursive, -n line numbers, -l only file names, -I skip binary files.

91.2

Step by step

  1. The options you need most:
OptionMeans
-r / -RSearch folders (-R also follows symlinks).
-nShow line numbers.
-iIgnore upper/lower case.
-wWhole words only (port but not export).
-l / -cOnly file names / count per file.
-EExtended regex (a|b, +, ?).
-FFixed text — no regex (fast, safe for dots and brackets).
-C 33 lines of context before and after.
--include='*.yml' / --exclude-dir=node_modulesLimit the files.
  1. Search config and code — exclude noise:
grep -rnI --exclude-dir={.git,node_modules,vendor} -E 'DATABASE_(HOST|URL)' /opt/app
grep -rnF '10.0.2.21' /etc
  1. Search logs with context, also rotated and compressed ones:
grep -n -C 3 'Out of memory' /var/log/messages
zgrep -c ' 502 ' /var/log/nginx/access.log*.gz
  1. Search the Git history — also in deleted code:
git grep -n 'OLD_API_KEY'
git log -S 'OLD_API_KEY' --oneline --all
  1. **ripgrep (rg)** — much faster on big trees, skips .gitignore files and binaries by default:
rg -n 'db-old.example.com' /opt/app
rg -n -t yaml 'image:' ./k8s
rg -l --hidden -g '!.git' 'SECRET'

Install: dnf install ripgrep (EPEL) / apt-get install ripgrep.

  1. Combine with find when you need file filters grep does not have (age, size):
find /var/log -name '*.log' -mtime -1 -print0 | xargs -0 grep -l 'ERROR'

91.3

Check that it worked

You get every file and line with the text — and an empty result really means "not there" (check the folder, the case with -i, and fixed text with -F before you trust "nothing found").

91.4

Common problems

ProblemCauseFix
Binary file … matchesgrep found it in a binary file.-I to skip binaries, or -a to treat as text.
Nothing found, but it is thereDots/brackets are regex characters, or case differs.-F for plain text; -i for case.
Permission denied lines everywhereFiles you can not read.sudo, or hide errors with 2>/dev/null.
Very slowSearching .git, node_modules, huge logs.--exclude-dir, --include, or rg.
Argument list too longgrep 'x' * with too many files.Use grep -r or find … | xargs.
Results from the wrong placeSymlinks or a mounted share.-r (not -R); limit the folder.

91.5

Go further

1Which option makes grep search for plain text instead of a regex?

-F (fixed strings).

2How do you search gzipped rotated logs?

All commands in this chapter