[Linux]history命令用法详解

history命令

简介


基本

在Linux中,history命令是用于显示历史执行命令以及读取命令历史文件中的历史执行的命令到内存中,或者从内存中把执行命令的历史写入到保存历史执行命令的文件中的内部命令

语法:history (选项) (参数)

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


命令历史储存文件 .bash_history

储存命令历史的文件在

~/bash_history

当我们登陆shell的时候,系统会将保存在文件中的命令历史读取到内存中,所以我们直接键入 history 便可以查询命令历史。

[root@centos7 ~]# history   查看内存中命令历史记录
    1  history
    2  reboot
    3  init 3
    4  history
    5  ls
    6  cd
    7  history
[root@centos7 ~]# 

直接键入history查询当前内存中已存在的历史,那么这个时候.bash_history文件中的历史也是这样的吗?

[root@centos7 ~]# cat .bash_history  查看文件中命令历史记录
history
reboot
[root@centos7 ~]# 

很明显,在.bash_history中储存的命令历史截止到了reboot重启命令。

当计算机正常执行命令关闭、重启或者用户正常退出的时候系统便会将内存中的命令历史写入到.bash_history中去,当用户重新登陆后又会将之前储存在.bash_history文件中的命令历史读取到内存中。

[root@centos7 ~]# exit
logout
[root@centos7 ~]# cat .bash_history 
history
reboot
init 3
history
ls
cd
history
cat .bash_history
exit

重新登陆后便可以发现.bash_history文件中的命令历史已经更新了,截止到exit


选项

history -c

history -c命令用于清空命令历史记录。注意:这里只是清理内存中的历史记录在.bash_history中的命令记录不会被该命令清除,我将在下面做实例演示。

[root@centos7 ~]# history
    1  reboot
    2  init 3
    3  history
    4  ls
    5  cd
    6  history
    7  cat .bash_history
    8  exit
    9  cat .bash_history 
   10  history
   11  history -d 1
   12  history
[root@centos7 ~]# 

键入history后当前内存中的命令历史如上所示。

[root@centos7 ~]# history -c   清除内存中的命令历史记录
[root@centos7 ~]# history      查看内存中的命令历史记录
    1  history
[root@centos7 ~]# 

当键入history -c命令后再次键入history便可发现内存中的命令历史已经清空,当前命令历史只剩下history -c后的一条命令,那么现在.bash_history中的命令历史还在吗?

[root@centos7 ~]# cat .bash_history  查看文件中的命令历史记录
history
reboot
init 3
history
ls
cd
history
cat .bash_history
exit
[root@centos7 ~]# 

由此可见history -c只是清除内存中的命令历史,.bash_history中的命令历史不会被清除。


history -d n

history -d n 用于删除使用history查询的内存中的命令历史中指定的第n个命令,也就是说删除的是内存中的命令历史,与.bash_history 文件无关。

[root@centos7 ~]# history
    1  history
    2  cat .bash_history
    3  history
[root@centos7 ~]# history -d 1 删除第1条记录
[root@centos7 ~]# history
    1  cat .bash_history
    2  history
    3  history -d 1
    4  history
[root@centos7 ~]# 

由上面的实例可以看到,我第一次查询命令历史第1条命令是history,当执行history -d 1后第1条命令历史变成了cat .bash_history,在第1次查询命令历史的时候该命令是第2条。当history -d n执行成功后被删除的那条历史命令就会被后面的历史所替代,后面的命令历史都向前靠拢。


history n

history n用于显示最近n条命令。

实例:

[root@centos7 ~]# history
    1  cat .bash_history
    2  history
    3  history -d 1
    4  history
[root@centos7 ~]# history 2  显示最近两条记录
    4  history
    5  history 2
[root@centos7 ~]# 

history -a

history -a用于将内存中的命令历史记录追加到.bash_history中而不是覆盖。

第一步先看看历史文件中有哪些命令历史

[root@centos7 ~]# cat .bash_history
history
reboot
init 3
history
ls
cd
history
cat .bash_history
exit

然后执行命令后再次查看

[root@centos7 ~]# history -a
[root@centos7 ~]# cat .bash_history
history
中间省略
exit
cat .bash_history
history
history -d 1
history
history 2
cat .bash_history
history -a
[root@centos7 ~]# 

命令生效。注意:这里是追加在后面而不是覆盖整个文件内容。


history -n

history -n用于将.bash_history中不存在于内存中的命令历史读取到内存中去。这里注意是只读取内存中命令历史没有的命令历史,如果内存中已经存在的相同命令历史就不会再次读取。

