vlogstack
Ask

How to find all files containing some text (grep)

SolvedTerminalCodeman · ep. 0312.3M views on Stack Overflow ↗updated Nov 23, 2026grepgrep-rfind-text-in-fileslinuxbash
Video cover: Find every file containing some text with grep
VideoThe direct link to this video is added here when it is public.

The problem

You need to know which files in a folder contain a piece of text, such as every TODO in a project, and on which line it appears.

Asked on Stack Overflow: Find all files containing a specific text (string) on Linux? (12,329,494 views as of Nov 23, 2026)

Solution

commands.sh
# every TODO, with line numbers
grep -rn "TODO" src
# only the file names
grep -rl "TODO" src
# only .js files
grep -rn "TODO" --include="*.js" src
# any case, whole word only
grep -rniw "todo" src

-r for folders, -n for line numbers, -l for names only.

Step by step

  1. Search the whole folder. grep -rn "TODO" src searches recursively and adds the line number to every match.
  2. Only the file names. grep -rl "TODO" src lists each matching file.
  3. One file type. Add --include="*.js" to keep only the file type you ask for.
  4. Any case, whole words. grep -rniw "todo" src: -i ignores case, -w matches whole words only.
What it looks like in a real run
# every TODO, with line numbers
$ grep -rn "TODO" src
src/style.css:1:/* TODO: dark mode */
src/lib/io.js:1:// TODO: handle errors
src/app.js:2:// TODO: add tests
# only the file names
$ grep -rl "TODO" src
src/style.css
src/lib/io.js
src/app.js
# only .js files
$ grep -rn "TODO" --include="*.js" src
src/lib/io.js:1:// TODO: handle errors
src/app.js:2:// TODO: add tests
# any case, whole word only
$ grep -rniw "todo" src
src/style.css:1:/* TODO: dark mode */
src/lib/io.js:1:// TODO: handle errors
src/notes.md:1:todo: write the docs
src/app.js:2:// TODO: add tests

Heads-up

Skip heavy folders with --exclude-dir=node_modules (or .git). The same flags work on Linux and macOS.

Errors this fixes

grep: src: Is a directory

You see it after grep "TODO" src. grep was given a folder but searches only files unless you ask it to go into folders.

Fix: Add -r to search the folder recursively: grep -rn "TODO" src.