跳到正文
DEV Community· ShellProven·· 1 小時前AI 評分48

使用 grep -r 搜尋整個專案,同時排除 node_modules、.git 和二進制檔案

Search a whole project with grep -r, without node_modules, .git or binary junk

AI 導讀

使用 grep -r 搜尋整個專案時,可透過 --exclude-dir 排除 node_modules、.git 和建置資料夾,再用 -I 跳過二進制檔案。搜尋還可加入 -i 忽略大小寫、--include 限定檔案類型、-w 匹配完整詞語,以及 -F 將句點等符號視為純文字。

正文

AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (GNU grep 3.11); all outputs below are copied from that run, not written by hand.

Search a whole project for a string with grep -r, then cut the noise: skip node_modules, .git and build folders with --exclude-dir, ignore binary files with -I, search only one file type with --include, match whole words with -w, and stop dots from acting as wildcards with -F.

The problem: You want to find every to-do in your project. You type grep -r, and the screen fills with junk from node_modules, dot git, and minified files.

The noisy first search

Our demo project has source code in src, a dependency in node_modules, a dot git folder, a minified build in dist, a compiled binary, and a log file. Here is the search everyone types first.

$ grep -r "TODO" .
./src/client.js:// TODO: handle errors
./src/auth.py:    # TODO: rate limit
./.git/config:  # TODO nothing
./node_modules/left-pad/index.js:// TODO upstream bug
./dist/app.min.js:function a(){/*TODO*/return 1}
grep: ./bin/tool: binary file matches

Only two of these lines are our code. One comes from a dependency, one from git's own config, one from the minified build, and the last line is a binary file that happens to contain the text.

Skip folders with --exclude-dir

Let's skip the folders we never want. --exclude-dir takes a folder name, and you can repeat it. I'm also adding -n for line numbers.

$ grep -rn "TODO" --exclude-dir=node_modules --exclude-dir=.git .
./src/client.js:1:// TODO: handle errors
./src/auth.py:2:    # TODO: rate limit
./dist/app.min.js:1:function a(){/*TODO*/return 1}
grep: ./bin/tool: binary file matches

Better. The dependency and the git config are gone. Note that you give the folder name, not a path, so it skips node_modules at any depth.

Ignore binary files with -I

Two problems left: the build folder and the binary. Typing --exclude-dir again and again is tedious, so here's a shortcut. In bash, a comma list in curly braces expands into several options. And capital -I tells grep to skip binary files completely.

$ grep -rnI "TODO" --exclude-dir={node_modules,.git,dist} .
./src/client.js:1:// TODO: handle errors
./src/auth.py:2:    # TODO: rate limit

Two lines, both from our own code. That is the search you actually wanted. One detail: the curly-brace trick is a bash feature. If a script runs under plain sh, write the options out one by one.

Case, file types and file names

Did we miss anything? Let's add -i to ignore case.

$ grep -rniI "todo" --exclude-dir={node_modules,.git,dist} .
./src/main.py:2:# todo: remove debug flag
./src/client.js:1:// TODO: handle errors
./src/auth.py:2:    # TODO: rate limit

There was a lowercase to-do hiding in main dot py. Case-sensitive search had quietly skipped it. Sometimes you only care about one language. --include limits the search to matching file names, and -l prints just the file names, not the lines.

$ grep -rl "TODO" --include="*.py" .
./src/auth.py

Keep the quotes around the star. Without them, the shell may expand it before grep ever sees it.

Whole words with -w

Next trap. Let's look for the word log.

$ grep -rn "log" logs src
logs/app.log:1:2026 login ok
logs/app.log:2:2026 user log out
src/client.js:2:fetch("/api/catalog")
src/auth.py:1:def login(user):

We get login, catalog, and log out. grep matches text anywhere, even inside longer words. -w only accepts whole words.

$ grep -rnw "log" logs src
logs/app.log:2:2026 user log out

Now only the line where log is a word of its own.

Dots are wildcards: use -F

Let's find the version one point five.

$ grep -rn "1.5" src logs
src/main.py:4:VERSION = "1.5"
logs/app.log:3:version 105 started

Surprise: we also matched one hundred and five in the log. In a pattern, a dot means any single character. -F treats the pattern as plain text. Use it whenever you search for something with dots, brackets or stars in it.

$ grep -rnF "1.5" src logs
src/main.py:4:VERSION = "1.5"

Several patterns and some context

To look for several things at once, use -E and separate the patterns with a vertical bar.

$ grep -rnIE "TODO|FIXME|DEBUG" --exclude-dir={node_modules,.git,dist} .
./src/main.py:3:DEBUG = True
./src/client.js:1:// TODO: handle errors
./src/auth.py:2:    # TODO: rate limit

And when one line isn't enough to understand a match, -A 1 shows one line after it. Capital B shows lines before, and capital C both.

$ grep -n -A 1 "TODO" src/auth.py
2:    # TODO: rate limit
3-    return check(user)

The trap, in one paragraph

In a grep pattern the dot is a wildcard. Searching for 1.5 also matches 105. Add -F to treat the pattern as plain text: grep -rnF "1.5".

Quick check

1. I search for version 1.5 and grep also matches 105. Which option fixes that?

  • -w
  • -F
  • -i

Answer
-F: -F treats the pattern as plain text. Without it, the dot matches any character.

2. I search for the word log and also get login and catalog. Which option keeps only whole words?

  • -w
  • -F
  • -l

Answer
-w: -w accepts a match only when it is a word of its own.

Recap

grep -rnI searches recursively, with line numbers, and skips binaries. --exclude-dir removes folders like node_modules and dot git. Add -i for case, --include for one file type, -w for whole words, and -F when your text has dots in it.

Source and reproducible setup


This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.

來源:DEV Community · dev.to