Redirection to Files

LinuxBeginner
Practice Now

Introduction

By default, Linux commands print everything to your screen. But what if you want to save that data?

This process is called Redirection. It is like putting a "Forwarding Address" on your mail.

That is a good way to think about shell syntax in general: instead of changing what a command fundamentally does, redirection changes where its input or output is sent.

In this lab, you will learn three ways to control where data goes:

  1. Redirect and Overwrite (>): Saves output to a file, replacing unmatched content.
  2. Redirect and Append (>>): Adds output to the end of a file.
  3. Error Redirection (2>): Catches error messages only.

Redirect Output (Overwrite)

The greater-than sign > is the Rewrite operator.

Many beginners learn > as "save to file," but it is more precise to think of it as "send standard output to this file and replace the previous contents." That wording becomes important once you start combining commands more carefully.

It tells Linux: "Take input from this command and write it to this file. If the file exists, erase it first."

Go to your project directory:

cd ~/project

Redirect output to output.txt:

echo "Hello, Linux." > output.txt

The screen is silent. The text went into the file. Verify it:

cat output.txt

Now, see why we call it "Overwrite". Run this:

echo "This is new content." > output.txt
cat output.txt

The old "Hello, Linux." is gone. The > operator wiped the file clean.

This is why redirection should be treated with the same caution as file modification. A short command can replace the contents of an existing file immediately.

Append Output (>>)

Often, you don't want to destroy data; you want to add to it. Use the double greater-than sign >>.

This acts as an Append instruction.

Add a new line to output.txt:

echo "This line is appended." >> output.txt
cat output.txt

You should see both lines.

The original content is safe. Let's do it again:

echo "Another line." >> output.txt
cat output.txt

This >> operator is perfect for logs.

Appending matters because many operational files grow over time instead of being recreated from scratch. Logs, reports, and command journals often use an "add at the end" workflow.

Redirect Errors (2>)

As you learned, Linux separates "Good Output" (stdout) from "Bad Output" (stderr).

The > and >> operators strictly handle "Good Output". They ignore errors.

This is where the stream model from the previous lab becomes practical. If output and errors are separate streams, then Linux can let you route them to different destinations.

To catch errors, we use 2>. (The ID for Standard Error is 2).

Try to list a missing directory:

ls /missing_directory 2> error.log

The screen remains blank. The error was caught and shoved into error.log.

Inspect it:

cat error.log

You will see the error message trapped inside.

The 2 in 2> comes from the conventional file descriptor number for standard error. You do not need to memorize all file descriptors yet, but it is useful to know that these numbers refer to the shell's stream channels.

Summary

You have mastered the art of traffic control:

  • > (Overwrite): Saves output to a file (wipes previous content).
  • >> (Append): Adds output to end of file (keeps history).
  • 2> (Error Redirect): Catches error messages.

These tools authorize you to save command results efficiently.