Skip to main content

How to Send a Command's Output to a File in bash and zsh with >

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A command prints its output on the screen. Write > and a file name after it, and the output goes into that file instead: the shell creates the file, or empties it first if it's already there.

This guide writes a short shopping list to a file, with real output from zsh 5.9 and bash 3.2 on macOS.

Syntax​

Syntax
<command> > <file>
<command> >> <file>
PartWhat it means
<command>Any command that prints something
>Sends the output into <file>, replacing what was in it
>>Adds the output to the end of <file>
<file>The file's name. The shell creates the file if it doesn't exist

The bash manual describes >: Redirecting output opens the file whose name results from the expansion of word for writing on file descriptor n, or the standard output (file descriptor 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size. Here, word is the file name, and the standard output is what the command prints. >> opens the file whose name results from the expansion of word for appending, that is, for adding to its end.

The Example​

You write a shopping list to list.txt, replace it, add a line to it, and then save the output of ls the same way.

Step 1: Save a Line in a File​

Terminal
echo 'Buy milk' > list.txt
cat list.txt

macOS-style terminal window titled Terminal with echo 'Buy milk' > list.txt, which sends the text into list.txt, and cat list.txt, which prints the file.

Output
Buy milk

echo printed nothing on the screen. Its output went into list.txt, which the shell created. cat prints a file, and shows that the line is in it.

Step 2: Run > Again to Replace the File​

Terminal
echo 'Buy bread' > list.txt
cat list.txt
Output
Buy bread

Buy milk is gone. list.txt existed, so the shell emptied it before echo wrote the new line. That's the second half of the rule above: if it does exist it is truncated to zero size.

Step 3: Add a Line with >>​

Terminal
echo 'Buy eggs' >> list.txt
cat list.txt
Output
Buy bread
Buy eggs

>> kept what was there and added the new line at the end. The zsh manual describes it as Open file word for writing in append mode as standard output.

Step 4: Save Any Command's Output​

Terminal
ls > files.txt
cat files.txt
Output
files.txt
list.txt

macOS-style terminal window showing a real zsh session: > writes Buy milk to list.txt, a second > replaces it with Buy bread, >> adds Buy eggs, and ls > files.txt saves a list that includes files.txt itself.

ls lists the files in a folder, and its list includes files.txt, the file it was writing to. The shell sets up > before the command starts. The bash manual: Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. So files.txt already existed, empty, when ls looked. bash printed the same in every step.

Common Mistakes​

MistakeWhat happensDo this instead
The same file on both sides: sort fruit.txt > fruit.txtThe file ended up empty: cat fruit.txt printed nothing, and wc -c fruit.txt counted 0 bytesWrite to a new file: sort fruit.txt > sorted.txt saved apples and pears in order
A file name with a space, not quoted: echo 'Buy milk' > my list.txtThe file was called my, and it held Buy milk list.txtQuote the name: echo 'Buy milk' > 'my list.txt' made my list.txt
A folder that doesn't exist: echo 'Buy milk' > notes/list.txtzsh: zsh: no such file or directory: notes/list.txt. bash: bash: notes/list.txt: No such file or directoryCreate the folder first with mkdir notes. Then the same command worked
Expecting errors in the file: ls missing > out.txtThe error ls: missing: No such file or directory still showed on the screen, and out.txt was emptySee the FAQ below

The first row is the rule from Step 2 again: the shell empties fruit.txt before sort starts, so sort reads an empty file. In the second row, only my follows >, so list.txt went to echo as one more word to print.

FAQ​

How do I stop > from replacing a file by mistake?​

Turn on the noclobber option:

Terminal
set -o noclobber
echo 'Buy bread' > list.txt

With list.txt already there, zsh printed zsh: file exists: list.txt and bash printed bash: list.txt: cannot overwrite existing file. The bash manual: If the redirection operator is ‘>’, and the noclobber option to the set builtin command has been enabled, the redirection fails if the file whose name results from the expansion of word exists and is a regular file. When you do mean to replace the file, write >|: echo 'Buy bread' >| list.txt worked in both shells. set +o noclobber turns the option off again.

How do I save the errors too?​

Add 2>&1 after the file name. ls missing > out.txt 2>&1 printed nothing on the screen, and cat out.txt showed ls: missing: No such file or directory inside the file.

Conclusion​

echo 'Buy milk' > list.txt created list.txt with one line in it, a second > replaced that line, and >> added Buy eggs after Buy bread. Use > for a fresh file and >> to add to one, and never send a command's output into the file it's reading.

Additional Resources​