I want to redirect stout and stderr of ls
to log.txt
. Why does this command
ls not_existing_file 2>&1 > log.txt
result in an empty log.txt file?
CodePudding user response:
It is due to you trying to redirect the output of the redirect merge operator stdout into the log file rather than the output of the command itself.
What you could do instead is redirect the stdout of the command into the log file, and then append the redirect merge operator as follows:
ls not_existing_file > log.txt 2>&1
This question has also been answered in How to redirect and append both standard output and standard error to a file with Bash