history命令来自英文单词“历史”,其功能是用于显示与管理历史命令记录。Linux系统默认会记录用户所执行过的所有命令,可以使用history命令查阅它们,也可以对其记录进行修改和删除操作。

语法格式:history [参数]

常用参数:

-a 保存命令记录
-c 清空命令记录
-d 删除指定序号的命令记录
-n 读取命令记录
-r 读取命令记录到缓冲区
-s 添加命令记录到缓冲区
-w 将缓冲区信息写入到历史文件

参考示例

显示执行过的全部命令记录:

[root@linuxcool ~]# history 
1  vim /etc/sysconfig/network-scripts/ifcfg-ens160
2  reboot
3  vim /etc/sysconfig/network-scripts/ifcfg-ens160
4  vim /etc/yum.repos.d/rhel.repo
5  mkdir /media/cdrom
6  vim /etc/fstab
7  reboot
8  ping 192.168.10.10
9  dnf install httpd
10  exit
………………省略部分输出信息………………

显示执行过的最近5条命令:

[root@linuxcool ~]# history 5
11  exit
12  ifconfig
13  vim /etc/hostname
14  reboot
15  history

将本次缓存区信息写入到历史文件中(~/.bash_history):

[root@linuxcool ~]# history -w

将历史文件中的信息读入到当前缓冲区中:

[root@linuxcool ~]# history -r  

将本次缓冲区信息追加写入到历史文件中(~/.bash_history):

[root@linuxcool ~]# history -a  

清空本次缓冲区及历史文件中的信息:

[root@linuxcool ~]# history -c 
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。