You've created files, moved them around, and cleaned up. Now you need to look inside them. Unix offers several commands for viewing file contents, each suited to a different situation.
cat โ quick peek
cat (short for concatenate) prints the entire file to the terminal. It's perfect for short files.
$ cat notes.txt
Buy groceries
Finish project
Call mom
For anything longer than a screen or two, cat becomes unwieldy โ the text scrolls past and you can't read it all.
less โ scroll and search
less shows you a file one screen at a time. You can scroll up and down and even search.
$ less longfile.txt
Once inside less:
- Space or
fโ forward one page bโ back one page/searchtermโ search for textqโ quit
The name "less" is a joke from the old days โ it's a better version of more, which only let you scroll forward. Less really is more.
head and tail โ just the edges
head shows the first 10 lines of a file. tail shows the last 10 lines.
$ head -n 20 data.csv
$ tail -n 5 app.log
Use -n to specify a different number of lines. head -n 100 shows the first 100 lines.
tail -f โ watch logs live
This is one of those Unix commands you'll fall in love with. tail -f follows a file as it grows โ new lines appear on screen as they're written.
$ tail -f /var/log/syslog
It's perfect for watching log files in real time. Press Ctrl+C to stop following. Every developer and sysadmin uses this daily โ it's how you see what your application is doing right now.
Which one to use?
catโ short files, quick checks, or piping to another commandlessโ long files you need to explorehead/tailโ when you only need the beginning or endtail -fโ watching logs or any live-updating file