Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Basic Command

grep -r "db_password" /var/www/example.com

Each result line starts with the file name, then a colon, then the matching line. Without a directory, grep -r searches the current one. Quote the pattern so the shell does not change it. grep is GNU grep on Ubuntu, Debian, AlmaLinux, and Rocky Linux, so the options below work on all of them.

-r or -R?

-r follows a symbolic link only when you name it on the command line. -R follows every symbolic link it meets, which can lead the search out of the tree or into the same files twice. Use -r unless you know you need the links followed.

Useful Options

OptionEffect
-nShow line numbers
-lOnly file names
-LFiles without a match
-iIgnore case
-wWhole words only
-cCount per file
-ISkip binary files
-FFixed string, no regex

Options combine: grep -rniw "timeout" /etc/nginx finds the whole word in any case, with line numbers. With -r, -c prints a line for every file, including those with no match; filter them out with | grep -v ':0$'.

Search Only Some Files

grep -rn --include='*.php' "mysqli_connect" .
grep -rn --include='*.js' --include='*.ts' "localStorage" src/

--include limits the search to file names that match a pattern, and you can repeat it. Quote the pattern so the shell does not expand it before grep sees it.

Skip Files and Directories

grep -rn --exclude='*.min.js' "api_key" .
grep -rn --exclude-dir=node_modules --exclude-dir=.git "TODO" .
grep -rn --exclude-dir={node_modules,.git,vendor} "TODO" .

The third line is a shortcut: bash expands the braces into three separate options. --exclude-dir matches directory names at any depth, so vendor skips every directory with that name.

Fixed Strings and Special Characters

Dots, brackets, stars, and dollar signs have a meaning in regular expressions. With -F, grep searches for the text exactly as written:

grep -rnF 'example.com/api?v=2' .
grep -rn -e '--force' scripts/

Single quotes keep the shell from touching the pattern, and -e marks a pattern that starts with a dash. To look for several words at once, see grep multiple strings.

Search Large Trees Sensibly

  • Start narrow: search /etc, /var/www, or a project directory, not /. The whole system includes /proc and /sys, which are not real files.
  • Name the real places: sudo grep -rIl "old-hostname" /etc /opt /home /var/www covers what usually matters.
  • Unreadable files: without sudo, grep reports "Permission denied" for files you cannot read. Use sudo, or add -s to hide those messages.
  • Less output, less work: -l stops reading a file at the first match, and -I skips binaries.
  • Filter by age or size first: find can choose the files and pass them to grep, as in find /var/www -name '*.php' -mtime -2 -exec grep -l 'eval(' {} +. See the find command.

A Faster Tool

For large code trees, ripgrep (the rg command, packaged as ripgrep on Debian and Ubuntu) searches recursively by default, skips binary files and paths listed in .gitignore, and is usually much faster than grep. For everything else about grep itself, from regular expressions to context lines, see the grep command guide.

Was this answer helpful?
Loading...