如何将stderr重定向到文件?

Ste*_*nie -2 unix bash find

将标准错误描述符重定向到unix中名为error.txt的文件所需的命令是什么?

到目前为止我有这个命令:

find / -name "report*" ________ error.txt
Run Code Online (Sandbox Code Playgroud)

fed*_*qui 5

您可以2像这样使用stderr处理程序:

find / -name "report*" 2>error.txt
Run Code Online (Sandbox Code Playgroud)

看一个例子:

$ ls a1 a2
ls: cannot access a2: No such file or directory  <--- this is stderr
a1                                               <--- this is stdin
$ ls a1 a2 2>error.txt
a1
$ cat error.txt 
ls: cannot access a2: No such file or directory  <--- just stderr was stored
Run Code Online (Sandbox Code Playgroud)

正如在BASH Shell中所读到的:如何将stderr重定向到stdout(将stderr重定向到文件),这些是处理程序:

Handle  Name    Description
0       stdin   Standard input   (stdin)
1       stdout  Standard output  (stdout)
2       stderr  Standard error   (stderr)
Run Code Online (Sandbox Code Playgroud)

注意区别于&>error.txt重定向stdin和stderr(请参阅bash脚本中的重定向stderr和stdout如何将stdout和stderr重定向到文件):

$ ls a1 a2 &>error.txt
$ cat error.txt 
ls: cannot access a2: No such file or directory  <--- stdin and stderr
a1                                               <--- were stored
Run Code Online (Sandbox Code Playgroud)