LearnLinux FoundationsReading Files

cat

cat - a hands-on Linux lab on a real virtual machine.

Read any short file with cat: dump it whole, number the lines with -n, expose the invisible newlines and tabs with -A (and spot a file with no final newline), and concatenate several files into one stream. Every drill runs in the practice terminal on the intercept fixture.

Almost everything on a Linux machine is a file: settings, logs, notes, scripts, raw data off the wire. The very first thing you ever want to do with a file is see what is inside it. The command for that is cat.

The name is short for concatenate, which means to join things end to end. That name will make sense later in this lesson. For now, the everyday job of cat is simpler: point it at one file and it prints that file's contents to your screen, top to bottom, and hands you back the prompt.

The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. It replays real output here. On the real machine at the end of the module, the capstone mission builds this same intercept folder and you run cat for real.

You are working an intercept station. Someone left a folder called intercept in your home directory, packed with captured logs and intel notes. Inside intercept/logs is a short boot log, messages.log. Point cat at it and read the whole thing. Type this exact command and press Enter:

cat ~/intercept/logs/messages.log

prompt: student@linuxcamp:~$ answer: cat ~/intercept/logs/messages.log output: line 1: system boot line 2: loading kernel modules line 3: network interface up line 4: mounting filesystems line 5: starting services line 6: user student logged in line 7: running diagnostics line 8: ready hint: Type cat, a space, then the path to the file: cat ~/intercept/logs/messages.log

cat dumped all eight lines of the boot log at once, then returned your prompt. That is its whole default job: give it a file, it prints every line and stops. For a short file like this, cat is the fastest possible look inside.

cat prints the entire file, start to finish, in one shot. That is exactly what you want for a short file you mean to read in full. It is the wrong tool for a giant one. Point cat at a log with fifty thousand lines and it fires every line past you faster than you can read, leaving you staring at the end with the top long gone.

That is not a flaw in cat, just its job. When a file is too long to read whole, other reading commands in this module handle it. head shows only the top, tail only the bottom, and less lets you page through it a screen at a time. Each of those has its own lesson. cat is the one you reach for when you want the whole short file in front of you at once.

A quick rule of thumb: if you expect the file to fit on your screen, cat it. If you are not sure how big it is, sample it first with head before you cat a possible monster.

cat has a handful of flags that change what it prints. The first is -n, short for number. cat -n <file> prints the file exactly as before, but puts a line number in front of every line. This is how you point someone at "the problem on line 2" without counting by hand.

There is a short poem in the intel folder, haiku.txt, three lines long. Number them:

cat -n ~/intercept/intel/haiku.txt

prompt: student@linuxcamp:~$ answer: cat -n ~/intercept/intel/haiku.txt output: 1 silent servers hum 2 packets cross the dark network 3 dawn brings fresh uptime hint: Put -n between cat and the path: cat -n ~/intercept/intel/haiku.txt

Each line now leads with its number, right-aligned in a little column, then a gap, then the original text. cat -n did not change the file at all; it only added the numbers to what it printed. The gap you see is a single tab character, a built-in spacer that lines the columns up. Numbering is most useful on config files and scripts, where you constantly need to name a specific line.

Every text line ends with an invisible newline character, the marker that tells the screen to drop to the next line. You never see it, but it is really there, one per line. Sometimes you need to. The -A flag, short for show all, reveals the characters that are normally invisible: it prints a $ wherever a newline sits, and shows tabs as ^I.

Run -A on the same haiku and watch the ends of the lines appear:

cat -A ~/intercept/intel/haiku.txt

prompt: student@linuxcamp:~$ answer: cat -A ~/intercept/intel/haiku.txt output: silent servers hum$ packets cross the dark network$ dawn brings fresh uptime$ hint: Swap -n for -A: cat -A ~/intercept/intel/haiku.txt

