1 > 符号
重定向符号 >
1 输出至文件
ls > log.txt
注意:
- 如果没有文件
log.txt
则会新建log.txt
;如果存在则覆盖 - 重定向符号只能将标准输出定向至文件中,由于标准错误不属于标准输出,因此无法使用该命令保存
2>
表示重定向标准错误
因此
可以保存输出和错误ls > log.txt 2>error.txt
要保存至一个文件中,可以使用ls > log.txt 2>&1
2 追加至文件尾部
ls >> log.txt
2 tee命令
1 输出至文件
尽管>比较简洁,但是遇到的需求一般是一方面保存结果,一方面在屏幕显示。
这种条件下,使用tee命令
ls | tee log.txt
其余选项与>相同
输出标准打印信息与错误信息到一个文件
ls 2>&1 | tee log.txt
2 追加至文档尾部
ls | tee -a log.txt
相关介绍
(MindSpore) [ma-user mindformers]$tee --help
Usage: tee [OPTION]... [FILE]...
Copy standard input to each FILE, and also to standard output.
-a, --append append to the given FILEs, do not overwrite
-i, --ignore-interrupts ignore interrupt signals
-p diagnose errors writing to non pipes
--output-error[=MODE] set behavior on write error. See MODE below
--help display this help and exit
--version output version information and exit
3 多行命令使用script
参考链接
- https://blog.csdn.net/qq_44681788/article/details/126239092
- https://www.cnblogs.com/jdksummer/articles/2532259.html