查看 Linux 历史记录范围 100 到 200 | AI生成和翻译
Question: 在 Linux history command 中,如何显示第 100个最近到第 200个最近的命令(不仅仅是最后 100 个)?
Answer:
history command 对条目进行编号,从最早的(较小的数字)到最新的(较大的数字)。
要查看从 第 100 个最近 到 第 200 个最近 的命令,你需要使用 negative offsets 或计算绝对行号。
以下是最实用的方法:
1. 最简单且推荐的方法(使用 negative offsets)
history | tail -n 200 | head -n 100
这表示:
- 获取最后 200 条命令(最近的 200 行)
- 然后取其中的前 100 条 → 结果恰好是第 200 个最旧到第 101 个最旧的命令(即第 100 个最近到第 200 个最近)
2. 更明确的版本(逻辑非常清晰)
# 显示从第 100 个最后 → 第 200 个最后
history | tail -n +101 | head -n 100
tail -n +101→ 跳过最新的 100 行,从第 101 个最新的开始head -n 100→ 之后取接下来的 100 行
3. 使用负数范围 (bash 4+)
# 显示从 -200 到 -100 的 history 条目(闭区间)
for i in {100..200}; do history -$i; done
或者更短(但可读性较差)的方式:
history | sed -n '101,200p'
快速总结 – 最常见的实用模式
| 需求 | Command |
|---|---|
| 最后 100 条命令 | history 100 或 history | tail -100 |
| 100–200 条之前的命令(你的问题) | history | tail -n 200 | head -n 100 |
| 101–200 条之前的命令 | history | tail -n +101 | head -n 100 |
| 50–150 条之前的命令 | history | tail -n 150 | head -n 100 |
| 最后 300 条 → 跳过最新的 100 条 | history | tail -n 300 | head -n 200 |
针对你的具体情况,最易读且最常用 的版本是:
history | tail -n 200 | head -n 100
References:
- None (标准 Linux/POSIX
history,tail,head行为)