1.history命令
history命令可以查看执行过的历史命令。
其命令格式为:
[root@shell ~]# history[n]
[root@shell ~]# history [-c]
[root@shell ~]# history [-raw] historyfiles
- n:数字,列出最近执行的n个指令
- -c:将目前shell中的所有history指令全部消除
- -a:将目前新增的history指令加入hisfiles中,如果没有加histfiles,默认写入~/.bash_history
- -r:将histfile内容读取到当前shell的history缓存中
- -w:将目前的history缓存内容写入到histfiles
2.系统中的历史指令
正常情况下,Linux中历史指令的读取和存储过程如下:
- 以bash登录Linux后,系统从HOME目录中的~/.bash_history文件读取历史指令
- .bash_history中存放的指令数由HISTFILESIZE变量设定
- 当前shell中的历史指令会在注销的时候写入~/.bash_history文件
-
history -w
可以强制立刻写入
3. 历史指令的使用(重要)
历史指令可以结合以下指令配合:
-
!number
:执行第number个指令 -
!command
:由最近的指令向前搜寻指令串开头为command的指令,并执行 -
!!
:执行上一个指令