命令简介

history命令用于显示历史记录和执行过的指令

命令语法



history [-c] [-d offset] [n]
history -anrw [filename]
history -ps arg [arg...]



命令选项



-c: 清空命令历史
-d offset: 删除历史中指定的第offset个命令
-a: 追加本次会话新执行的命令历史列表至历史文件
-n: 读历史文件中未读过的行到历史列表
-r: 读历史文件附加到历史列表
-w: 保存历史列表到指定的历史文件
-p: 展开历史参数成多行,但不存在历史列表中
-s: 展开历史参数成一行,附加在历史列表后



示例

1.显示最近5条命令



[root@centos7 ~]# history 5
   11  who am i
   12  whoami
   13  who am i
   14  echo $HISTCONTROL
   15  history 5



2.调用历史中的命令

(1)重复执行第N条命令 (!N),重复执行倒数第N条命令(!-N)



[root@centos7 ~]# !12
whoami
root

[root@centos7 ~]# !-2
cat /etc/centos-release
CentOS Linux release 7.5.1804 (Core)



(2)调用上一条命令,有以下几种方法

          ① 按上方向键,并回车执行

          ② 输入 !! 并回车执行

          ③ 输入 !-1 并回车执行

          ④ 按Ctrl+p并回车执行



[root@centos7 ~]# !!
hostname
centos7



(3)执行前一条去除参数的命令(!:0)



[root@centos7 ~]# id -u root
0

[root@centos7 ~]# !:0
id
uid=0(root) gid=0(root) groups=0(root)



(4)执行最近一次以string开头的命令(!string)



[root@centos7 ~]# !una
uname -r
3.10.0-862.9.1.el7.x86_64



!string:p 表示仅打印命令历史,而不执行

(5)执行最近一次包含string的命令(!?string)



[root@centos7 ~]# !?centos
cat /etc/centos-release
CentOS Linux release 7.5.1804 (Core)



(6)上一条命令的删除与替换

        ① ^string删除上一条命令中的第一个string



[root@centos7 ~]# touch file1 file2 file3 [root@centos7 ~]# ^file touch 1 file2 file3



        ② ^string1^string2将上一条命令中的第一个string1替换为string2



[root@centos7 ~]# touch file1 file2 file3 [root@centos7 ~]# ^file^FILE touch FILE1 file2 file3



        ③ !:gs/string1/string2将上一条命令中所有的string1都替换为string2



[root@centos7 ~]# touch file1 file2 file3 [root@centos7 ~]# !:gs/file/FILE touch FILE1 FILE2 FILE3



(7)命令搜索

Ctrl+r来在命令历史中搜索命令(reverse-i-search)`’:
Ctrl+g:从历史搜索模式退出

3.调用历史参数

(1)调用上一条命令的最后一个参数,有以下几种方法

         ① !$ 表示
         ② Esc, .(点击Esc键后松开,然后点击. 键)
         ③ Alt+ .(按住Alt键的同时点击. 键)。要使用此按键,SecureCRT中需要如下设置:Options→Session Options→Terminal→Emulation→Emacs中的 “ Use ALT as meta key ”勾选上;Xshell中需要如下设置:文件→属性→终端→键盘 中的 “将ALT用作Meta键 ”勾选上



[root@centos7 ~]# touch file1 file2 file3

[root@centos7 ~]# ll !$
ll file3
-rw-r--r-- 1 root root 0 Jul 24 14:17 file3



同理 :   !$:p 打印输出上一条命令的最后一个参数的内容。command !n:$ 调用第n条命令的最后一个参数。command !string:$ 从命令历史中搜索以string 开头的命令,并获取它的最后一个参数

(2)调用上一条命令的第一个参数(!^)



[root@centos7 ~]# touch file1 file2 file3 

[root@centos7 ~]# ll !^
ll file1
-rw-r--r-- 1 root root 0 Jul 24 14:08 file1



 同理 :   !^:p 打印输出上一条命令的第一个参数的内容。command !n:^ 调用第n条命令的第一个参数。command !string:^ 从命令历史中搜索以string 开头的命令,并获取它的第一个参数

(3)调用上一条命令的第n个参数(!:n)



[root@centos7 ~]# touch file1 file2 file3 

[root@centos7 ~]# ll !:2
ll file2
-rw-r--r-- 1 root root 0 Jul 24 14:14 file2



同理 :  !*:p打印输出!上一条命令的所有参数的内容。command !n:m调用第n条命令的第m个参数。command !string:n 从命令历史中搜索以string 开头的命令,并获取它的第n个参数

(4)调用上一条命令的全部参数(!*) 



[root@centos7 ~]# touch file1 file2 file3 

[root@centos7 ~]# ll !*
ll file1 file2 file3
-rw-r--r-- 1 root root 0 Jul 24 14:11 file1
-rw-r--r-- 1 root root 0 Jul 24 14:11 file2
-rw-r--r-- 1 root root 0 Jul 24 14:11 file3



同理 :  command !n:* 调用第n条命令的所有参数。command !string:* 从命令历史中搜索以string 开头的命令,并获取它的所有参数

4.控制命令历史的记录方式 : HISTCONTROL



ignoredups    默认,忽略重复的命令,连续且相同为“重复”
ignorespace   忽略所有以空白开头的命令
ignoreboth    相当于ignoredups, ignorespace的组合
erasedups     删除重复命令



[root@centos7 ~]# export HISTCONTROL=ignoreboth



想要永久生效则写入/etc/profile 或~/.bash_profile文件中

5.HISTORY相关环境变量,定制history功能



HISTSIZE:命令历史记录的条数,默认为1000
HISTFILE:指定历史文件,默认为~/.bash_history
HISTFILESIZE:命令历史文件记录历史的条数,默认为1000
HISTTIMEFORMAT=“%F %T “ 显示时间
HISTIGNORE=“str1:str2*:… “ 忽略str1命令,str2开头的历史



history显示时间,用户,ip



[root@centos7 ~]# USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
[root@centos7 ~]# export HISTTIMEFORMAT="%F %T `whoami` ${USER_IP} "
[root@centos7 ~]# history 1
   74  2018-07-24 15:11:04 root 192.168.249.1 history 1



6.清空历史记录

清空历史命令文件



[root@centos7 ~]# > .bash_history



清空当前缓存中的命令



[root@centos7 ~]# history -c