Lesson 5.1

Redirection

7 minutes

Sending Output to Files

Every command you’ve run so far has printed its output to the terminal screen. That’s called standard output (or stdout). Redirection lets you take that output and send it somewhere else — usually into a file.

There are two redirection operators you need to know, and the difference between them is critical.

The > Operator — Write (and Overwrite)

The > operator takes the output of a command and writes it to a file. If the file doesn’t exist, it creates it. If the file already exists, it completely overwrites the contents.

Try it now: Type these commands one at a time:

echo "Hello" > ~/test.txt
cat ~/test.txt

You just created a file called test.txt in your home directory containing the word “Hello”. The cat command confirms it.

Now try this:

echo "Goodbye" > ~/test.txt
cat ~/test.txt

Notice what happened? The file now says “Goodbye” — the word “Hello” is gone forever. The > operator replaced the entire file contents.

Warning: > Is Destructive
The > operator silently overwrites files without asking for confirmation. If you run echo "oops" > important-config.json, your config file is gone — replaced with the word "oops". There is no undo. When an AI tool suggests a command with >, make sure you understand what file it's writing to.

The >> Operator — Append

The >> operator also writes output to a file, but instead of overwriting, it adds to the end of the existing content.

Try it now:

echo "Line 1" > ~/test.txt
echo "Line 2" >> ~/test.txt
echo "Line 3" >> ~/test.txt
cat ~/test.txt

You should see all three lines. The first command used > to create the file (or overwrite it), and the next two used >> to add lines without erasing what was already there.

The Key Distinction

Operator Behavior Safe for existing files?
> Creates or overwrites No — destroys existing content
>> Creates or appends Yes — adds to the end

This distinction matters every time you see these operators in a command. Always pause and ask: do I want to replace what’s in this file, or add to it?

When AI Tools Use Redirection

AI coding tools use > frequently. Some common patterns:

  • Creating config files: echo '{"name": "my-app"}' > package.json
  • Writing code to files: generating a file by redirecting output
  • Saving command output: ls -la > file-list.txt to capture a directory listing

The append operator >> shows up when tools add to existing files:

  • Adding to logs: echo "Build completed" >> build.log
  • Appending config lines: echo "export PATH=..." >> ~/.bashrc

Redirecting Any Command’s Output

It’s not just echo that works with redirection. Any command’s output can be sent to a file:

Try it now:

ls -la ~/projects/my-app > ~/project-listing.txt
cat ~/project-listing.txt

That saved the full directory listing to a file instead of printing it on screen.

Practice

Try these commands in the terminal:

  1. echo "My first file" > ~/practice.txt — create a file
  2. echo "Adding more" >> ~/practice.txt — append to it
  3. cat ~/practice.txt — verify both lines are there
  4. ls -la ~/projects > ~/my-projects.txt — save a directory listing to a file
  5. cat ~/my-projects.txt — check the saved listing
BlueBox Terminal