How to Show the First Lines of a File in the Terminal with head
A file can be too long to print whole, but you often only need its start: the header line of a CSV file, or the first entries of a log. head prints the first lines of a file and stops. In PowerShell, Get-Content does the same with -TotalCount.
Syntax
In zsh and bash:
head -n <count> <file>
| Part | What it means |
|---|---|
-n <count> | How many lines to print. Leave it out to get 10 |
<file> | The file to read. Give several to see the start of each |
The macOS manual page for head says it "displays the first count lines or bytes of each of the specified files", and "If count is omitted it defaults to 10."
In PowerShell:
Get-Content <file> -TotalCount <count>
Microsoft's documentation says "Beginning in PowerShell 3.0, Get-Content can also get a specified number of lines from the beginning or end of an item." It also lists -First and -Head as other names for -TotalCount.
The Example
A small CSV file, sales.csv, holds a header line and five sales. You print its first three lines: the header and the first two sales.
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: Print the First Three Lines
- macOS / Linux
- Windows (PowerShell)
head -n 3 sales.csv
date,item,amount
2026-11-01,Tea,3.50
2026-11-01,Cake,4.25
Get-Content sales.csv -TotalCount 3
date,item,amount
2026-11-01,Tea,3.50
2026-11-01,Cake,4.25

You got the header and two sales, and the other three lines were never printed. The file itself didn't change: head only reads. The bash command and output are the same as in zsh.
Step 3: Leave Out the Count
In zsh and bash, head without -n prints 10 lines:
head 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
sales.csv has only six lines, so you got all of them, with no error. PowerShell has no such default: Get-Content without -TotalCount prints the whole file.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| A wrong name or folder | head: missing.csv: No such file or directory. In PowerShell: Cannot find path '...missing.csv' because it does not exist. | Check the name with ls |
Giving head a folder | head: Error reading reports | Give it a file inside the folder |
A negative count, such as head -n -2 | On macOS: head: illegal line count -- -2 | Use a positive number of lines |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Fewer lines than you asked for | The file is shorter than the count | Nothing to fix: head prints what there is |
No such file or directory | The file isn't in the current folder | Run pwd and ls, or give the full path |
FAQ
How do I see the last lines instead?
Use tail the same way: tail -n 2 sales.csv prints the last two sales. In PowerShell, use Get-Content sales.csv -Tail 2.
What happens with several files?
head prints the start of each, under a header line with its name. head -n 1 sales.csv todo.txt prints ==> sales.csv <==, the first line of sales.csv, a blank line, ==> todo.txt <== and the first line of todo.txt.
Conclusion
head -n 3 sales.csv printed the header and the first two sales, and Get-Content sales.csv -TotalCount 3 did the same in PowerShell. Leave out -n and head prints 10 lines. Use it to check the start of a file without printing all of it.
Additional Resources
- Get-Content — PowerShell's command, with
-TotalCountand-Tail. man headin your terminal — the manual page on macOS and Linux.
