为什么 "ls not_existing_file 2&>1 > log.txt" 不向 log.txt 写入任何内容?

Why does "ls not_existing_file 2&>1 > log.txt" not write anything to log.txt?

我想将 ls 的标准输出和标准错误重定向到 log.txt。为什么这个命令

ls not_existing_file 2>&1 > log.txt

生成一个空 log.txt 文件?

这是因为您试图将重定向合并运算符 stdout 的输出重定向到日志文件而不是命令本身的输出。

您可以改为将命令的标准输出重定向到日志文件中,然后附加重定向合并运算符,如下所示: ls not_existing_file > log.txt 2>&1

这个问题在How to redirect and append both standard output and standard error to a file with Bash

中也有回答