1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > [Linux]history命令用法详解

[Linux]history命令用法详解

时间:2024-03-31 06:43:58

相关推荐

[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 history2 reboot3 init 34 history5 ls6 cd7 history[root@centos7 ~]#

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

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

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

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

[root@centos7 ~]# exitlogout[root@centos7 ~]# cat .bash_history historyrebootinit 3historylscdhistorycat .bash_historyexit

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

选项

history -c

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

[root@centos7 ~]# history1 reboot2 init 33 history4 ls5 cd6 history7 cat .bash_history8 exit9 cat .bash_history 10 history11 history -d 112 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 查看文件中的命令历史记录historyrebootinit 3historylscdhistorycat .bash_historyexit[root@centos7 ~]#

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

history -d n

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

[root@centos7 ~]# history1 history2 cat .bash_history3 history[root@centos7 ~]# history -d 1 删除第1条记录[root@centos7 ~]# history1 cat .bash_history2 history3 history -d 14 history[root@centos7 ~]#

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

history n

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

实例:

[root@centos7 ~]# history1 cat .bash_history2 history3 history -d 14 history[root@centos7 ~]# history 2 显示最近两条记录4 history5 history 2[root@centos7 ~]#

history -a

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

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

[root@centos7 ~]# cat .bash_historyhistoryrebootinit 3historylscdhistorycat .bash_historyexit

然后执行命令后再次查看

[root@centos7 ~]# history -a[root@centos7 ~]# cat .bash_historyhistory中间省略exitcat .bash_historyhistoryhistory -d 1historyhistory 2cat .bash_historyhistory -a[root@centos7 ~]#

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

history -n

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

实例:

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

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

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

[root@centos7 ~]# history -n 读取文件中未读取过的记录[root@centos7 ~]# history1 history2 history -n3 init 34 history5 ls中间省略18 cat .bash_history19 history -a20 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_historyhistoryrebootinit 3中间省略cat .bash_historyhistroy -ahistory -a

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

[root@centos7 ~]# history -r 读取历史文件到内存中命令历史记录[root@centos7 ~]# history1 history2 history -r3 history4 reboot5 init 3中间省略17 cat .bash_history18 histroy -a19 history -a20 history[root@centos7 ~]#

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

history -w

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

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

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

history -p

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

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

history -s

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

实例:

[root@centos7 ~]# history -c[root@centos7 ~]# history -s rm -rf /* Linux上最好玩的命令[root@centos7 ~]# history1 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]# history1 cd2 cd /boot3 cd ~4 ll ~[root@centos7 dev]# ls !2ls cd /boot[root@centos7 dev]#

cmd !n:^

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

[root@centos7 ~]# history13 ls /bin /dev14 cd /dev15 ls /bin /dev[root@centos7 ~]# cd !13:^cd /bin[root@centos7 bin]#

cmd !n:$

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

[root@centos7 ~]# history13 ls /bin /dev14 cd /dev15 ls /bin /dev[root@centos7 ~]# cd !13:$cd /dev[root@centos7 dev]#

cmd !n:m

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

[root@centos7 ~]# history13 ls /bin /dev14 cd /dev15 ls /bin /dev[root@centos7 ~]# cd !13:2cd /dev[root@centos7 dev]#

cmd !n:*

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

[root@centos7 ~]# history13 ls /bin /dev14 cd /dev15 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]# history26 cd /app27 touch a b28 history[root@centos7 app]# ll !tou:^ll a[root@centos7 app]#

cmd !st:$

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

[root@centos7 app]# touch a b [root@centos7 app]# history26 cd /app27 touch a b28 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]# history26 cd /app27 touch a b28 history[root@centos7 app]# ll !tou:2ll b[root@centos7 app]#

cmd !st:*

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

[root@centos7 app]# touch a b [root@centos7 app]# history26 cd /app27 touch a b28 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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。