Commandgrep

Search text in files recursively

Searches every file under a directory for a text pattern and prints the matching line number, so you can jump straight to it in an editor.

Last updated

Commandgrep
grep -rn "TODO" .

How it works

-r makes grep recurse into every subdirectory starting from the given path instead of only checking files you name explicitly; -n prefixes each match with its line number, which is what turns the output into something you can act on immediately (jump to that exact line) rather than just confirming a file contains the term somewhere. The search pattern comes first, the starting path last — quoting the pattern protects it from the shell if it contains spaces or characters that look like glob wildcards.

This is the everyday "where did I leave a TODO/FIXME/console.log" check across an entire project, needing nothing beyond grep itself.

Watch out for

  • grep's pattern is a regex by default (basic regular expressions unless you pass -E), so characters like ., *, [ have special meaning — escape them for a literal search, or add -F to force a fixed-string match with no regex interpretation at all.
  • GNU grep supports --include="*.js" and --exclude-dir=node_modules to scope a recursive search to specific files or skip noisy directories; older BSD grep (macOS's default before installing GNU coreutils) has more limited support for these — reach for ripgrep or GNU grep explicitly if you rely on heavy filtering.
  • Recursing through a tree that includes .git or node_modules is slow and produces a lot of irrelevant noise — pair -r with --exclude-dir=.git --exclude-dir=node_modules (GNU grep) to skip them.

Related in Commands