Viewing File Contents
To look inside a text file you don't need to open an editor. Every shell can print a file, show only its first or last lines, page through a long one, and count its lines. These commands are the everyday tools for reading logs, config files and data.
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Print a whole file | type notes.txt |
Get-Content notes.txt (gc, cat, type) |
cat notes.txt |
| Page through a long file | more notes.txt |
Get-Content notes.txt | more |
less notes.txt |
| First 10 lines | (no built-in) | Get-Content f -TotalCount 10 (-Head) |
head -n 10 f |
| Last 10 lines | (no built-in) | Get-Content f -Tail 10 |
tail -n 10 f |
| Follow a growing log | (no built-in) | Get-Content f -Wait -Tail 10 |
tail -f f |
| Line numbers | findstr /n "^" f |
Select-String '' f |
cat -n f, nl f |
| Count lines | type f | find /c /v "" |
(Get-Content f).Count |
wc -l f |
| Join files | copy /b a+b c / type a b > c |
Get-Content a, b | Set-Content c |
cat a b > c |
All the examples use the same small log file:
08:00 INFO server started
08:05 INFO user ada logged in
08:07 WARN disk 85% full
08:12 ERROR payment failed
08:15 INFO user ada logged outWindows (CMD)
type prints a file. For long files, more shows one screen at a time: Space for the next page, Enter for the next line, Q to quit. type has no options for the first or last lines, but a for /f loop reads a file line by line, so a counter gives you both the line count and the first lines:
@echo off
setlocal enabledelayedexpansion
(
echo 08:00 INFO server started
echo 08:05 INFO user ada logged in
echo 08:07 WARN disk 85%% full
echo 08:12 ERROR payment failed
echo 08:15 INFO user ada logged out
) > app.log
type app.log
echo --- first 2:
set n=0
for /f "usebackq delims=" %%l in ("app.log") do (
set /a n+=1
if !n! leq 2 echo %%l
)
echo lines: !n!08:00 INFO server started
08:05 INFO user ada logged in
08:07 WARN disk 85% full
08:12 ERROR payment failed
08:15 INFO user ada logged out
--- first 2:
08:00 INFO server started
08:05 INFO user ada logged in
lines: 5
Inside a batch file a literal % is written %%. The loop is explained in for /f: Parse Text. At the interactive prompt, the quickest line count is type app.log | find /c /v "" (count the lines that do not contain the empty string, which is all of them), and for the first or last lines many people simply call PowerShell: powershell -c "Get-Content app.log -Tail 2".
C:\Users\ada>more app.log
08:00 INFO server started
...
-- More (45%) --Tip:
typeis meant for text. Typing a binary file (an.exe, an image) prints garbage and may beep. The same is true forcatandGet-Content.
Windows (PowerShell)
Get-Content reads a file as an array of lines, so you can count them, index them, or take a slice:
@'
08:00 INFO server started
08:05 INFO user ada logged in
08:07 WARN disk 85% full
08:12 ERROR payment failed
08:15 INFO user ada logged out
'@ | Set-Content app.log
Get-Content app.log -TotalCount 2
'--- last 2:'
Get-Content app.log -Tail 2
'--- facts:'
$lines = Get-Content app.log
"lines: $($lines.Count)"
"third line: $($lines[2])"
"last line: $($lines[-1])"08:00 INFO server started
08:05 INFO user ada logged in
--- last 2:
08:12 ERROR payment failed
08:15 INFO user ada logged out
--- facts:
lines: 5
third line: 08:07 WARN disk 85% full
last line: 08:15 INFO user ada logged out
Use Get-Content -Raw to get the whole file as one string (useful for JSON), and -Encoding utf8 if a file shows strange characters. Get-Content app.log -Wait -Tail 5 keeps the file open and prints new lines as they're added, until you press Ctrl+C.
Linux & macOS (bash/zsh)
cat (from concatenate) prints one or more files. head and tail show the start and end, -n sets the number of lines (10 by default), and wc counts lines, words and bytes:
cat > app.log <<'EOF'
08:00 INFO server started
08:05 INFO user ada logged in
08:07 WARN disk 85% full
08:12 ERROR payment failed
08:15 INFO user ada logged out
EOF
head -n 2 app.log
echo "--- last 2:"
tail -n 2 app.log
echo "--- lines 2 to 3:"
sed -n '2,3p' app.log
echo "--- numbered:"
cat -n app.log | tail -n 1
wc -l app.log08:00 INFO server started
08:05 INFO user ada logged in
--- last 2:
08:12 ERROR payment failed
08:15 INFO user ada logged out
--- lines 2 to 3:
08:05 INFO user ada logged in
08:07 WARN disk 85% full
--- numbered:
5 08:15 INFO user ada logged out
5 app.log
tail -n +3 means "from line 3 to the end", which is handy for skipping a header line in a CSV file.
For long files use less, the standard pager on both Linux and macOS: arrows or Space to scroll, g/G to jump to the start or end, /word to search (n for the next match), q to quit. tail -f app.log follows a log as it grows; Ctrl+C stops it.
ada@ubuntu:~$ tail -f /var/log/syslog
Mar 12 09:41:02 ubuntu systemd[1]: Started Daily apt download activities.
Mar 12 09:41:07 ubuntu NetworkManager[812]: <info> [1741772467.1302] dhcp4 (wlp2s0): state changed
^C(Sample output — your log will differ. On macOS, system logs are read with log show or log stream instead.)
zsh extras
zsh can read a file straight into an array of lines with ${(f)"$(<file)"}: (f) splits on newlines and $(<file) reads the file without starting cat. Then the usual array slices apply (1-based):
print -l 'alpha' 'beta' 'gamma' 'delta' > words.txt
lines=( ${(f)"$(<words.txt)"} )
print "count: $#lines"
print "first two: $lines[1,2]"
print "last: $lines[-1]"count: 4
first two: alpha beta
last: delta
Viewing other kinds of files
- Hidden characters:
cat -A(Linux) shows tabs as^Iand line ends as$; Windows line endings appear as^M$. On macOS usecat -et. - Binary files:
xxd file | headorhexdump -C(Linux/macOS),Format-Hex file(PowerShell). - JSON:
jq . data.json(Linux/macOS, installjq),Get-Content data.json -Raw | ConvertFrom-Json(PowerShell). - Compressed logs:
zcatorzlessread.gzfiles without extracting them.
Learn more: cat · head, tail & wc · type & more · PowerShell Files & Folders
Test yourself
Which command shows the last 20 lines of app.log in PowerShell?
Get-Content app.log -Tail 20tail -n 20 app.logtype app.log /last:20Get-Content app.log -Last 20
What does tail -f server.log do?
- Shows the first lines
- Deletes the file after reading it
- Keeps printing new lines as they're added, until Ctrl+C
- Fixes broken lines
How do you quit less or more?
- Ctrl+Z
qexit- Esc