How to append the output to a file?

LinuxShellCommand Line

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

Use >> to append:

command >> file

Solution 2 - Linux

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

Solution 3 - Linux

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThe StudentView Question on Stackoverflow
Solution 1 - LinuxMike LewisView Answer on Stackoverflow
Solution 2 - LinuxEdvardMView Answer on Stackoverflow
Solution 3 - Linuxuser3680358View Answer on Stackoverflow