tee ¶
tee 命令完全指南 ¶
tee 命令用于同时将数据输出到文件并显示在屏幕上。就像字母 T 一样,数据流被分成两路。
一、命令简介 ¶
基本功能 ¶
- 从标准输入读取数据
- 同时输出到屏幕(标准输出)和文件
- 可以同时写入多个文件
使用语法 ¶
命令 | tee [选项] 文件名...
二、选项说明 ¶
| 选项 | 说明 |
|---|---|
-a, --append |
追加到文件末尾(不覆盖原内容) |
-i, --ignore-interrupts |
忽略中断信号(Ctrl+C) |
--help |
显示帮助信息 |
--version |
显示版本信息 |
三、常用案例 ¶
1. 基本用法:显示并保存 ¶
# 查看文件内容的同时保存
cat file.txt | tee copy.txt
# 查看文件并显示行号
nl file.txt | tee numbered.txt
2. 命令行输出保存并查看 ¶
# 查看目录列表,同时保存到文件
ls -la | tee listing.txt
# 查看系统信息,同时保存
uname -a | tee sysinfo.txt
3. 追加到文件(-a 选项) ¶
# 追加内容,不覆盖
echo "新行" | tee -a log.txt
# 追加目录列表到文件
ls | tee -a filelist.txt
4. 同时写入多个文件 ¶
# 同时保存到多个文件
echo "重要信息" | tee file1.txt file2.txt file3.txt
# 同时显示并保存多个日志
date | tee log1.txt log2.txt log3.txt
5. 结合 sudo 写入需要权限的文件 ¶
# 写入系统配置文件(需要 root 权限)
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
# 追加到系统文件
echo "新配置" | sudo tee -a /etc/hosts
6. 在脚本中记录操作 ¶
#!/bin/
# 记录脚本执行过程
echo "开始执行: $(date)" | tee -a script.log
./run_task.sh | tee -a script.log
echo "执行完成: $(date)" | tee -a script.log
7. 管道中的使用 ¶
# 查看并统计行数
cat file.txt | tee copy.txt | wc -l
# 查看并搜索
dmesg | tee kernel.log | grep error
# 查看并压缩
ps aux | tee process.txt | grep root
8. 实时监控并记录 ¶
# 监控日志并保存
tail -f /var/log/syslog | tee syslog_copy.txt
# 监控系统负载
vmstat 1 | tee vmstat.log
9. 忽略中断信号(-i) ¶
# 按 Ctrl+C 时不会中断 tee,会继续处理
tee -i log.txt
# 输入内容,Ctrl+C 不会停止(Ctrl+D 才退出)
四、常见使用场景 ¶
场景1:写入需要 sudo 权限的文件 ¶
# ❌ 错误写法(重定向没有权限)
echo "配置内容" > /etc/myconfig.conf
# Permission denied
# ✅ 正确写法
echo "配置内容" | sudo tee /etc/myconfig.conf
# 追加
echo "新配置" | sudo tee -a /etc/myconfig.conf
场景2:同时查看和保存命令输出 ¶
# 查看磁盘使用,同时保存
df -h | tee disk_usage.txt
# 查看内存信息,同时保存
free -h | tee -a system_info.txt
# 查看进程信息,同时保存
ps aux | tee process_list.txt
场景3:备份命令输出 ¶
# 执行命令,保存输出,同时显示
./deploy.sh 2>&1 | tee deploy.log
# 查看错误日志
./build.sh 2>&1 | tee build.log
场景4:配置文件的快速修改 ¶
# 修改 hosts 文件
echo "127.0.0.1 myapp.local" | sudo tee -a /etc/hosts
# 修改 sysctl 配置
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.conf
# 修改 limits 配置
echo "* soft nofile 65535" | sudo tee -a /etc/security/limits.conf
场景5:数据备份和传输 ¶
# 查看并备份
cat data.txt | tee backup.txt | gzip > data.txt.gz
# 查看并处理
cat raw.csv | tee backup.csv | sort | uniq > processed.csv
五、管道中的数据流向 ¶
# 基本流向
echo "hello" | tee file.txt
# ┌─────────┐ ┌─────┐ ┌──────────┐
# │ echo │────▶│ tee │────▶│ 屏幕 │
# └─────────┘ └─────┘ └──────────┘
# │
# ▼
# ┌──────────┐
# │ file.txt │
# └──────────┘
# 多个管道
echo "hello" | tee file.txt | wc -l
# ┌─────────┐ ┌─────┐ ┌───────┐
# │ echo │────▶│ tee │────▶│ wc -l │
# └─────────┘ └─────┘ └───────┘
# │
# ▼
# ┌──────────┐
# │ file.txt │
# └──────────┘
六、常见问题解决 ¶
1. 写入没有权限的文件 ¶
# 使用 sudo
echo "内容" | sudo tee /etc/config.conf
2. 追加而不是覆盖 ¶
# 使用 -a 选项
echo "新内容" | sudo tee -a /etc/config.conf
3. 同时写入 stderr 和 stdout ¶
# 将错误也一起保存
./script.sh 2>&1 | tee output.log
4. 不需要屏幕显示 ¶
# 只保存到文件(不显示)
echo "内容" | tee file.txt > /dev/null
七、对比重定向 ¶
| 命令 | 屏幕显示 | 写入文件 | 适用场景 |
|---|---|---|---|
echo "a" > file |
❌ | ✅ | 只需要保存 |
| `echo "a" | tee file` | ✅ | ✅ |
| `echo "a" | tee -a file` | ✅ | ✅(追加) |
| `echo "a" | sudo tee file` | ✅ | ✅(root) |
八、快速参考表 ¶
| 需求 | 命令 |
|---|---|
| 显示并保存 | `command |
| 追加到文件 | `command |
| 写入多个文件 | `command |
| 需要 root 权限 | `echo "x" |
| 需要 root 追加 | `echo "x" |
| 显示但不需要保存 | command(不用 tee) |
| 保存但不显示 | command > file.txt |
九、实用组合 ¶
# 1. 查看系统信息并保存
uname -a | tee sysinfo.log
dmesg | tee dmesg.log
# 2. 监控日志并保存
tail -f /var/log/nginx/access.log | tee access.log
# 3. 安装软件记录输出
apt-get install nginx 2>&1 | tee install.log
# 4. 查看并统计
cat bigfile.txt | tee copy.txt | wc -l
# 5. 备份和压缩
cat data.sql | tee backup.sql | gzip > data.sql.gz
总结 ¶
tee 的核心作用:
- 同时查看和保存输出
- 写入多个文件(包括需要 root 权限的文件)
- 追加内容而不覆盖
- 管道中间传递数据
最常用场景:
# 写入系统配置文件
echo "配置" | sudo tee /etc/file.conf
# 查看命令输出并记录日志
command | tee log.txt