How can I do something like command > file
in a way that it appends to the file, instead of overwriting?
Asked
Active
Viewed 1.7e+01k times
148

Dennis Williamson
- 346,391
- 90
- 374
- 439

The Student
- 27,520
- 68
- 161
- 264
-
1(There are lots and lots of -- perhaps far too many! -- goodies in the [bash reference manual](http://www.gnu.org/software/bash/manual/bashref.html) including all sorts of redirections. Adjust as needed for shell.) – Mar 17 '11 at 17:31
-
4You may also use tee, if you want to redirect to both STDOUT and append results to a file. For example: echo "hello" | tee -a somefile.txt, where the -a flag stands for append. – Henrik Oct 18 '11 at 12:03
-
Possible duplicate of [How can I redirect and append both stdout and stderr to a file with Bash?](https://stackoverflow.com/questions/876239/how-can-i-redirect-and-append-both-stdout-and-stderr-to-a-file-with-bash) – jww Jan 03 '18 at 02:42
2 Answers
117
Yeah.
command >> file
to redirect just stdout of command
.
command >> file 2>&1
to redirect stdout and stderr to the file (works in bash, zsh)
And if you need to use sudo
, remember that just
sudo command >> /file/requiring/sudo/privileges
does not work, as privilege elevation applies to command
but not shell redirection part. However, simply using
tee
solves the problem:
command | sudo tee -a /file/requiring/sudo/privileges

EdvardM
- 2,934
- 1
- 21
- 20
-
7I'm using this for all output capturing program.sh 2>&1 | tee -a screen.log. "-a" stands for append. – Xdg Jul 17 '14 at 18:38