Learn › Linux Foundations › I/O Redirection
>> - a hands-on Linux lab on a real virtual machine.
The single erases a file every time it writes. The double appends: it adds new lines to the end and never touches what is above. Build a three-line log in the practice terminal, learn the one-keystroke rule that separates growing a file from wiping it, then rebuild the log from memory.
Every Linux server keeps logs: files that grow line by line, for months, without losing a single entry. Yet the only file-writing arrow you know, the single > from the last lesson, ERASES the file every time it writes. Something does not add up. This short lesson closes that gap with one operator.
The black boxes below are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. On the real machine at the end of the module, the capstone mission builds this same signal folder and you grow these files for real.
First, feel the problem. You are in your signal folder. Put the word hi into a file called out.txt, using the single arrow you already know:
echo hi > out.txt
prompt: student@linuxcamp:~/signal$ answer: echo hi > out.txt output: hint: echo hi, a space, the single greater-than arrow, a space, the filename: echo hi > out.txt
Silence means it worked. Remember from the > lesson: the word went into the file instead of the screen. Now write to the same file a second time:
echo replaced > out.txt
prompt: student@linuxcamp:~/signal$ answer: echo replaced > out.txt output: hint: Same shape, new word, same file: echo replaced > out.txt
Two writes, one file. Before you look inside, decide: does out.txt now hold two lines or one?
prompt: student@linuxcamp:~/signal$ answer: cat out.txt output: replaced hint: cat prints a file's contents: cat out.txt
One line. The hi is gone for good, because > always starts the file fresh: erase everything, then write. That is fine for a file that should hold only the latest result. It is a disaster for a log. A log that forgets everything on every new entry is not a log at all. Linux clearly knows how to ADD to a file without wiping it, and the fix costs exactly one keystroke.
The append operator is >>, two greater-than signs pressed together. Append means add to the end. command >> file takes the command's output and writes it AFTER whatever the file already holds, touching nothing above it. And if the file does not exist yet, >> quietly creates it, just like > would.
One arrow replaces. Two arrows add. That is the whole lesson. The rest is putting it in your hands until it sticks.
Appending is as old as Unix, the system Linux grew out of: the very first Unix shell, written in the early 1970s, already understood both arrows. Fifty years later, the same two keystrokes are still how machines grow their logs.
You will build a small log now: three entries, one command each. The file is log.txt, and the opening entry should start it clean, so this first write uses the single > you already know:
echo first > log.txt
prompt: student@linuxcamp:~/signal$ answer: echo first > log.txt output: hint: One arrow to create the file with its opening line: echo first > log.txt
log.txt now holds one line, first. Every write from here on must protect that line.
Time for the new operator. Before you run this, decide: when the command finishes, will log.txt hold one line or two? With a single > you already know the sad answer, second would replace first. The double >> makes a different promise.
echo second >> log.txt
prompt: student@linuxcamp:~/signal$ answer: echo second >> log.txt output: hint: Two greater-than signs, side by side with no space between them: echo second >> log.txt
Silence again, so the write landed in the file. Hold your prediction. Add one more entry, then open the log and settle it.
The third entry, same doubled arrow:
echo third >> log.txt
prompt: student@linuxcamp:~/signal$ answer: echo third >> log.txt output: hint: Same double arrow, new word: echo third >> log.txt
Three writes so far: one >, then two >>. Time to see what survived.
cat log.txt
prompt: student@linuxcamp:~/signal$ answer: cat log.txt output: first second third hint: Read it back with cat: cat log.txt
All three lines, in the exact order you wrote them. Now put this next to the cold open. Two writes with > left out.txt holding one survivor. Three writes here left log.txt holding everything, because >> opens the file at the END and writes below what is already there. The first line was never touched by the later commands. One keystroke is the whole difference between a log and a lost cause.
Stop and notice what changed. At the start of this lesson, every write destroyed the file's past. Three commands later you built a file that only grows. That is a real capability: it is how shell scripts collect results into one report, how a notes file accumulates over weeks, and how every long-running Linux machine keeps its history.
Two rules to carry out of here:
> replaces, >> appends. One keystroke apart, opposite behavior.>> is the safe arrow. It never destroys anything, and if the file is missing it creates it. When in doubt about a file you care about, double the arrow.The classic slip is typing > when you meant >>, and it never announces itself. The command runs silently even as it wipes the file you meant to grow. Most engineers get bitten by this exactly once. Check your arrows before you press Enter.
One forward reference: later in this module you will meet tee, a tool that writes a stream to a file while passing it on. It even has an append switch of its own, tee -a, built on this same idea.
Scaffolding off. No command is shown from here to the end.
Disaster drill: pretend log.txt was deleted. Rebuild it from memory. First move: create the file so it holds exactly one line, the word first. It must start clean even if an old copy is still lying around, so pick your arrow carefully.
prompt: student@linuxcamp:~/signal$ answer: echo first > log.txt output: hint: echo the word into the file. One arrow starts a file fresh, and starting fresh is exactly what this drill asks for.
Now the entries. Add second to the end of the log without disturbing the line already inside it.
prompt: student@linuxcamp:~/signal$ answer: echo second >> log.txt output: hint: The word, then the arrow that adds instead of erases, then the file.
Then the final entry, third, the same way: onto the end, nothing lost.
prompt: student@linuxcamp:~/signal$ answer: echo third >> log.txt output: hint: Same doubled arrow. A single one here would erase first and second.
Last move: print the log and grade your own rebuild. If all three entries are there in order, first, second, third, the rebuild is perfect.
prompt: student@linuxcamp:~/signal$ answer: cat log.txt output: first second third hint: The same reading command you used to open the log earlier.
first, second, third: a perfect rebuild, from memory, with no commands shown. You now own both arrows: one to start a file, two to grow it. That pair sits behind every log file on every Linux server you will ever touch.
You earned this cheat sheet. Every row is something you just ran in the signal folder:
The one idea underneath: > opens a file at the beginning and wipes it. >> opens the same file at the end and preserves it. Everything else about the two operators is identical, the command, the spacing, the filename. Only the opening position changes, and that changes everything.
When you are about to redirect into a file that already matters, pause on the arrow. If losing the file's current contents would hurt, the answer is >>. Reserve the single > for files that are supposed to hold only the latest result.
The practice terminal walked you through the whole move: one > to start a log, >> to grow it entry by entry, and cat to prove nothing was lost. Then you rebuilt the whole log from memory with no commands shown.
The io-redirection module ends with one real Linux machine: the redirection mission lab. There a VM boots just for you with this same signal folder, and the mission hands you objectives with no commands shown. When an objective says grow a file without erasing it, you will know exactly which arrow to reach for.
Finish the other redirection lessons, then go grow some files for real.
Practice Append with >> in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.