Skip to main content

How to Count the Lines in a File in the Terminal with wc -l

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

How many rows does a CSV file have, or how long has a log grown? wc -l counts the lines in a file and prints the number. In PowerShell, (Get-Content <file>).Count does the same.

Syntax​

In zsh and bash:

Syntax
wc -l <file>
wc -l < <file>
PartWhat it means
wcThe counting command. On its own it prints lines, words and bytes
-lCount lines only
<file>The file to count. wc prints the count and the file's name
< <file>Sends the file to wc as its input instead, so it prints only the number

The macOS manual page for wc says it "displays the number of lines, words, and bytes contained in each input file", and that "A line is defined as a string of characters delimited by a ⟨newline⟩ character."

In PowerShell:

Syntax
(Get-Content <file>).Count
PartWhat it means
Get-Content <file>Reads the file, one item per line
( ... )Runs the command first, so you can use its result
.CountThe number of items: here, the number of lines

Microsoft's documentation for Get-Content says that for files, "the content is read one line at a time and returns a collection of objects, each representing a line of content." about_Arrays calls Count "the most commonly used property to determine the number of items in any collection".

The Example​

A small CSV file, sales.csv, holds a header line and five sales. You count its lines, print the count without the name, and then see how wc -l counts a file whose last line has no newline.

Step 1: Create the File​

Save these six lines as sales.csv in the folder your terminal is in:

sales.csv
date,item,amount
2026-11-01,Tea,3.50
2026-11-01,Cake,4.25
2026-11-02,Tea,3.50
2026-11-02,Coffee,3.80
2026-11-03,Cake,4.25

Step 2: Count the Lines​

Terminal
wc -l sales.csv
Output
6 sales.csv
Terminal
wc -l < sales.csv
Output
6

The zsh terminal after wc -l sales.csv, which prints 6 and the file name, and wc -l < sales.csv, which prints only 6.

The file has 6 lines: the header and five sales. With < sales.csv, the shell hands the file to wc as its input, so wc never sees a name and prints only the number, which is easier to use in a script. The spaces before the 6 are how macOS's wc lines up its numbers. bash printed the same counts. PowerShell's .Count prints only the number.

Step 3: Count a File Whose Last Line Has No Newline​

Every line of sales.csv ends with a newline character, the invisible character that ends a line. A file can stop without one. This command writes two lines, first and second, with no newline after second, and counts them:

Terminal
printf 'first\nsecond' > no-final-newline.txt
wc -l no-final-newline.txt
Output
1 no-final-newline.txt

You can see two lines, but wc -l counted 1, because it counts newline characters. The manual page says: "Characters beyond the final ⟨newline⟩ character will not be included in the line count." PowerShell's (Get-Content no-final-newline.txt).Count printed 2 for the same file, because Get-Content also returns the last piece as a line.

Common Mistakes​

MistakeWhat happensDo this instead
A wrong name or folderwc: missing.csv: open: No such file or directoryCheck the name with ls
Trusting PowerShell's number after an errorFor a missing file, it prints the error and then 0Read the error above the number and fix the path
Comparing wc -l with what an editor showsThe counts differ by one when the last line has no newlineEnd the file with a newline, or remember that wc -l counts newlines

Troubleshooting​

SymptomLikely causeFix
A count one lower than the lines you seeThe last line has no newline at its endSee Step 3
No such file or directoryThe file isn't in the current folderRun pwd and ls, or give the full path
Three numbers instead of one-l was left outAdd -l

FAQ​

What does wc print without -l?​

Three numbers and the name: lines, words and bytes. wc sales.csv printed 6 6 122 sales.csv: 6 lines, 6 words and 122 bytes. Each line counts as one word here because no line has a space in it.

How many sales does sales.csv hold?​

Five. The count includes the header line, so subtract one for the rows of data.

Conclusion​

wc -l sales.csv counted 6 lines, wc -l < sales.csv printed the number alone, and (Get-Content sales.csv).Count gave 6 in PowerShell. wc -l counts newline characters, so a last line without one isn't counted.

Additional Resources​

  • Get-Content — PowerShell's command for reading a file.
  • about_Arrays: Count — the Count property.
  • man wc in your terminal — the manual page on macOS and Linux.