find & sort
Two more small text tools complete the CMD toolbox: find searches for a literal string (simpler than findstr, and handy for counting), and sort puts lines in order. Both read files or piped text, so they fit naturally into pipelines.
Note: CMD's
findsearches text inside files. It is not the Linuxfindcommand that searches for files by name; on Windows that job is done bydir /s /b *.txtorwhere /r.
find: lines containing a string
The search string must be in double quotes. When find reads a file, it prints a header line with the file name in capitals, then the matching lines:
@echo off
mkdir demo & cd demo
(
echo apple,3,fruit
echo Carrot,7,vegetable
echo banana,12,fruit
echo potato,5,vegetable
echo Cherry,9,fruit
) > produce.csv
find "fruit" produce.csv
---------- PRODUCE.CSV
apple,3,fruit
banana,12,fruit
Cherry,9,fruit
When the text comes from a pipe or from <, there is no file name, so there is no header either. That is the form scripts usually use:
@echo off
mkdir demo & cd demo
(
echo apple,3,fruit
echo Carrot,7,vegetable
echo banana,12,fruit
echo potato,5,vegetable
echo Cherry,9,fruit
) > produce.csv
type produce.csv | find "vegetable"
echo --- case-insensitive search for "c":
find /i "c" < produce.csvCarrot,7,vegetable
potato,5,vegetable
--- case-insensitive search for "c":
Carrot,7,vegetable
Cherry,9,fruit
find has no regular expressions and takes only one search string. For patterns or several words, use findstr.
find options
| Option | Meaning |
|---|---|
/i |
ignore case |
/v |
show lines that do not contain the string |
/c |
print only the count of matching lines |
/n |
prefix each line with its line number in brackets |
/off[line] |
don't skip files with the offline attribute |
/c on a file prints the header with the count:
@echo off
mkdir demo & cd demo
(
echo apple,3,fruit
echo Carrot,7,vegetable
echo banana,12,fruit
echo potato,5,vegetable
echo Cherry,9,fruit
) > produce.csv
find /c "fruit" produce.csv
---------- PRODUCE.CSV: 3
The other options, with output from a real Windows PC and the same produce.csv:
C:\Users\ada\demo>find /v "fruit" produce.csv
---------- PRODUCE.CSV
Carrot,7,vegetable
potato,5,vegetable
C:\Users\ada\demo>find /n "fruit" produce.csv
---------- PRODUCE.CSV
[1]apple,3,fruit
[3]banana,12,fruit
[5]Cherry,9,fruit
C:\Users\ada\demo>type produce.csv | find /c "fruit"
3Counting lines and files
find /c /v "" is a famous idiom: "count the lines that do not contain the empty string", which is every line. It is CMD's version of wc -l:
C:\Users\ada\demo>type produce.csv | find /c /v ""
5
C:\Users\ada\demo>dir /b /a:-d | find /c /v ""
1The second command counts the files in the current folder. To store a count in a variable, wrap the pipeline in for /f:
for /f %%n in ('type produce.csv ^| find /c /v ""') do set lines=%%n
echo produce.csv has %lines% linesLike findstr, find sets the exit code: 0 if something was found, 1 if not, so find "x" file >nul && echo yes works too.
sort: put lines in order
sort reads a file (or standard input) and prints its lines in order. The comparison is case-insensitive and alphabetic, following the Windows language settings:
@echo off
mkdir demo & cd demo
(
echo pear
echo Apple
echo banana
echo cherry
) > fruit.txt
echo --- A to Z:
sort fruit.txt
echo --- Z to A (/r):
sort /r fruit.txt--- A to Z:
Apple
banana
cherry
pear
--- Z to A (/r):
pear
cherry
banana
Apple
Numbers sort as text
sort compares characters from left to right, so 12 comes before 5 (because 1 < 5). Pad numbers with leading zeros, or put them in a fixed-width column, when you need numeric order:
@echo off
mkdir demo & cd demo
(
echo 5 potato
echo 12 banana
echo 3 apple
) > plain.txt
(
echo 05 potato
echo 12 banana
echo 03 apple
) > padded.txt
echo --- plain:
sort plain.txt
echo --- zero-padded:
sort padded.txt--- plain:
12 banana
3 apple
5 potato
--- zero-padded:
03 apple
05 potato
12 banana
Sorting by a column: /+n
/+n starts the comparison at character position n of each line (counting from 1). That sorts fixed-width text by any column:
@echo off
mkdir demo & cd demo
(
echo 2026-10-03 INFO backup done
echo 2026-10-01 WARN slow response
echo 2026-10-02 ERROR disk full
) > app.log
echo --- by date (column 1):
sort app.log
echo --- by level (column 12):
sort /+12 app.log--- by date (column 1):
2026-10-01 WARN slow response
2026-10-02 ERROR disk full
2026-10-03 INFO backup done
--- by level (column 12):
2026-10-02 ERROR disk full
2026-10-03 INFO backup done
2026-10-01 WARN slow response
Dates written as YYYY-MM-DD sort correctly as text, which is one reason that format is so popular in file names and logs.
Writing the result: /o and pipes
sort /o file writes the sorted lines to a file. Unlike sort data.txt > data.txt, which empties the file before sort can read it, /o may name the input file itself:
@echo off
mkdir demo & cd demo
(
echo zeta
echo alpha
echo mu
) > names.txt
sort names.txt /o names.txt
type names.txt
echo --- from a pipe:
dir /b /o:n | sort /ralpha
mu
zeta
--- from a pipe:
names.txt
| Option | Meaning |
|---|---|
/r |
reverse order |
/+n |
start comparing at column n |
/o file |
write output to file (may be the input file) |
/unique |
drop duplicate lines (Windows 10 and later) |
/c |
case-sensitive sort |
/t folder |
folder for temporary files when sorting huge inputs |
find & sort vs. other shells
| Task | CMD | PowerShell | bash |
|---|---|---|---|
| lines containing x | find "x" f |
Select-String -SimpleMatch x f |
grep -F x f |
| count matching lines | find /c "x" f |
(Select-String x f).Count |
grep -c x f |
| count all lines | find /c /v "" < f |
(Get-Content f).Count |
wc -l < f |
| sort lines | sort f |
Get-Content f | Sort-Object |
sort f |
| reverse / unique | sort /r, sort /unique |
-Descending, -Unique |
sort -r, sort -u |
Learn more: findstr: Search Text · Bash sort & uniq · PowerShell Filter & Sort
Test yourself
Which command counts all lines of data.txt?
- find /c data.txt
- type data.txt | find /c /v ""
- sort /c data.txt
- count data.txt
In which order does sort put the lines 5, 12 and 3?
- 3, 5, 12
- 12, 3, 5
- 5, 12, 3
- 12, 5, 3
How do you sort data.txt and save the result back into data.txt?
- sort data.txt > data.txt
- sort data.txt /o data.txt
- sort /r data.txt
- type data.txt | sort >> data.txt