tee
命令是Linux/Unix系统中一个非常实用的工具,它能够从标准输入读取数据,同时将数据写入标准输出和指定的文件中。
command | tee [OPTION]... [FILE]...
选项 | 描述 |
---|---|
-a 或 --append |
追加到文件而不是覆盖 |
-i 或 --ignore-interrupts |
忽略中断信号 |
--help |
显示帮助信息 |
--version |
显示版本信息 |
ls -l | tee filelist.txt
将ls -l
的结果显示在屏幕上,同时保存到filelist.txt文件中
echo "New line" | tee -a filelist.txt
将"New line"追加到filelist.txt文件末尾,而不是覆盖原有内容
dmesg | tee file1.txt file2.txt file3.txt
将dmesg
命令的输出保存到三个不同的文件中
echo "new line" | sudo tee -a /etc/file.conf
当需要向需要root权限的文件写入内容时,可以使用sudo tee
ping example.com | tee -i ping.log
即使使用Ctrl+C中断命令,也能保证已捕获的数据写入文件
ls -l | tee directory_listing.txt | grep "\.txt$"
将目录列表保存到文件,同时只筛选出.txt文件显示在屏幕上
ls -l | tee filelist.txt | wc -l
保存文件列表到filelist.txt,同时计算文件数量
./script.sh | tee script_output.log
运行脚本并同时查看输出和保存日志
command1 | tee intermediate.log | command2 | tee final.log
在复杂的管道中记录中间结果
tee
会覆盖目标文件,使用-a
选项可以追加内容tee
前使用sudo
而不是整个命令tee
不会缓冲输出,数据会立即写入文件和标准输出tee
命令在系统管理、日志记录和调试过程中非常有用,能够帮助管理员同时查看和保存命令输出。