history 命令用于查询已执行的历史命令。
常用参数:
n :数字,表示列出最近n行命令
-c :将目前的shell中的所有的history内容删除
-a:将目前新增的history命令新增入histfile中,若没有加hisfiles,则默认写入~/.bash_history
-r:将histfiles的内容读到目前这个shell 的history记忆中。
-w:将目前的history记忆内容写入histfiles中。
查看所有history记忆中的命令:
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# history 1 df-h 2 reboot 3 ssh esggy-qa-n013 4 yum install nfs-utils 5 yum install parted 6 yum install nfs-utils …
查看history的最后6条命令:
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# history 6 395 su - trafodion 396 hive 397 history 398 history n 399 history 3 400 history 6
将目前的已执行的命令添加到histfile中,默认为~/.bash_history并查看文件内容:
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# history -w [root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# cat ~/.bash_history | tail -n 10 clear hive su - trafodion hive history history n history 3 history 6 echo $HISTORY history -w
将新增的history命令加到histfile中,并查看文件内容:
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# history -a [root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# cat ~/.bash_history | tail -n 10 history n history 3 history 6 echo $HISTORY history -w echo $HISTORY cat ~/.bash_history cat ~/.bash_history | tail 10 cat ~/.bash_history | tail -n 10 history -a
查看$HISTSIZE变量大小(~/.bash_history文件能记录的命令数量由$HISTSIZE决定):
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# echo $HISTSIZE 1000
使用!执行命令:
! 命令行号 (执行history中指定行号的命令)
! 字符串 (执行最近以指定字符串开头的命令)
!! (执行上一条命令)
[root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# history 5 406 cat ~/.bash_history | tail -n 10 407 history -a 408 cat ~/.bash_history | tail -n 10 409 echo $HISTSIZE 410 history 5 [root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# !410 history 5 406 cat ~/.bash_history | tail -n 10 407 history -a 408 cat ~/.bash_history | tail -n 10 409 echo $HISTSIZE 410 history 5 [root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# !! history 5 406 cat ~/.bash_history | tail -n 10 407 history -a 408 cat ~/.bash_history | tail -n 10 409 echo $HISTSIZE 410 history 5 [root@abcdefghijklmnopqrstuvwxyzabcdefghijkl123456789012~]# !ec echo $HISTSIZE 1000
转载于:https://blog.51cto.com/mjal01/1963004