The fundamental difference between > and >> is whether they overwrite or append data to a file. The single greater-than sign (>) redirects a command's output and overwrites the destination file, while the double greater-than sign (>>) redirects output and appends it to the end of the destination file. Both operators will create the file if it does not already exist.
The overwrite operator (>)
When you use the > operator, the shell first truncates the file to zero size before writing any output. If the file is a new one, it is simply created. This means any existing data in the file is permanently deleted.
Example of overwriting file contents
# Initial file creation
$ echo "This is the first line." > myfile.txt
$ cat myfile.txt
This is the first line.
# Overwriting the file
$ echo "This is the second line." > myfile.txt
$ cat myfile.txt
This is the second line.
Use code with caution.
As you can see, the original content was completely replaced. This behavior is useful for creating a new version of a file from a command's output, such as generating a fresh log file for a new process.
The append operator (>>)
The >> operator is the non-destructive option. It redirects output to a file and adds it to the end of any existing content. This is particularly useful for building log files or collecting output from multiple commands into a single document over time.
Example of appending to a file
# Initial file creation
$ echo "Log entry 1." > mylog.txt
$ cat mylog.txt
Log entry 1.
# Appending a new line
$ echo "Log entry 2." >> mylog.txt
$ cat mylog.txt
Log entry 1.
Log entry 2.
Use code with caution.
The original "Log entry 1" is preserved, and the new line is added at the end.
Detailed comparison: overwrite vs. append
| Feature | Overwrite (>) |
Append (>>) |
|---|---|---|
| Action on existing file | Truncates the file to zero length and replaces all content. | Adds new content to the end of the file, preserving the original data. |
| Behavior on new file | Creates a new file. | Creates a new file. |
| Ideal use case | Saving a command's output as a fresh version of a file. Resetting a file's content. | Consolidating output from multiple commands. Building log files over time. |
| Risk | High risk of data loss, as previous content is permanently deleted. | Low risk of data loss from overwriting, as it only adds content. |
Advanced considerations and best practices
1. Standard Error redirection
Both > and >> by default only handle standard output (stdout), which is the normal output of a command. Standard error (stderr), where error messages are sent, is a separate stream.
- To redirect both stdout and stderr to a file, you can use the
&>operator (in Bash and some other shells) or the more traditionalcommand > file 2>&1syntax. - To append both stdout and stderr, use
&>>orcommand >> file 2>&1.
2. The noclobber option
For added safety, some shells offer a noclobber option. When this is set, the shell will not overwrite a file with > if the file already exists.
# Enable noclobber
$ set -o noclobber
$ echo "Some content" > testfile.txt
$ echo "New content" > testfile.txt
bash: testfile.txt: cannot overwrite existing file
Use code with caution.
To bypass noclobber and force an overwrite, you can use >|.
3. Combining redirections
Redirection operators can be used in conjunction with pipes (|) for complex data processing. For example, you could pipe the output of one command to another, and then redirect the final result to a file.
# Search for a pattern and redirect the results to a file
$ grep "error" access.log > error_report.txt
Use code with caution.
4. The tee command
The tee command is a powerful alternative for simultaneously displaying and redirecting output.
- Overwrite:
command | tee logfile.txtwrites to the file while also showing output on the terminal. - Append:
command | tee -a logfile.txtappends to the file and shows output on the terminal.teeis a valuable tool when you need to both see the output in real-time and save it to a file.