如何将stderr重定向到文件?

时间:2014-08-07 11:43:02

标签: bash unix find

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

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

find / -name "report*" ________ error.txt

1 个答案:

答案 0 :(得分:5)

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

find / -name "report*" 2>error.txt

查看示例:

$ 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

正如BASH Shell: How To Redirect stderr To stdout ( redirect stderr to a File )所述,这些是处理程序:

Handle  Name    Description
0       stdin   Standard input   (stdin)
1       stdout  Standard output  (stdout)
2       stderr  Standard error   (stderr)

注意与&>error.txt的区别,重定向stdin和stderr(请参阅Redirect stderr and stdout in a bash scriptHow to redirect both stdout and stderr to a file):

$ ls a1 a2 &>error.txt
$ cat error.txt 
ls: cannot access a2: No such file or directory  <--- stdin and stderr
a1                                               <--- were stored