在 Linux 中查看历史命令主要通过 history
命令和相关技巧实现,以下是详细说明:
history
$HISTSIZE
环境变量控制)。bash
history 10 # 显示最近10条
bash
history -c # 清空当前会话的历史(不会删除文件)
bash
history -d 123 # 删除第123条记录
bash
Ctrl + R # 输入关键词反向搜索,按 `Ctrl+R` 继续上翻
grep
:
bash
history | grep "apt"
bash
!123 # 执行历史中第123条命令
bash
!!
bash
!apt # 执行最近一条以 `apt` 开头的命令
.bash_history
文件中(Bash 终端):
bash
cat ~/.bash_history
bash
history -a # 立即追加当前会话命令到文件
bash
export HISTSIZE=10000 # 内存中保存的条数
export HISTFILESIZE=20000 # 文件中的最大条数
~/.bashrc
或 ~/.bash_profile
中。在 ~/.bashrc
中添加:
export HISTIGNORE="pwd:ls:history" # 忽略pwd、ls等命令
启用历史命令的时间戳:
export HISTTIMEFORMAT="%F %T " # 在~/.bashrc中添加
history # 显示带时间的记录
~/.zsh_history
,使用 history
或 fc -l
。~/.local/share/fish/fish_history
。通过以上方法,你可以高效地查看、搜索和管理 Linux 历史命令。