一.作用
history 命令用来显示执行过的命令,也可以根据显示的命令重新执行需要的命令。
二.用法
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg…]
ps.官方,以下为个人解释。
-c 清空当前history list缓存
-d 删除list中的某个命令,后面跟的是number号,就是上面的offset
n 显示最近n条命令
-a append 添加当前会话history list中的命令历史到文件中去
-n new 只添加history list中没有从history file中添加过的命令
-r read 读取history file中的文件到当前session的history file中
-w write 把当前history file中的命令覆盖到history文件中去。
可以跟上另外的history file做备份,也可以配合 -c 先清空然后再默认不加[filename],
清空~/.bash_history 文件。
-p 执行但是不记录到history list中
-s 会把参数添加到history list中去,但是并不执行
三.实例
查看最近10条执行的历史命令
history 10
当你从命令行执行 history 命令后,通常只会显示已执行命令的序号和命令本身。如果你想要查看命令历史的时间戳,那么可以执行:
export HISTTIMEFORMAT='%F %T '
history | more
将本次登录的命令写入历史文件中:
history -w
将命令历史文件中的内容读入到目前shell的history记忆中 :
history -r
将当前Shell会话的历史命令追加到命令历史文件中:
history -a
清空当前历史命令列表
history -c