history命令 用于显示历史记录和执行过的历史命令
读取历史命令文件中的目录到历史命令缓冲区和将历史命令缓冲区中的目录写入历史命令文件
history命令单独使用时,仅显示历史记录,使用符号“!”可以执行指定序号的历史命令
例如,要执行第2个历史命令,则输入“!2”
history命令是保存在内存中的,当退出或者登录Shell时,会自动保存或读取
在内存中,仅能够存储1000条历史命令,该数量由环境变量HISTSIZE进行控制
默认并不显示命令的执行时间,但命令的执行时间history已经记录,只是没有显示而已
语法:
history [选项] [参数]
选项:
-N显示历史记录中最近的N个记录; -c清空当前历史记录; -a将历史命令缓冲区中的命令写入历史命令文件中; -r将历史命令文件中的命令读入当前历史命令缓冲区; -w将当前历史命令缓冲区中的命令写入历史命令文件中; -d<offset> 删除历史记录中第offset个命令; -n<filename> 读取指定文件
参数:
n打印最近的n条历史命令
实例1:查看历史记录
[root@Linux home]# history 1 cat redhat 2 cd /usr/local/ 3 ll ...
示例2:查看历史记录中的后5条
[root@Linux home]# history 5 700 cd .. 701 ll 702 ls -al 703 history 704 history 5
实例3:使用“!”执行历史命令,执行历史记录中的第10条命令
!number 执行第几条命令
[root@Linux home]# !702 ls -al total 20 drwxr-xr-x. 5 root root 4096 Sep 8 13:37 . dr-xr-xr-x. 19 root root 4096 Oct 11 16:45 .. drwxr-xr-x 2 root root 4096 Sep 13 2021 mysql drwx------ 2 test test 4096 Sep 8 10:27 test drwx------ 2 zhangsan zhangsan 4096 Sep 8 13:37 zhangsan
实例4:示例:!! 执行上一条命令
[root@Linux home]# !! ls -al total 20 drwxr-xr-x. 5 root root 4096 Sep 8 13:37 . dr-xr-xr-x. 19 root root 4096 Oct 11 16:45 .. drwxr-xr-x 2 root root 4096 Sep 13 2021 mysql drwx------ 2 test test 4096 Sep 8 10:27 test drwx------ 2 zhangsan zhangsan 4096 Sep 8 13:37 zhangsan
实例5:将目前所有的历史命令全部清除
# history -c