How to Count the Lines in a File in the Terminal with wc -l
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:
wc -l <file>
wc -l < <file>
| Part | What it means |
|---|---|
wc | The counting command. On its own it prints lines, words and bytes |
-l | Count 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:
(Get-Content <file>).Count
| Part | What it means |
|---|---|
Get-Content <file> | Reads the file, one item per line |
( ... ) | Runs the command first, so you can use its result |
.Count | The 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:
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
- macOS / Linux
- Windows (PowerShell)
wc -l sales.csv
6 sales.csv
wc -l < sales.csv
6
(Get-Content sales.csv).Count
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:
printf 'first\nsecond' > no-final-newline.txt
wc -l no-final-newline.txt
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
| Mistake | What happens | Do this instead |
|---|---|---|
| A wrong name or folder | wc: missing.csv: open: No such file or directory | Check the name with ls |
| Trusting PowerShell's number after an error | For a missing file, it prints the error and then 0 | Read the error above the number and fix the path |
Comparing wc -l with what an editor shows | The counts differ by one when the last line has no newline | End the file with a newline, or remember that wc -l counts newlines |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A count one lower than the lines you see | The last line has no newline at its end | See Step 3 |
No such file or directory | The file isn't in the current folder | Run pwd and ls, or give the full path |
| Three numbers instead of one | -l was left out | Add -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
Countproperty. man wcin your terminal — the manual page on macOS and Linux.
