Searching Files & Text
"Where is that file?" and "Which file mentions this?" are two different questions, and every shell has tools for both:
- Find files by name (or size, or date):
dir /s,Get-ChildItem -Recurse,find. - Find text inside files:
findstr,Select-String,grep.
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Find files by name, in all sub-folders | dir /s /b *.log |
Get-ChildItem -Recurse -Filter *.log |
find . -name '*.log' (zsh: ls **/*.log) |
| Lines containing a word | findstr "error" app.log |
Select-String error app.log |
grep error app.log |
| Ignore case | findstr /i |
(default) / -CaseSensitive to turn it off |
grep -i |
| Show line numbers | findstr /n |
(always shown) | grep -n |
| Lines that do NOT match | findstr /v |
Select-String -NotMatch |
grep -v |
| Search every file in a tree | findstr /s /i "todo" *.txt |
Get-ChildItem -Recurse | Select-String todo |
grep -rn todo . |
| Only the names of matching files | findstr /m |
Select-String … -List | % Path |
grep -l |
| Count matches | find /c "error" app.log |
(Select-String error app.log).Count |
grep -c error app.log |
| Regular expressions | findstr /r (basic) |
always (.NET regex) | grep -E |
All examples search this little project:
project/
├── notes.txt "Buy milk" / "TODO: call the bank"
├── docs/
│ ├── guide.md "# Guide" / "todo: write chapter 2"
│ └── old.log "error: disk full"
└── src/
└── app.py "print('hello') # TODO refactor"Windows (CMD)
dir /s /b with a pattern finds files by name in the current folder and all sub-folders. findstr searches inside files: /i ignores case, /v inverts the match, /n adds line numbers, /s searches sub-folders, /m prints only file names, and /c:"two words" searches for a phrase instead of separate words:
@echo off
mkdir project\docs project\src
cd project
(echo Buy milk& echo TODO: call the bank)> notes.txt
(echo # Guide& echo todo: write chapter 2)> docs\guide.md
echo error: disk full> docs\old.log
echo print('hello') # TODO refactor> src\app.py
echo --- files named *.md or *.py:
dir /b /o:n /s *.md *.py
echo --- TODO in notes.txt, any case:
findstr /i "todo" notes.txt
echo --- milk OR bank:
findstr "milk bank" notes.txt
echo --- the exact phrase:
findstr /c:"call the bank" notes.txt
echo --- count:
find /c /i "todo" notes.txt--- files named *.md or *.py:
C:\Users\learner\project\docs\guide.md
C:\Users\learner\project\src\app.py
--- TODO in notes.txt, any case:
TODO: call the bank
--- milk OR bank:
Buy milk
TODO: call the bank
--- the exact phrase:
TODO: call the bank
--- count:
---------- NOTES.TXT: 1
find /c (an older, simpler search tool) prints a header with the file name and the number of matching lines. On a real Windows machine the most useful findstr form searches a whole tree, with file names and line numbers (sample output):
C:\Users\ada\project>findstr /s /i /n "todo" *.*
notes.txt:2:TODO: call the bank
docs\guide.md:2:todo: write chapter 2
src\app.py:1:print('hello') # TODO refactor
C:\Users\ada\project>findstr /s /i /m "todo" *.*
notes.txt
docs\guide.md
src\app.pyNote:
findstr "milk bank"matches lines containing milk or bank: spaces separate alternatives. Usefindstr /c:"call the bank"for an exact phrase. Its regular expressions (/r) are limited; for real regex power use PowerShell'sSelect-String.
Windows (PowerShell)
Get-ChildItem -Recurse finds files; -Filter (fast) or -Include narrows them down. Select-String searches text with full regular expressions. It's case-insensitive by default and prints file:line:text:
New-Item -ItemType Directory project/docs, project/src -Force | Out-Null
Set-Location project
Set-Content notes.txt 'Buy milk', 'TODO: call the bank'
Set-Content docs/guide.md '# Guide', 'todo: write chapter 2'
Set-Content docs/old.log 'error: disk full'
Set-Content src/app.py "print('hello') # TODO refactor"
'--- *.md and *.py files:'
Get-ChildItem -Recurse -Include *.md, *.py -Name
'--- every TODO:'
Get-ChildItem -Recurse -File | Select-String 'todo' | Sort-Object Path |
ForEach-Object { '{0}:{1}: {2}' -f $_.Filename, $_.LineNumber, $_.Line }
'--- case-sensitive:'
(Get-ChildItem -Recurse -File | Select-String 'TODO' -CaseSensitive).Count--- *.md and *.py files:
docs/guide.md
src/app.py
--- every TODO:
guide.md:2: todo: write chapter 2
notes.txt:2: TODO: call the bank
app.py:1: print('hello') # TODO refactor
--- case-sensitive:
2
Each match is an object with Path, Filename, LineNumber, Line and Matches properties, which is why the example can format the result freely. On Windows you'd write the same command with the short aliases: gci -r | sls todo.
Linux & macOS (bash/zsh)
find walks a folder tree and tests each entry: -name (case-sensitive pattern), -iname (ignore case), -type f (files) or -type d (folders), -size +1M, -mtime -7 (changed in the last 7 days). Quote the pattern so the shell doesn't expand it first. grep searches text: -r recursive, -n line numbers, -i ignore case, -l file names only, -v invert, -c count, -w whole words:
mkdir -p project/docs project/src && cd project
printf 'Buy milk\nTODO: call the bank\n' > notes.txt
printf '# Guide\ntodo: write chapter 2\n' > docs/guide.md
echo 'error: disk full' > docs/old.log
echo "print('hello') # TODO refactor" > src/app.py
echo "--- files named *.md or *.py:"
find . -type f \( -name '*.md' -o -name '*.py' \) | sort
echo "--- every TODO, any case:"
grep -rni todo . | sort
echo "--- just the file names:"
grep -rli todo . | sort
echo "--- case-sensitive count per file:"
grep -rc TODO --include='*.txt' --include='*.py' . | sort--- files named *.md or *.py:
./docs/guide.md
./src/app.py
--- every TODO, any case:
./docs/guide.md:2:todo: write chapter 2
./notes.txt:2:TODO: call the bank
./src/app.py:1:print('hello') # TODO refactor
--- just the file names:
./docs/guide.md
./notes.txt
./src/app.py
--- case-sensitive count per file:
./notes.txt:1
./src/app.py:1
grep output from -r follows the order of the directory, which isn't guaranteed, so the example sorts it. Linux and macOS both have find and grep; GNU grep (Linux) and BSD grep (macOS) agree on all the options above.
zsh: recursive globbing
In zsh, **/ matches any number of sub-folders, so many find jobs become a simple pattern, and glob qualifiers add filters like (.) for files or (L+1) for non-empty files:
mkdir -p project/docs project/src && cd project
print 'TODO: call the bank' > notes.txt
print 'todo: write chapter 2' > docs/guide.md
print 'error: disk full' > docs/old.log
: > src/empty.py
print -l **/*.(md|py)
print -- ---
print -l **/*(.L+1)
print -- ---
grep -il todo **/*(.)docs/guide.md
src/empty.py
---
docs/guide.md
docs/old.log
notes.txt
---
docs/guide.md
notes.txt
Faster modern tools
On big codebases, ripgrep (rg) and fd are much faster than grep -r and find, skip .gitignored files automatically, and run on Windows, macOS and Linux (winget install BurntSushi.ripgrep.MSVC, brew install ripgrep fd, sudo apt install ripgrep fd-find):
ada@ubuntu:~/project$ rg -i todo
notes.txt
2:TODO: call the bank
docs/guide.md
2:todo: write chapter 2
src/app.py
1:print('hello') # TODO refactorGraphical search is also available from the terminal: mdfind "name" uses macOS Spotlight's index, and locate name (Linux) searches a prebuilt database of file names.
Learn more: grep · find · findstr: Search Text · find & sort · PowerShell Regex
Test yourself
Which bash command lists every file under the current folder whose name ends in .log?
grep '*.log' .find . -name '*.log'ls -r *.logdir /s *.log
Select-String in PowerShell is case-insensitive. How do you make it case-sensitive?
-i-CaseSensitive/c- It can't be
What does findstr "milk bank" notes.txt match?
- Lines containing the phrase "milk bank"
- Lines containing milk or bank
- Lines containing both words in any order
- Nothing; spaces are not allowed