head — Output the first lines of a file across all 5 shells
Equivalents in every shell
Worked examples
Show first 5 lines
Bash
head -n 5 file.txtPowerShell
Get-Content file.txt -TotalCount 5Show first lines of multiple files
Bash
head -n 3 *.txtPowerShell
Get-ChildItem *.txt | ForEach-Object { Get-Content $_ -TotalCount 3 }Skip the first N bytes
Bash
head -c 1024 filePowerShell
(Get-Content file -AsByteStream -TotalCount 1024)Gotchas
- PowerShell `-TotalCount` reads lines, while `-AsByteStream -TotalCount` reads bytes — be explicit about your unit.
- GNU `head -n -5` removes the last 5 lines (negative count); BSD head (macOS) does not support negatives.
- For very large files, `Get-Content` is line-by-line — use `[System.IO.File]::OpenRead` for byte-level streaming.
WSL & PowerShell Core notes
pwsh`Get-Content -TotalCount N` and the alternative `Get-Content | Select-Object -First N` differ in streaming: `-TotalCount` stops reading the file once N lines are seen (efficient on large files); `Select-Object -First N` triggers Get-Content to read the whole file before propagating Stop. Reach for `-TotalCount` for large-file scenarios.
WSL`head /mnt/c/path/to/file` works but goes through DrvFs — reading the first lines of a Windows-side file from WSL is ~5–10× slower than reading the same file from Windows directly. For interactive previewing of large Windows-side logs, `cmd.exe /c "more /e +0 file.log"` or Windows-side PowerShell `Get-Content` is faster.
Common tasks using head
- Count lines in a file
Get the line count of one or more files, recursively or in a single command.
- Find the largest files in a directory
Identify the top N largest files under a directory tree, sorted by size.
- Pick a random line from a file
Select one random line from a file — useful for fortune-style picks, A/B bucketing, and test sampling.