实例:
因为现在内存中命令历史比.bash_history中的多,所以我先执行history -c命令删除内存中的命令然后再执行history -n查看效果。

[root@centos7 ~]# history -c
[root@centos7 ~]# history
    1  history
[root@centos7 ~]# 

以上为清除内存中命令历史的操作

[root@centos7 ~]# history -n 读取文件中未读取过的记录
[root@centos7 ~]# history
    1  history
    2  history -n
    3  init 3
    4  history
    5  ls
  中间省略
   18  cat .bash_history
   19  history -a
   20  history
[root@centos7 ~]# 

由上可见history -n命令是直接把.bash_history中的命令历史读取到内存中已存在的命令历史后面,而不是前面。


history -r

history -r 读历史文件附加到历史列表,也就是把.bash_history中的命令历史记录直接加到内存中命令历史记录后面,与history -n不同的是history -n只读取没有读过的内容。
那么为了实验我还是要先执行history -c清理一下内存中的历史记录然后再执行history -r,清理操作我就不再做演示。

[root@centos7 ~]# cat .bash_history
history
reboot
init 3
中间省略
cat .bash_history
histroy -a
history -a

先查看.bash_history文件确认记录内容

[root@centos7 ~]# history -r 读取历史文件到内存中命令历史记录
[root@centos7 ~]# history
    1  history
    2  history -r
    3  history
    4  reboot
    5  init 3
    中间省略
   17  cat .bash_history
   18  histroy -a
   19  history -a
   20  history
[root@centos7 ~]# 

如上所示.bash_history中的记录已读取


history -w

history -w用于用内存中的命令历史记录覆盖.bash_history中的记录,直接覆盖毫不留情。

[root@centos7 ~]# history -c 清空内存中的记录
[root@centos7 ~]# history
    1  history
[root@centos7 ~]# history -w 将内存中的记录覆盖到文件中
[root@centos7 ~]# cat .bash_history 
history
history -w
[root@centos7 ~]# 

如上所示,内存中的记录覆盖到了文件中。


history -p

history -p 可以展开历史参数成多行,但不存在历史列表中

[root@centos7 ~]# history -c
[root@centos7 ~]# history -p aa bb 
aa
bb
[root@centos7 ~]# history
    1  history
[root@centos7 ~]# 

history -s

history -s这个命令相当于直接在内存记录中添加一条内容。

实例:

[root@centos7 ~]# history -c
[root@centos7 ~]# history -s rm -rf /*  Linux上最好玩的命令
[root@centos7 ~]# history
    1  rm -rf /1 /app /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /path /proc /root /run /sbin /srv /sys /tmp /usr /var 
    2  history
[root@centos7 ~]# 

如上所示,虽然内存历史记录里面有这么一条,但是并没有执行过。


调用历史参数

简介

cmd代表任意命令

cmd !^ : 利用上一个命令的第一个参数做cmd的参数
cmd !$ : 利用上一个命令的最后一个参数做cmd的参数
cmd !* : 利用上一个命令的全部参数做cmd的参数
cmd !:n : 利用上一个命令的第n个参数做cmd的参数
cmd !n:^ 调用第n条命令的第一个参数
cmd !n:$ 调用第n条命令的最后一个参数
cmd !n:m 调用第n条命令的第m个参数
cmd !n:* 调用第n条命令的所有参数
cmd !st:^ 从命令历史中搜索以 st 开头的命令 ,并获取它的第一个参数
cmd !st:$ 从命令历史中搜索以 st 开头的命令 ,并获取它的最后一个参数
cmd !st:n 从命令历史中搜索以 st 开头的命令 ,并获取它的第n个参数
cmd !st:* 从命令历史中搜索以 st 开头的命令 ,并获取它的所有参数


所有实例均省略不必要内容


cmd !^

利用上一个命令的第一个参数做cmd的参数

实例:

[root@centos7 ~]# ll /
[root@centos7 ~]# cd !^
cd /
[root@centos7 /]#

cmd !$

利用上一个命令的最后一个参数做cmd的参数

实例:

[root@centos7 ~]# ls /bin /dev
[root@centos7 ~]# cd !$
cd /dev
[root@centos7 dev]# 

cmd !*

利用上一个命令的全部参数做cmd的参数

实例:

[root@centos7 ~]# ls /bin /dev
[root@centos7 dev]# ll !*
ll /bin /dev
[root@centos7 dev]#

cmd !:n

利用上一个命令的第n个参数做cmd的参数

实例:

[root@centos7 dev]# history
    1  cd
    2  cd /boot
    3  cd ~
    4  ll ~
[root@centos7 dev]# ls !2
ls cd /boot
[root@centos7 dev]# 

