首页·终端命令

tee

文本处理

用法

[cmd] | tee [file]

把输入同时写进文件并送到屏幕;要写 root 拥有的文件就靠它,因为 sudo cmd > file 里的重定向是你自己的 shell 打开的。

常用选项

选项含义
-aAppend to the file instead of overwriting it
-iIgnore Ctrl-C while the pipe runs
-pGNU: report errors when writing to a pipe

示例

make 2>&1 | tee build.log

Watch the build and keep a copy.

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Appends to a root-owned file, which > cannot do.

curl -s api | tee raw.json | jq .name

Keeps the raw body and reads one field.

它们本来就是用管道串起来用的。串三条往往比让一条命令做完所有事更短。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. tee 是做什么的?

把输入同时写进文件并送到屏幕;要写 root 拥有的文件就靠它,因为 sudo cmd > file 里的重定向是你自己的 shell 打开的。

Q. 怎么写?

[cmd] | tee [file] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 3 个,完整列表在 man tee。这条命令属于文本处理。

相关命令

man 手册: man tee