Skip to content
zero2vibecodelearn vibe coding
Level 2 · Working with content 6 min Reviewed by the author · Aug 4, 2026

Writing to a file: echo and >

The `>` symbol sends a command's output into a file instead of onto the screen. `>` replaces the file, `>>` adds to the end.

Level 2 · 0 / 4 lessons0%

So far commands have printed to the screen. Now send that output somewhere useful.

The arrow

echo Hello from the terminal > hello.txt

Nothing appears on screen. That is the point: the > caught the output that echo would have printed and wrote it into hello.txt instead.

Check it:

cat hello.txt
Hello from the terminal

You just created a file with content in it, without ever opening an editor.

Note

This is called redirection, and it is not an echo feature — it works with any command. ls > files.txt saves a folder listing. grep ERROR log.txt > errors.txt saves just the error lines. Anything that prints can be redirected.

Quick check

You run `ls > files.txt` and see no output. What happened?

One arrow replaces. Two arrows add.

> does not add to a file. It replaces it, completely, with no warning and no confirmation:

echo first > notes.txt
echo second > notes.txt
cat notes.txt
second

first is gone. There is no undo.

Use >> to append instead:

echo first > notes.txt
echo second >> notes.txt
cat notes.txt
first
second

Important

This is the single most expensive typo in the terminal. > on a file that already matters wipes it instantly and silently. Before you point a at an existing file, take the half-second to check whether you meant >>.

Quick check

You want to add a line to a file that already has content. Which do you use?

A useful habit

When you are not certain what a command will produce, run it without the redirect first and look at the output. Then run it again with the redirect.

grep ERROR server.log
grep ERROR server.log > errors.txt

Costs you one extra command, saves you writing garbage into a file you then have to clean up.

Quick check

Why run a command once without the redirect before adding one?

Try it

Build a small file in two steps:

  1. echo Hello from the terminal > hello.txt — create it with one line.
  2. echo Second line >> hello.txt — append a second.
  3. cat hello.txt — read both lines back.

Then try echo Oops > hello.txt followed by cat hello.txt, and watch both lines vanish. Better to learn that here than on something you care about.

Check your understanding

Answered 0/3
Mode:

What does `>` do?

What is the difference between `>` and `>>`?

The file log.txt already has 200 lines. You run: echo done > log.txt. What is in it now?

Finish the task in the terminal and answer the questions to complete the lesson.

📝 My notes

Saved automatically in this browser · shows up on the Notes page.

Task

Create hello.txt with one line, add a second line, then read it back.

Tab complete · history · Ctrl+L clear · help commands

  • Not done: hello.txt exists
  • Not done: The first line was written
  • Not done: The second line was appended

Lesson Q&A

What does `>` do?

Redirection takes the text that would have been printed and writes it to a file. The old contents are gone.

What is the difference between `>` and `>>`?

One arrow overwrites, two arrows append. Mixing them up is how people lose files.

The file log.txt already has 200 lines. You run: echo done > log.txt. What is in it now?

A single > wipes the file first and asks nothing. This is the most common way beginners destroy their own work.

You run `ls > files.txt` and see no output. What happened?

Redirection means the output goes to the file rather than the terminal. Silence is exactly what you expect.

You want to add a line to a file that already has content. Which do you use?

A single > truncates the file to nothing before writing. Only >> keeps what was there.

Why run a command once without the redirect before adding one?

Looking first is cheap. Discovering afterwards that you wrote the wrong thing over an existing file is not.

Move between lessons with · search with ⌘K