查看 Linux 历史记录范围 100 到 200 | AI生成和翻译

Home 2026.02

Question: 在 Linux history command 中,如何显示第 100个最近到第 200个最近的命令(不仅仅是最后 100 个)?

Answer:

history command 对条目进行编号,从最早的(较小的数字)到最新的(较大的数字)。 要查看从 第 100 个最近第 200 个最近 的命令,你需要使用 negative offsets 或计算绝对行号。

以下是最实用的方法:

1. 最简单且推荐的方法(使用 negative offsets)

history | tail -n 200 | head -n 100

这表示:

2. 更明确的版本(逻辑非常清晰)

# 显示从第 100 个最后 → 第 200 个最后
history | tail -n +101 | head -n 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:


Back Donate