brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: grep -r --include and --exclude-dir search only the files you care about

Search
~/tips/tip-grep-include-exclude-dir

grep -rn --include='*.conf' --exclude-dir=.git 'listen' /etc/nginx

Limits a recursive grep to certain file types and skips folders like .git.

grep -r walks a whole folder tree, which is great until it also walks .git, node_modules and a few gigabytes of logs. Then you get a screen full of hits from files you never meant to search.

Two options fix that. --include=GLOB only searches files whose name matches the glob, and --exclude-dir=GLOB skips directories with a matching name:

grep -rn --include='*.conf' --exclude-dir=.git 'listen' /etc/nginx

That searches only the .conf files and skips any .git folder on the way. You can repeat both options, so --include='*.yml' --include='*.yaml' catches both spellings of a compose file. On an Unraid box I’d point this at /mnt/user/appdata to find which container config mentions a certain port.

The caveat is the quotes. Without them your shell may expand *.conf into the names of files in the current folder before grep ever sees it, and then the filter is suddenly something else. Also note that --include looks at the base name of a file, not at its path, so --include='nginx/*.conf' won’t do what you hope. For that kind of path filtering, find is the better tool.

Further reading: grep(1) manual page

Going deeper: Run degoog on Unraid as your own search page, compared with SearXNG and 4get

More Search tips !!wfr Wau Holland zipgrep db_host backup.zip pdfgrep -rin "warranty" ~/Documents "fatal error" repo:github-linguist/linguist is:open involves:@me All Search tips (11) All topics

Link