Viewing Files

You don't always need a text editor to see what's in a file. In fact, most of the time you just want a quick look.

cat - Concatenate

The simplest file viewer:

Terminal
$cat notes.txt
These are my notes. Line two. Line three.

cat dumps the entire file to the screen.

Multiple Files

Terminal
$cat header.txt body.txt footer.txt
(outputs all three files in sequence)

That's actually why it's called "concatenate" - it joins files together.

With Line Numbers

Terminal
$cat -n script.sh
1 #!/bin/bash 2 echo "Hello" 3 exit 0

When cat Fails

cat is bad for large files - it dumps everything at once. If you cat a 10GB log file, you'll regret it.

less - Paginated Viewing

For larger files, use less:

Terminal
$less /var/log/syslog
(opens paged view)

Navigation in less:

  • Space / Page Down - next page
  • b / Page Up - previous page
  • g - go to beginning
  • G - go to end
  • / + text - search forward
  • ? + text - search backward
  • n - next search result
  • N - previous search result
  • q - quit

Less is More

There's also a more command, but less is better (yes, "less is more"). less can scroll backwards; more can't.

head - First Lines

See just the beginning of a file:

Terminal
$head config.json
(first 10 lines)
$head -n 5 config.json
(first 5 lines)
$head -5 config.json
(same thing)

Default is 10 lines. Use -n to specify how many.

tail - Last Lines

See just the end:

Terminal
$tail /var/log/syslog
(last 10 lines)
$tail -n 20 /var/log/syslog
(last 20 lines)

tail -f (Follow)

This is gold for watching logs in real-time:

Terminal
$tail -f /var/log/nginx/access.log
(stays open, shows new lines as they're added)

New log entries appear as they're written. Press Ctrl+C to stop.

Debugging in Production

When something's broken and you're watching logs:

hljs bash
tail -f /var/log/nginx/error.log

Then trigger the error in another window and watch what happens.

Combining head and tail

Get lines from the middle:

Terminal
$head -20 file.txt | tail -5
(lines 16-20)

Quick Comparison

CommandBest For
catSmall files, quick dumps
lessLarge files, navigation needed
headBeginning of files
tailEnd of files
tail -fWatching live logs

Modern Alternative: bat

bat is like cat but with syntax highlighting, line numbers, and git integration:

hljs bash
sudo apt install bat  # Then use: batcat (or bat on other distros)

Once you try bat, plain cat feels primitive.

Viewing Binary Files

What happens if you cat a binary file?

Terminal
$cat /bin/ls
(garbage characters, possible terminal corruption)

Your terminal might break. Reset it with:

Terminal
$reset
(restores terminal)

For binary files, use xxd or hexdump:

Terminal
$xxd /bin/ls | head
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
Knowledge Check

Which command would you use to watch a log file update in real-time?

Key Takeaways

  • cat for small files, quick viewing
  • less for large files with navigation
  • head -n X for first X lines
  • tail -n X for last X lines
  • tail -f for live log monitoring
  • Don't cat binary files - it corrupts your terminal

Next: identifying file types.