$ run as a normal user# run as root (or with sudo)
The quick answer
Step by step
- The options you need most:
| Option | Means |
|---|---|
-r / -R | Search folders (-R also follows symlinks). |
-n | Show line numbers. |
-i | Ignore upper/lower case. |
-w | Whole words only (port but not export). |
-l / -c | Only file names / count per file. |
-E | Extended regex (a|b, +, ?). |
-F | Fixed text — no regex (fast, safe for dots and brackets). |
-C 3 | 3 lines of context before and after. |
--include='*.yml' / --exclude-dir=node_modules | Limit the files. |
- 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- Search logs with context, also rotated and compressed ones:
- Search the Git history — also in deleted code:
- **ripgrep (
rg)** — much faster on big trees, skips.gitignorefiles 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.
- Combine with find when you need file filters grep does not have (age, size):
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").
Common problems
| Problem | Cause | Fix |
|---|---|---|
Binary file … matches | grep found it in a binary file. | -I to skip binaries, or -a to treat as text. |
| Nothing found, but it is there | Dots/brackets are regex characters, or case differs. | -F for plain text; -i for case. |
Permission denied lines everywhere | Files you can not read. | sudo, or hide errors with 2>/dev/null. |
| Very slow | Searching .git, node_modules, huge logs. | --exclude-dir, --include, or rg. |
Argument list too long | grep 'x' * with too many files. | Use grep -r or find … | xargs. |
| Results from the wrong place | Symlinks or a mounted share. | -r (not -R); limit the folder. |
Go further
- Command pages: grep, find, xargs, git.
- Next how-tos: Replace text in many files, Find files by name, size, date, Read and follow the logs of a service.
- Learn it: RHCSA — grep, RH134 — grep with regex, TLCL — grep, TLCL — xargs.
- Practise: Lab: get answers from text files and logs.
1Which option makes grep search for plain text instead of a regex?
-F (fixed strings).
2How do you search gzipped rotated logs?
zgrep (or zcat FILE | grep).