Lesson 2.2

Reading Files

8 minutes

Now that you can find files and see their details, let’s learn how to look inside them. There are three main commands for reading file contents, and each one has a different purpose.

cat — Show the Whole File

What it does: Prints the entire contents of a file to the terminal.

Try it now: Type cat ~/projects/my-app/README.md

You’ll see the full file contents printed out. cat is short for “concatenate” — it was originally designed to join files together, but it’s most commonly used to quickly view a file.

cat -n — With Line Numbers

Try it now: Type cat -n ~/projects/my-app/README.md

Adding -n puts a line number next to each line. This is helpful when you need to refer to a specific line — for example, when an error message says “problem on line 12.”

When AI Tools Use cat
When Claude Code or another AI assistant needs to read a file, it often runs cat filename. If you see this in the command log, the AI is reading that file to understand its contents before making changes.

When to Use cat

cat works best for small files — configuration files, READMEs, short scripts. For large files like logs, it dumps everything at once, which floods your terminal. That’s where head and tail come in.

head — See the Beginning

What it does: Shows the first 10 lines of a file (by default).

Try it now: Type head ~/projects/my-app/logs/error.log

You’ll see just the first 10 lines. This is useful when you want to quickly peek at a file without reading the whole thing.

head -n — Choose How Many Lines

Try it now: Type head -n 5 ~/projects/my-app/logs/error.log

The -n 5 flag tells head to show only the first 5 lines. You can use any number.

When it’s useful: Checking the top of configuration files, seeing the structure of a CSV, or previewing any large file.

tail — See the End

What it does: Shows the last 10 lines of a file (by default).

Try it now: Type tail ~/projects/my-app/logs/error.log

You’ll see just the last 10 lines. This is the most important command for working with log files, because the newest entries are always at the bottom.

tail -n — Choose How Many Lines

Try it now: Type tail -n 3 ~/projects/my-app/logs/error.log

This shows only the last 3 lines — perfect for checking the most recent errors.

Pro Tip: tail -f for Live Logs
Running tail -f logfile.log will "follow" the file — it keeps watching and prints new lines as they appear. This is extremely useful when you're running a development server and want to watch for errors in real time. Press Ctrl+C to stop.

Choosing the Right Command

Command Best For
cat Small files you want to read entirely
cat -n When you need line numbers
head Peeking at the start of large files
head -n 5 Just the first few lines
tail Checking recent entries in logs
tail -n 3 Just the last few lines
tail -f Watching a file update in real time

Practice

Try these commands in the terminal:

  1. cat ~/projects/my-app/README.md — read the project README
  2. cat -n ~/projects/my-app/package.json — view the config with line numbers
  3. head -n 5 ~/projects/my-app/logs/error.log — check the first 5 log entries
  4. tail -n 3 ~/projects/my-app/logs/error.log — check the 3 most recent errors
BlueBox Terminal