Skip to content
zero2vibecodelearn vibe coding
Level 2 · Working with content 5 min Reviewed by the author · Aug 4, 2026

Reading a file: cat

`cat` prints a file straight into the terminal. It is how you look inside something without opening an editor.

Level 2 · 0 / 4 lessons0%

You can create files. Now look inside one.

cat — print a file

cat shopping.txt
milk
bread
coffee

That is the whole command. cat reads the file and prints it into the terminal, then gives you your back.

Nothing is opened and nothing can be edited, which means nothing can be accidentally saved either. cat is purely a read.

Note

The name is short for concatenate, because its original job was joining files together. cat a.txt b.txt prints both in a row. Printing one file turned out to be the far more common use, and the name stuck.

Quick check

What is the difference between ls and cat?

cat -n — numbered lines

cat -n shopping.txt
     1	milk
     2	bread
     3	coffee

Numbers make it possible to point at a spot. “The bug is on line 42” is a sentence you can say to a colleague or to an AI agent, and both will know exactly where to look.

Quick check

Why would you add -n when reading a file?

When cat is the wrong tool

cat prints the whole file. On a three-line shopping list that is perfect. On a ten-thousand-line log it dumps everything and scrolls the useful part off the top.

For big files:

  • head file — the first ten lines
  • tail file — the last ten lines (usually what you want in a log)
  • less file — page through it a screen at a time

Both head and tail take -n to choose how many: head -n 3 file.

Tip

In less, press Space to go down a page and q to quit. Nobody discovers q on their own, and getting stuck in a pager is a genuinely common first-week experience. Now you know.

Quick check

You want the last 20 lines of a long log file. What do you run?

Try it

The folder has two small files in it.

  1. ls — see what is there.
  2. cat shopping.txt — read it.
  3. cat -n shopping.txt — read it again, numbered.

Then, if you like, try cat shopping.txt readme.txt to see both printed one after the other.

Check your understanding

Answered 0/2
Mode:

What does `cat notes.txt` do?

What is `cat -n` useful for?

Finish the task in the terminal and answer the questions to complete the lesson.

📝 My notes

Saved automatically in this browser · shows up on the Notes page.

Task

Read shopping.txt, then read it again with line numbers.

Tab complete · history · Ctrl+L clear · help commands

  • Not done: Print shopping.txt
  • Not done: You can see the file's contents
  • Not done: Print it again with line numbers

Lesson Q&A

What does `cat notes.txt` do?

cat prints and exits. Nothing is opened, nothing can be edited, nothing can be accidentally saved.

What is `cat -n` useful for?

Numbered lines make it possible to talk about a specific spot in a file — with a colleague or with an AI agent.

What is the difference between ls and cat?

ls answers 'what is here'. cat answers 'what is in this one'.

Why would you add -n when reading a file?

Line numbers are how people and tools agree on a location inside a file.

You want the last 20 lines of a long log file. What do you run?

tail reads from the end, and -n sets how many lines. In a log, the end is the part that just happened.

Move between lessons with · search with ⌘K