tail · 看文件的尾巴
上游:cat(cat 看全部,tail 只看末尾)· grep(组合查日志) 下游:管道(tail + grep 是日志调试黄金组合)· shell-script(脚本里常用)
一句话是什么
tail 只显示文件的最后几行——看日志时几乎每次都要用它,因为最新的事件永远在文件末尾。
为什么需要它
MC 服务器运行几小时后,latest.log 可能有上万行。你想看”最近发生了什么”,用 cat 会刷屏几秒,还找不到重点。用 tail:
tail ~/mc-server/logs/latest.log
# 只显示最后 10 行,正好是刚刚发生的事更强的用法:实时盯着日志,每当服务器有新动静就立刻显示:
tail -f ~/mc-server/logs/latest.log
# 像视频直播一样,新日志来一条显示一条
# 按 Ctrl+C 停止核心用法
公式
tail [选项] 文件名
常用命令
| 命令 | 说明 |
|---|---|
tail 文件 | 显示最后 10 行 |
tail -n 50 文件 | 显示最后 50 行 |
tail -f 文件 | 实时跟随:文件变了立刻显示(最重要!) |
tail -F 文件 | 跟随 + 文件被轮转/重建也能续上(比 -f 更稳) |
tail -c 200 文件 | 显示最后 200 字节(按字节而不是行) |
tail vs head
tail = 尾巴(末尾) → 看最新
head = 头(开头) → 看最早
head 的用法和 tail 完全对称:head -n 20 文件 = 显示前 20 行。
Linux 笑话
Aaron 的 MC 服务器突然断了,他用 cat latest.log 查日志。
终端刷了两秒才停下来,滚到屏幕顶部他都来不及看。
爸爸:「用 tail。」
tail -n 30 ~/mc-server/logs/latest.log三行崩溃前的日志直接展示在眼前——一个玩家放了太多 TNT,服务器内存炸了。
规律:日志文件只能看 tail,cat 不叫”看日志”,cat 叫”刷屏”。
例题精讲
📗 初探 Starter — 看备份日志末尾
# 看最近 20 行备份日志
tail -n 20 ~/logs/backup.log
# 看最后一行(最近一次备份结果)
tail -n 1 ~/logs/backup.log
# 2026-04-20 03:00:45 ✅ 备份成功:mc-worlds-20260420.tar.gz📘 应用 User — 实时监控 MC 服务器日志
# 一边玩游戏,一边看服务器日志实时输出
tail -f ~/mc-server/logs/latest.log
# 输出示例(每行都是实时的):
# [INFO] Player connected: Aaron, xuid: 2535...
# [INFO] Aaron joined the game
# [INFO] Player disconnected: Aaron
# 按 Ctrl+C 停止监控📙 管理 Admin — tail + grep 组合查错
# 只看最近 100 行里的错误信息
tail -n 100 ~/mc-server/logs/latest.log | grep -i "error"
# 实时监控,但只显示 error 行(过滤噪音)
tail -f ~/mc-server/logs/latest.log | grep -i "error"
# 服务崩了之后看最后 50 行排查
sudo journalctl -u mc-server | tail -n 50黄金组合:tail -f | grep = 实时过滤只看你关心的事。
官方文档参考
man tail # 完整手册
tail --help # 快速选项
tail显示文件末尾;-n N控制行数,默认 10;-f实时跟随(日志调试首选);-F是更稳健的-f,文件被轮转不会失联。
节点链接
上游
下游
- 管道 ← tail -f | grep 黄金组合
- shell-script ← 脚本里 tail -n 1 取最新结果
实战
- Minecraft-Bedrock-Server ← latest.log 必用 tail -f 监控
- cron ← 每日看 tail -n 30 backup.log 确认备份正常
节点版本:v1.0 · 2026-04-20 Tier 2 关键补充——日志调试从此有了眼睛