Skip to main content

How to Show the First Lines of a File in the Terminal with head

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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:

Syntax
head -n <count> <file>
PartWhat 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:

Syntax
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:

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: Print the First Three Lines​

Terminal
head -n 3 sales.csv
Output
date,item,amount
2026-11-01,Tea,3.50
2026-11-01,Cake,4.25

The zsh terminal after head -n 3 sales.csv, which prints the header line date,item,amount and the first two sales.

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:

Terminal
head sales.csv
Output
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​

MistakeWhat happensDo this instead
A wrong name or folderhead: 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 folderhead: Error reading reportsGive it a file inside the folder
A negative count, such as head -n -2On macOS: head: illegal line count -- -2Use a positive number of lines

Troubleshooting​

SymptomLikely causeFix
Fewer lines than you asked forThe file is shorter than the countNothing to fix: head prints what there is
No such file or directoryThe file isn't in the current folderRun 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 -TotalCount and -Tail.
  • man head in your terminal — the manual page on macOS and Linux.