Why is appending different streams to a file safe?
14
votes
2
answers
1992
views
It's well known that redirecting standard output and error to the same file with
cmd >out_err.txt 2>out_err.txt
can lead to loss of data, as per the example below:
work:/tmp$ touch file.txt
work:/tmp$ ls another_file.txt
ls: cannot access 'another_file.txt': No such file or directory
The above is the setup code for the example. An empty file file.txt
exists and another_file.txt
is not a thing. In the code below, I naively redirect to out_err.txt
both input and output os listing these files.
work:/tmp$ ls file.txt another_file.txt >out_err.txt 2>out_err.txt
work:/tmp$ cat out_err.txt
file.txt
t access 'another_file.txt': No such file or directory
And we see that we lost a few characters in the error stream. However, using >>
works in the sense that replicating the example would yield keep the whole output and the whole error.
Why and how does cmd >>out_err.txt 2>>out_err.txt
work?
Asked by Sweet Shell O'Mine
(315 rep)
Feb 6, 2023, 01:56 PM
Last activity: Feb 7, 2023, 02:34 AM
Last activity: Feb 7, 2023, 02:34 AM