How do I exclude lines that match?
grep -v 'healthcheck' /var/log/nginx/access.log
-v inverts the match: every line that does not contain the pattern is printed. Combine it with a positive search through a pipe to keep some lines and drop others:
grep ' 50[0-9] ' /var/log/nginx/access.log | grep -v '/healthz'
This shows server errors except those from a monitoring endpoint that fails on purpose during tests.
How do I exclude several patterns at once?
grep -v -e 'healthcheck' -e 'kube-probe' -e 'UptimeCheck' access.log grep -vE 'healthcheck|kube-probe|UptimeCheck' access.log grep -v -F -f /etc/log-noise.txt access.log
Each -e adds a pattern; -E with | does the same in one extended regex; -f reads patterns from a file, one per line, and -F treats them as fixed strings. The file form keeps a growing noise list in one place, reusable across scripts.
How do I skip comments and blank lines?
grep -vE '^\s*(#|;|$)' /etc/php/8.3/fpm/php-fpm.conf
This prints only active settings, ignoring lines that start with # or ;, possibly after spaces, and empty lines. It is the fastest way to see what a long, heavily commented configuration file actually sets.
How do I exclude files and folders in a recursive search?
| Option | Effect |
|---|---|
--exclude='*.min.js' | Skip files whose name matches |
--exclude-dir=node_modules | Skip folders by name at any depth |
--exclude-dir={.git,vendor,cache} | Several folders, via shell brace expansion |
--exclude-from=FILE | Read file patterns from a file |
--include='*.php' | Search only matching files |
grep -rn 'DB_PASSWORD' /srv --exclude-dir={.git,node_modules,vendor} --exclude='*.log'
grep -rn --include='*.conf' --include='*.ini' 'memory_limit' /etc
--exclude-dir matches folder names, not paths, so --exclude-dir=cache skips every folder named cache anywhere in the tree. To skip one specific path but not others with the same name, combine find with -prune and pass the result to grep, as shown in The find command guide.
How do I keep binary files out?
By default grep reports "binary file matches" instead of printing lines from binaries. Skip them entirely with -I:
grep -rIn 'api.example.net' /opt/app
This avoids noise from images, archives and compiled files, and speeds up searches in trees with large binaries such as backups or VM images.
How do I exclude grep itself from process searches?
ps aux | grep '[n]ginx: worker' pgrep -af 'nginx: worker'
The bracket trick makes the pattern match the target but not the grep command line itself, since grep's own arguments contain the brackets. pgrep avoids the problem altogether and is the better habit.
Can I exclude with context lines?
Context options such as -A, -B and -C only apply to matching lines, so they do not combine meaningfully with -v. For "show errors with two lines of context, except known harmless ones", filter in two passes: grep -C2 'ERROR' app.log | grep -v 'cache warmup', accepting that context lines around a removed match may remain.
What about counts and multiple strings?
Counting non-matching lines is grep -vc, explained in Count matches with grep. Searching for several things at once, the positive side of the same tools, is in Grep for multiple strings.
How do I make exclusions permanent for a project?
Wrap them in a shell alias or function so every search uses them: alias gs='grep -rnI --exclude-dir={.git,node_modules,vendor,storage}' in ~/.bashrc gives a project-friendly search in two letters. For code trees with a .gitignore, ripgrep applies those rules automatically, which removes most manual excludes.
How do I exclude lines from a live log stream?
Pipe tail -F or journalctl -f through grep --line-buffered -v. The --line-buffered flag makes grep pass each line on immediately instead of waiting to fill a buffer, so filtered output appears in real time: journalctl -fu nginx | grep --line-buffered -v healthz.
Keep Reading
Count matches with grep
Count matching lines, every match, matches per file and per value with grep -c, -o and a few pipes, plus common counting traps.
OpenGrep for multiple strings
Match any of several patterns, all of them on one line, whole words from a list or strings in a set order, with grep and small pipelines.
OpenThe find command guide
A thorough guide to find: tests for name, type, size, time and owner, logical operators, pruning, actions, -exec forms and safe deletion.
OpenExclude files in rsync
Skip caches, logs and build folders with --exclude, --exclude-from and filter rules, and learn how anchored patterns really match.
OpenFAQ
Quick Answers
Use -v to print only lines that do not match the pattern.
Use several -e options with -v, or -vE with alternatives joined by |.
Add --exclude-dir with the folder name; it applies at any depth.
Use --include, for example --include='*.conf'.
Add -I to skip files grep detects as binary.