Skip to content
shellmap

Count lines in a file

Get the line count of one or more files, recursively or in a single command.

How to count lines in a file in each shell

Bashunix
wc -l file.txt
Zshunix
wc -l file.txt
Fishunix
wc -l file.txt
PowerShellwindows
(Get-Content file.txt | Measure-Object -Line).Lines

For huge files, `[System.IO.File]::ReadLines("file.txt") | Measure-Object` is faster than `Get-Content`.

cmd.exewindows
find /c /v "" file.txt

`/v ""` matches "lines not containing nothing" — i.e. all lines. `/c` counts them. Yes, that is the canonical cmd one-liner.

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • `wc -l` counts **newline characters**, so a file whose last line is missing `\n` reports one fewer than the visible line count.
  • For a directory total: `wc -l *.txt | tail -1` (bash); `Get-ChildItem *.txt | Get-Content | Measure-Object -Line` (PowerShell).
  • cmd `find /c /v ""` includes the final unterminated line in its count — its arithmetic differs from `wc -l` by exactly one on files without a trailing newline.
  • On binary files `wc -l` returns 0 silently; PowerShell `Get-Content` will decode bytes as text and may produce misleading counts. Strip binaries first.

Related commands

Related tasks