138

How can I do something like command > file in a way that it appends to the file, instead of overwriting?

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
  • 4
    You 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 Answers3

239

Use >> to append:

command >> file
Tim Cooper
  • 151,519
  • 37
  • 317
  • 271
Mike Lewis
  • 61,841
  • 20
  • 140
  • 111
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
  • 7
    I'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.