cmd !n:^

调用第n条命令的第一个参数

[root@centos7 ~]# history
   13  ls /bin /dev
   14  cd /dev
   15  ls /bin /dev
[root@centos7 ~]# cd !13:^
cd /bin
[root@centos7 bin]# 

cmd !n:$

调用第n条命令的最后一个参数

[root@centos7 ~]# history
   13  ls /bin /dev
   14  cd /dev
   15  ls /bin /dev
[root@centos7 ~]# cd !13:$
cd /dev
[root@centos7 dev]# 

cmd !n:m

调用第n条命令的第m个参数

[root@centos7 ~]# history
   13  ls /bin /dev
   14  cd /dev
   15  ls /bin /dev
[root@centos7 ~]# cd !13:2
cd /dev
[root@centos7 dev]# 

cmd !n:*

调用第n条命令的所有参数

[root@centos7 ~]# history
   13  ls /bin /dev
   14  cd /dev
   15  ls /bin /dev
[root@centos7 ~]# ll !13:*
ll /bin /dev
[root@centos7 ~]# 

cmd !st:^

从命令历史中搜索以 st 开头的命令 ,并获取它的第一个参数

[root@centos7 app]# touch a b 在/app下创建 a b两个文件
[root@centos7 app]# history
   26  cd /app
   27  touch a b
   28  history
[root@centos7 app]# ll !tou:^
ll a
[root@centos7 app]# 

cmd !st:$

从命令历史中搜索以 st 开头的命令 ,并获取它的最后1个参数

[root@centos7 app]# touch a b 
[root@centos7 app]# history
   26  cd /app
   27  touch a b
   28  history
[root@centos7 app]# ll !tou:$
ll b
[root@centos7 app]# 

cmd !st:n

从命令历史中搜索以 st 开头的命令 ,并获取它的第n个参数

[root@centos7 app]# touch a b 
[root@centos7 app]# history
   26  cd /app
   27  touch a b
   28  history
[root@centos7 app]# ll !tou:2
ll b
[root@centos7 app]# 

cmd !st:*

从命令历史中搜索以 st 开头的命令 ,并获取它的所有参数

[root@centos7 app]# touch a b 
[root@centos7 app]# history
   26  cd /app
   27  touch a b
   28  history
[root@centos7 app]# ll !tou:*
ll a b
[root@centos7 app]# 

命令历史相关环境变量

HISTSIZE:命令历史记录的条数,默认1000 HISTFILE:指定历史文件,默认为~/.bash_history

HISTFILESIZE:命令历史文件记录历史的条数

HISTIGNORE=“str1:str2:… “ 忽略string1,string2历史 控制命令历史的记录方式:

环境变量:HISTCONTROL
ignoredups 默认,忽略重复的命令,连续且相同为“重复“

ignorespace忽略所有以空白开头的命令

ignoreboth 相当于ignoredups, ignorespace的组合

export 变量名=”值“ 存放在 /etc/profile 或 ~/.bash_profile

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
历史命令Linux/Unix系统中的一个常用命令,用于查看当前用户在命令行中执行过的命令历史记录。通过使用history命令,您可以方便地查看、搜索、编辑和执行之前执行过的命令,从而提高操作的便利性和效率。 使用history命令可以显示当前会话中执行过的命令历史记录。默认情况下,它会显示最近执行的命令列表,每个命令前面都有一个数字作为索引。您可以使用这些索引来重新执行以前的命令。例如,要重新执行索引为10的命令,可以使用!10命令。 您可以通过history命令的选项来扩展其功能。例如,使用history -c命令可以清空当前会话的命令历史记录。这在某些情况下可能很有用,比如当您希望清除敏感信息或者只想保留当前会话的命令历史记录。 另一个有用的选项是history | grep命令,它可以在历史命令记录中搜索特定的命令。例如,如果您想找到所有包含"ls"的历史命令,可以使用history | grep ls命令。这对于查找特定命令的使用情况或者回顾之前执行过的类似命令非常有帮助。 您还可以使用HISTSIZE环境变量来设置历史命令记录的长度。默认情况下,命令历史记录的长度是1000个命令。通过设置HISTSIZE环境变量,您可以增加或减少历史命令记录的长度。例如,要将历史命令记录的长度设置为2000,可以在命令行中输入export HISTSIZE=2000。 综上所述,history命令Linux/Unix系统中一个非常有用的命令,它允许用户查看、搜索、编辑和执行之前执行过的命令历史记录。通过使用history命令的不同选项,您可以进一步扩展其功能,提高命令行操作的效率和便利性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值