Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

Mastering Output Redirection (> and >>) in Linux: Overwrite and Append Like a Pro

Published

LinuxLinux and Commands

If you spend any time on the Linux command line, you’ll quickly notice that almost everything you do involves text: logs, configurations, reports, code, and more. But what really sets the Linux shell apart is how easy it is to capture, save, and manipulate the output of commands whether you’re running something simple like ls, or pulling multi-screen reports from a complex script.

At the heart of this power are two humble operators: > and >>. They’re deceptively simple, but understanding how they work can mean the difference between keeping a valuable log or accidentally erasing hours of data. In this guide, we’ll go deep into how these redirection operators work, why they matter, and how to use them for everything from basic file writing to log management and scripting.

What is Output Redirection?

When you run a command in the shell, its output by default goes to your screen (specifically, to something called “standard output,” or stdout). But what if you want to save that information for later, or pass it to another program, or quietly log it while you run something else? That’s where output redirection comes in.

  • >: sends output to a file, overwriting any previous content.
  • >>: sends output to a file, appending it to whatever was already there.

This simple difference can have huge consequences, as you’ll see in the examples below.

The Basics: Overwriting Files with >

Let’s start with the basics. The > operator is your go-to when you want to create a new file out of command output, or replace the contents of an existing one.

Example 1: Overwriting a File

echo "Hello, world!" > greeting.txt

This creates greeting.txt (if it doesn’t exist) and writes “Hello, world!” into it. If greeting.txt already exists, its entire content is erased and replaced.

Example 2: Overwriting with a Command’s Output

ls -l /usr/bin > binlist.txt

Now, binlist.txt contains a fresh list of everything in /usr/bin. If you run the same command again, binlist.txt will be replaced with the new output.

Pitfall: If you use > on the wrong file by accident, you can easily destroy hours of work. There’s no “undo” for a clobbered file in the shell!

Appending to Files with >>

The double arrow >> is what you use when you want to add to the end of an existing file, rather than overwriting it.

Example 3: Appending a Line

echo "Farewell, world!" >> greeting.txt

Now, if you look inside greeting.txt, you’ll see:

Hello, world!
Farewell, world!

Example 4: Appending Command Output Over Time

Suppose you want to build up a log of disk usage every hour:

date >> disk_usage.log
df -h >> disk_usage.log

If you schedule these commands to run repeatedly (say, via cron), your log will keep growing, with each new entry tacked on to the end.

Real-World Use: System administrators commonly use >> for log files so you get a running record of events, rather than losing history every time a command is run.

Standard Output and Standard Error: The Full Picture

Linux commands can output to standard output (normal results) and standard error (error messages). By default, > and >> only redirect standard output.

Example 5: Errors Still Show

ls /root > root_list.txt

If you don’t have permission, root_list.txt will be empty, but you’ll still see the error “Permission denied” on your screen.

How to Capture Errors Too?

You need to redirect standard error using 2> and 2>>:

  • 2> overwrites error output file.
  • 2>> appends error output.

Example 6: Logging Both Output and Errors

ls /root > all.log 2>&1

> sends standard output to all.log.

2>&1 tells the shell to send standard error (2) to the same place as standard output (1).

To append both:

ls /root >> all.log 2>&1

Use Cases: When to Overwrite vs. When to Append

Overwriting (>):

  • You want a fresh start like a new report or data export.
  • You’re only interested in the most recent output.
  • Your script is meant to produce a single, up-to-date result each time.

Appending (>>):

  • You’re building up a log over time.
  • Multiple runs of a command should all be kept for auditing or troubleshooting.
  • You don’t want to lose what was already in the file.

Pro tip: If you’re ever unsure, use >> while testing, so you don’t accidentally wipe out important data!

Real-World Story: The Accidental Overwrite

Here’s a war story from the trenches: I once had a backup script that ran every night. It was supposed to append each night’s results to a log. One typo later, I used > instead of >>. Result? Every night’s log wiped out the previous one. We didn’t realize until we had to diagnose a problem, only to discover a single day’s log no history. From then on, I always double-check my redirection operators.

Practical Examples

Example 7: Aggregating Logs

A script that pings a server every minute and logs whether it’s up or down:

while true; do
    date >> server_status.log
    ping -c 1 example.com && echo "UP" || echo "DOWN" >> server_status.log
    sleep 60
done

Each run adds a new timestamp and status to the end of server_status.log.

Example 8: Rotating Logs

If you use >> over months, logs can get huge. That’s why admins use tools like logrotate to archive and compress old logs, then start fresh with >.

Advanced: Combining Inputs and Outputs

You can use { … } braces to group commands and redirect all their output together:

{ 
    echo "Disk check at $(date)" 
    df -h 
    echo "Check complete" 
} >> diskcheck.log 2>&1

Now, both outputs and any errors are appended as a block.

Bonus: Preventing Accidental Overwrites

If you’re nervous about accidentally overwriting files, Bash has the noclobber option:

set -o noclobber
echo "test" > existingfile.txt   # Will warn and not overwrite

To force overwrite anyway:

echo "test" >| existingfile.txt

Common Mistakes

  1. Forgetting Which Operator Does What:
    > : Overwrites, dangerous if you want to keep your data.
    >> : Appends, safer for logs and history.
  2. Permissions Errors: If you don’t have write access to a file or directory, redirection will silently fail (or throw an error).
  3. Using sudo Incorrectly: If you write:
    sudo echo "Hello" > /root/somefile.txt

    You’ll get a permission denied error, because the redirection happens before sudo takes effect. Instead, use:

    echo "Hello" | sudo tee /root/somefile.txt > /dev/null

    Or to append:

    echo "Another line" | sudo tee -a /root/somefile.txt > /dev/null

Frequently Asked Questions

What if the file doesn’t exist?
Both > and >> will create the file if it doesn’t already exist, as long as you have permission.

Can I redirect output from multiple commands into a single file?
Absolutely use grouping with { … } or semicolons:

{ date; uptime; } >> system.log

How can I see the output live and save it to a file at the same time?
Use tee:

somecommand | tee output.log

Or to append:

somecommand | tee -a output.log

Conclusion

The > and >> operators are two of the most powerful and commonly used features of the Linux shell. They let you control exactly where your command output goes, whether you’re building logs, saving reports, or stringing together complex scripts.

Just remember the difference:

  • > will overwrite (replace) a file.
  • >> will append (add to the end) of a file.

Both are essential tools for anyone who wants to get serious about working in Linux, and mastering them will save you time, prevent data loss, and make your shell scripts far more flexible and robust.

Next time you’re on the terminal, pay attention to your redirection. A single character can make all the difference!

Related guides

Account services are unavailable until the production backend is configured.