Thuta Learning
BasicDevOps & Toolsbeginner

Reading Files with cat, less, head, tail

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand cat, less, head, and tail for reading files, without the intimidation
  • Get comfortable trying out commands yourself in the terminal
  • See how these commands are actually useful on a real server or project

Let's think about it this way for a second

cat (concatenate) prints an entire file to the terminal all at once — best suited for short files. less is for larger files — you can scroll through page by page and search for text with / (press q to quit). head shows the first 10 lines (by default) of a file, and tail shows the last 10 — if you want a quick look at 'what just happened' in a log file, tail is your best bet. tail -f is the superpower flag for 'live' watching a log file as it grows.

Let's connect this to a real-world scenario

To peek at a small config file, just type cat /etc/hostname. For a large log file, you can page through it with less /var/log/syslog — using cat on a big file can flood the screen and get messy fast. After deploying an app on a server, tail -f app.log becomes your go-to tool for live-monitoring errors.

Let's try it together in the terminal

bash
cat /etc/hostname
less /var/log/syslog
head -n 5 notes.txt
tail -n 5 notes.txt
tail -f app.log      # Ctrl+C နဲ့ ရပ်
You should see
The contents of the hostname file print out as a single line in the terminal.

5-minute try-it

Create a file with 20 lines, then run head -n 3 and tail -n 3 on it. Write down how the results differ.

A quick word of caution

Running cat on a binary file (like an image) can spew strange characters into your terminal — if it looks broken, just open a new terminal, no harm done.

Easy traps

  • Using cat on large files and ending up with a confusing wall of text overflowing the screen
  • Not knowing how to exit less and closing the whole terminal instead — just press q

Now try it yourself

Create a file with 20 lines, then run head -n 3 and tail -n 3 on it. Write down how the results differ.

You'll know it worked when: The contents of the hostname file print out as a single line in the terminal.

Reading Files with cat, less, head, tail | Thuta Learning