Writing Files in Terminal
Introduction: From Empty Files to Useful Content
In the previous course, you learned how to create empty files and folders from the terminal — tools like mkdir and touch gave you an empty canvas to work with. But an empty file isn't very useful by itself. In this lesson, you'll learn how to put real text into those files using nothing but the terminal.
A text fixture is a small file containing known example data that we create as a predictable starting point for an exercise or test.
Three tools do this job. printf, combined with >, writes a line into a file, creating it if needed. Combined with >>, printf adds a line without disturbing what's already there. A heredoc lets you pour many lines into a file at once — perfect for a log, a config, or a note. Along the way, you'll also use cat just enough to check your work. Everything you need — zsh, printf, and cat — is already installed in your CodeSignal terminal, so you can start typing right away.
Peeking at Files with cat
Before we write anything, we need a quick way to look inside a file and confirm what's there. That's the job of cat. Typing cat followed by a filename prints the entire contents of that file straight to your screen. It doesn't open an editor or ask any questions — it just reads the file from start to finish and shows you what it found.
We're introducing cat here only so you can check your work as you go. That matters a lot when you're working purely from the terminal — there's no file browser to double-click and peek inside, so a command that prints contents on demand becomes essential. cat has a lot more depth to it — different ways to read files and other tools that pair with it — but that full picture is the subject of the next unit. For now, think of cat as your quick "let's see what's actually in that file" command.
Let's set up a practice folder first. As a reminder from the previous course, mkdir -p creates a folder — and any missing parent folders — without complaining if it already exists:
Then move into it with cd:
Now let's create our very first file so cat has something to show us:
That command prints nothing to the screen — the text went into the file instead. To see it, ask cat:
cat printed exactly one line, first shopping item: coffee, confirming that the file exists and holds exactly what we expect. We'll unpack exactly how printf and > created that file in the next section — for now, just notice how cat gave us instant proof that the write worked.
