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