redirect所有输出到文件
我知道在Linux中,为了将输出从屏幕redirect到文件,我可以使用>或tee 。 但是,我不知道为什么部分输出仍然输出到屏幕上,而不是写入文件。 
有没有办法将所有输出redirect到文件?
 这部分写入stderr,使用2>redirect它。 例如: 
 foo > stdout.txt 2> stderr.txt 
或者如果你想在同一个文件中:
 foo > allout.txt 2>&1 
注意:这在(ba)sh中有效,请检查你的shell是否有正确的语法
 所有POSIX操作系统都有3个stream :stdin,stdout和stderr。  stdin是input,它可以接受stdout或stderr。 标准输出是主要的输出,用> , >>或|redirect  。  stderr是错误输出,它是分开处理的,因此任何exception都不会被传递给一个命令或写入一个可能会破坏的文件; 通常,这被发送到某种types的日志,或直接转储,即使stdout被redirect。 要redirect到相同的地方,使用: 
 command &> /some/file
编辑 :感谢扎克指出,上述解决scheme是不可移植的 – 使用,而不是:
 *command* > file 2>&1 
如果您想要消除错误,请执行以下操作:
 *command* 2> /dev/null 
 例如,要在控制台上获取输出,并在文件file.txt获取输出。 
 make 2>&1 | tee file.txt 
 注意: & (在2>&1 )指定1不是文件名,而是文件描述符。 
 使用这个 – "require command here" > log_file_name 2>&1 
在Unix / Linux中的redirect操作符的详细描述。
>运算符通常将输出redirect到一个文件,但它可能是一个设备。 你也可以用>>来追加。
如果你没有指定一个数字,那么标准输出stream被假定,但你也可以redirect错误
 > file redirects stdout to file 1> file redirects stdout to file 2> file redirects stderr to file &> file redirects stdout and stderr to file 
/ dev / null是空设备,它需要你想要的任何input并把它扔掉。 它可以用来抑制任何输出。
这可能是标准错误。 你可以redirect它:
 ... > out.txt 2>&1 
学分到osexp2003和ja …
而不是放
 &>> your_file.log 
在一条线后面
 crontab -e 
我用
 #!/bin/bash exec &>> your_file.log … 
在BASH脚本的开头。
优点:您的脚本中有日志定义。 适合Git等
 您可以使用exec命令稍后redirect所有命令的所有stdout / stderr输出。 
示例脚本:
 exec 2> your_file2 > your_file1 your other commands..... 
命令:
 foo >> output.txt 2>&1 
附加到output.txt文件,而不replace内容。
 使用>>添加: 
 command >> file 
 在Linux Mint中,此命令string将执行脚本和错误路由到单个txt文件。  bash -x ./setup.sh > setup.txt 2>&1 。 脚本名称是setup.sh,输出目标是setup.txt。