Labs ICT
โญ Pro Login

Viewing Files

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.

Try it Yourself โ†’

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 text
  • q โ€” 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 command
  • less โ€” long files you need to explore
  • head / tail โ€” when you only need the beginning or end
  • tail -f โ€” watching logs or any live-updating file

๐Ÿงช Quick Quiz

Which command lets you view a file one screen at a time?