How to Send a Command's Output to a File in bash and zsh with >
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
<command> > <file>
<command> >> <file>
| Part | What 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
echo 'Buy milk' > list.txt
cat list.txt

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
echo 'Buy bread' > list.txt
cat list.txt
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 >>
echo 'Buy eggs' >> list.txt
cat list.txt
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
ls > files.txt
cat files.txt
files.txt
list.txt

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
| Mistake | What happens | Do this instead |
|---|---|---|
The same file on both sides: sort fruit.txt > fruit.txt | The file ended up empty: cat fruit.txt printed nothing, and wc -c fruit.txt counted 0 bytes | Write 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.txt | The file was called my, and it held Buy milk list.txt | Quote the name: echo 'Buy milk' > 'my list.txt' made my list.txt |
A folder that doesn't exist: echo 'Buy milk' > notes/list.txt | zsh: zsh: no such file or directory: notes/list.txt. bash: bash: notes/list.txt: No such file or directory | Create the folder first with mkdir notes. Then the same command worked |
Expecting errors in the file: ls missing > out.txt | The error ls: missing: No such file or directory still showed on the screen, and out.txt was empty | See 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:
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
- Bash manual: Redirections:
>,>>,>|and the rest. - zsh manual: Redirection: the same operators in zsh.
