Linux-history的用法
history: history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...]
history的作用是显示或操纵历史列表。
相关选项:
-c 删除所有条目从而清空历史列表。
使用 HISTTIMEFORMAT 显示时间戳
当你从命令行执行 history 命令后,通常只会显示已执行命令的序号和命令本身。如果你想要查看命令历史的时间戳,那么可以执行:
1 [root@localhost ~]# export HISTTIMEFORMAT='%F %T ' #使用 HISTTIMEFORMAT 显示时间戳 2 [root@localhost ~]# history | tail -5 #显示最近5条命令,可以从上面看到命令的使用时间记录 3 22 2019-03-20 02:36:33 ls 4 23 2019-03-20 02:38:05 export HISTTIMEFORMAT='%F %T ' 5 24 2019-03-20 02:38:09 history 6 25 2019-03-20 02:41:36 export HISTTIMEFORMAT='%F %T ' 7 26 2019-03-20 02:41:44 history | tail -5
快速重复执行上一条命令
有 4 种方法可以重复执行上一条命令:
1. 使用上方向键,并回车执行。
2. 按 !! 并回车执行。
3. 输入 !-1 并回车执行。
4. 按 Ctrl+P 并回车执行。
从命令历史中执行一个指定的命令
在下面的例子中,如果你想重复执行第 31 条命令,那么可以执行 !31:
1 [root@localhost ~]# history | tail -5 #显示最近5条记录 2 28 2019-03-20 02:46:49 pwd 3 29 2019-03-20 02:47:05 history | tail -5 4 30 2019-03-20 02:47:09 ls 5 31 2019-03-20 02:47:16 pwd 6 32 2019-03-20 02:48:35 history | tail -5 7 [root@localhost ~]# !31 #执行第31条命令 8 pwd 9 /root 10 [root@localhost ~]#
通过指定关键字来执行以前的命令
在下面的例子,输入 !history 并回车,将执行以 history 打头的命令:
1 [root@localhost ~]# !history #显示并执行以history打头的命令 2 history | tail -5 #执行history | tail -5命令 3 30 2019-03-20 02:47:09 ls 4 31 2019-03-20 02:47:16 pwd 5 32 2019-03-20 02:48:35 history | tail -5 6 33 2019-03-20 02:48:46 pwd 7 34 2019-03-20 02:53:50 history | tail -5 8 [root@localhost ~]#