A $ now sits at the end of every line, marking each newline that was there all along. The text is unchanged; -A just made the invisible endings visible. There are no ^I markers here because this file has no tabs. -A is the tool you reach for when whitespace matters and your eyes cannot see it, like hunting a stray tab in a config file.

Here is the subtle part, and it is worth meeting on purpose. A file's last line does not have to end in a newline. It usually does, but nothing forces it. When the final line has no newline, -A shows you exactly that: there is simply no $ at the end, and your next prompt appears right up against the text.

The notes.md file in the intel folder is a single line with no newline on the end. Run -A on it and look closely at where the prompt lands:

cat -A ~/intercept/intel/notes.md

prompt: student@linuxcamp:~$ answer: cat -A ~/intercept/intel/notes.md output: quick note: rotate the keys at 0600 hint: Same flag, different file: cat -A ~/intercept/intel/notes.md

No $ at the end, and on a real terminal the prompt would sit on the same line as 0600 instead of dropping below it. That missing $ is -A telling you the file has no final newline. This is a real gotcha: tools that count lines by newlines will treat this one-line file as having zero complete lines. Seeing it with cat -A is how you catch it.

Now the name pays off. Give cat more than one file and it prints them one after another, in the order you list them, as a single continuous stream. That is concatenation, joining end to end, and it is where the name comes from.

Print the haiku and then the boot log, in that order, with one command. List the two paths after cat, separated by a space:

cat ~/intercept/intel/haiku.txt ~/intercept/logs/messages.log

prompt: student@linuxcamp:~$ answer: cat ~/intercept/intel/haiku.txt ~/intercept/logs/messages.log output: silent servers hum packets cross the dark network dawn brings fresh uptime line 1: system boot line 2: loading kernel modules line 3: network interface up line 4: mounting filesystems line 5: starting services line 6: user student logged in line 7: running diagnostics line 8: ready hint: List both paths after cat, with a space between them: cat ~/intercept/intel/haiku.txt ~/intercept/logs/messages.log

The three haiku lines came first, then all eight log lines, with no gap or divider between them: one continuous stream, in the order you named the files. That is concatenation, and it is the trick the name promises. Swap the order of the two paths and the two files trade places in the output.

Scaffolding off. No command is shown this time. You have every piece you need.

You want to print the boot log, ~/intercept/logs/messages.log, with a number in front of every line, so you can talk about "line 6" without counting. Pick the flag that numbers lines and aim cat at the log.

prompt: student@linuxcamp:~$ answer: cat -n ~/intercept/logs/messages.log output: 1 line 1: system boot 2 line 2: loading kernel modules 3 line 3: network interface up 4 line 4: mounting filesystems 5 line 5: starting services 6 line 6: user student logged in 7 line 7: running diagnostics 8 line 8: ready hint: You want line numbers, so use -n, then the path to the log: cat -n ~/intercept/logs/messages.log

Eight lines, each led by its own number. You chose -n from memory and pointed it at the right file, no prompt in front of you. That is exactly the move the real machine will ask of you: know what you want, pick the flag, type it.

You earned this cheat sheet. Every row is a form of cat you have already run:

The one rule to carry away: cat prints the whole file. That makes it the right first read on anything short, and the wrong one on anything huge, where head, tail, or less (each its own lesson) take over. Add -n to number lines, -A to expose the invisible whitespace, and list more than one file to concatenate them.

When a script or config file misbehaves for no visible reason, cat -A is a quiet hero: it shows the stray tab or the missing final newline your eyes slid right past.

The practice terminal has shown you every form of cat. The reading-files module ends with one real Linux machine, and that is where you read files for real.

In the reading-files capstone mission, a Linux VM boots just for you with this same intercept folder already built: the same logs and intel notes. Its objectives use exactly what you just ran: plain cat to dump a file, -n to number lines, -A to reveal the invisible endings, and several files at once to concatenate them. One difference from here: the lab shows no commands. You read the objective, recall the flag, and type it. That recall is what makes it stick.

Finish the other reading-files lessons, then go read the intercept for real.

Practice cat in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.