How can I do something like command > file in a way that it appends to the file, instead of overwriting?
Asked
Active
Viewed 1.5e+01k times
138
Dennis Williamson
- 324,833
- 88
- 366
- 429
The Student
- 26,271
- 66
- 155
- 257
-
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
3 Answers
113
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,781
- 1
- 20
- 18
-
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
-8
you can append the file with >> sign. It insert the contents at the last of the file which we are using.e.g if file let its name is myfile contains xyz then cat >> myfile abc ctrl d
after the above process the myfile contains xyzabc.
user3680358
- 1
- 1
-
5The `>>` operator was mentioned three years earlier already, and the OP knows what appending means. – Dan Dascalescu Sep 13 '16 at 05:51