Reading Files with Purpose
Introduction: Four Questions You Can Ask a File
In the last lesson, you used cat for one simple job: printing a file back to you so you could confirm that a write worked. That's a fine use for cat, but it's only a sliver of what reading files in the terminal is really about. Once your files grow — a log with thousands of lines, a config with dozens of settings — printing everything is rarely the right move.
This unit is about asking better questions. Do you want to see the whole file, just the start, just the end, or just a count of how many lines it holds? Each question has its own tool: cat for everything, head for the beginning, tail for the end, and wc -l for a quick size check before you commit to printing anything. Like cat, all four of these come standard on any terminal, including the one in CodeSignal, so there's nothing to install — just commands to learn.
Showing Everything with cat (and Why That's Sometimes a Mistake)
As a quick reminder from the last lesson, cat reads a file from start to finish and prints the entire thing to your screen. There's no scrolling, no pagination — it just dumps everything at once. That behavior is exactly what you want when a file is short: a two-line note, a five-line config, anything small enough to fit comfortably on one screen. You saw this already when cat confirmed that your writes landed correctly.
But that same behavior turns into a problem the moment a file gets big. Picture a server log with 10,000 lines recording a full day of traffic. Running cat on that file floods your terminal with thousands of lines scrolling past faster than you can read, and by the time it stops, you're left scrolling back up, trying to find anything useful. cat isn't broken here — it's doing exactly what it's designed to do. The problem is that "print everything" was the wrong question to ask in the first place.
Let's set up a practice folder and build a small file to work with. It's short on purpose — short enough to read comfortably — but keep that 10,000-line log in mind as you go. Everything you learn here about when to reach for cat scales up directly to a file that size.
First, create the folder (-p also creates any missing parents):
Then move into it:
Now write two lines into notes.txt using the quoted heredoc pattern from the last lesson:
With the file in place, let's ask cat to show us everything in it:
Both lines printed instantly, with nothing cut off and nothing to scroll through. That's the entire point of cat — it's the right tool exactly when the whole file is small enough that "everything" is a reasonable amount to look at.
