36.2 > and >>: Redirecting stdout to Files
Right, let’s talk about making your terminal commands stop yelling into the void and start writing things down. By default, when a command runs, its standard output (stdout)—the regular, expected results—just gets printed to your screen and then vanishes into the ether. Useful for one-off glances, useless for, well, everything else. This is where the > operator comes in. It’s your command-line bouncer, grabbing stdout by the collar and redirecting it to a file you specify.
The basic syntax is dead simple: command > file.txt. Run that, and instead of seeing the output on your screen, you’ll find it neatly tucked into file.txt. Go ahead, try it. Let’s see what’s in your home directory.
ls ~ > my_home_list.txt
cat my_home_list.txt
See? No output to the screen. The cat command dutifully shows you what ls wrote to the file. It’s the fundamental building block of scripting: capturing results for later.
The Brutal Truth of >
Here’s the first thing you absolutely must internalize: the > operator is brutally efficient. It doesn’t “add to” a file. It replaces the entire contents of that file with the new output. Every. Single. Time. If file.txt already existed, its previous contents are now gone, overwritten without so much as a warning. This is the most common “oh crap” moment for newcomers. You think you’re appending a little log entry, and you’ve just nuked your entire application log. Consider this your official warning.
Appending with >>
So what if you don’t want to vaporize your old data? What if you’re collecting logs, adding lines to a list, or just being sensible? That’s where >> (the append operator) shines. It takes the output of your command and politely adds it to the end of the specified file. If the file doesn’t exist, it creates it, just like > does. This is what you use 99% of the time when you’re writing to a log file.
# Let's start a log
echo "System update started at $(date)" >> system_log.txt
# ... time passes, you do some update stuff ...
# Then add a finish entry
echo "System update completed at $(date)" >> system_log.txt
cat system_log.txt
The File Creation Quirk (and Permissions)
Here’s a fun bit of shell trivia: the program itself never actually sees the redirection operator. The shell handles all of it before the command is even run. Here’s the process:
- You type
ls > output.txt. - The shell parses this and sees the
>. - The shell first creates or truncates (empties) the file
output.txt. It does this immediately. - The shell then runs the
lscommand, but hooks itsstdoutstream directly to the open file descriptor foroutput.txt. lsruns, blissfully unaware it’s writing to a file instead of a terminal. It just pumps bytes to itsstdoutlike it always does.
This explains why you can get “Permission denied” errors even if the command itself wouldn’t need to write to a file. The shell tries to create the file in the current directory, and if you don’t have write permissions there, it’ll fail before ls even gets a chance to run.
cd /root # assuming you're not root and can't write here
ls > some_file.txt
# You'll get: bash: some_file.txt: Permission denied
# The ls command never executed. The shell failed at step 3.
Redirecting into the Abyss (/dev/null)
Sometimes, you want a command to run, but you genuinely couldn’t care less about its standard output. Maybe it’s a noisy process with useless success messages, or you’re running it in a script and want a clean output. The solution is to redirect to /dev/null. This isn’t a real file; it’s a special system device that acts as a black hole. Data goes in and is never seen again. It’s the ultimate “shut up” switch.
# This command will run, but print absolutely nothing to the screen.
# Any error messages (stderr) would still show up, though. We'll cover that next.
ls / > /dev/null
The takeaway? Use > when you want to create a new file or completely replace an old one. Use >> when you want to add to an existing file. And always, always, double-check your filenames before hitting enter. Your future self, staring at a newly empty important_document.txt, will thank you.