Linux 命令行小技巧 – !叹号的用处
history 的基础
HISTSIZE
变量值设置保存在历史列表中的命令数。默认情况下,该值为 500。这些先前发出的命令(称为历史列表)存储在历史文件中。它的默认位置是~/.bash_history
,这个位置存储在shell变量HISTFILE
中。!
命令将 bash 历史记录列表中的命令引入到输入中。此功能使使用者可以轻松地快速重复命令、替换文本、操作参数和修复先前命令中的拼写错误。命令重复实例
!
执行操作的一些示例。!
后跟与要运行的命令匹配的个字符(或者字符串)将重复该命令的实例:[root@localhost ~]# ls /root/
anaconda-ks.cfg centos2ol.sh employee.json list_users new.txt wsbkuplog.txt
[root@localhost ~]# !l
ls /root/
anaconda-ks.cfg centos2ol.sh employee.json list_users new.txt wsbkuplog.txt
[root@localhost ~]# !ls
ls /root/
anaconda-ks.cfg centos2ol.sh employee.json list_users new.txt wsbkuplog.txt
!?
格式与上述相同,但李世明令不必是命令的开头:[root@localhost ~]# cat employee.json
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
[root@localhost ~]# !?employee
cat employee.json
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
[root@localhost ~]# !?json
cat employee.json
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
[root@localhost ~]# !772
!!
。重复历史列表中的后一个命令,其行为与 !-1
相同:[root@localhost ~]# cat employee.json
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
[root@localhost ~]# !!
cat employee.json
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
[root@localhost ~]# !-1
cat employee.json
[{"name": "John Brooks","id": "003"},{"name": "Randy Park","id": "053"},{"name": "Todd Gray","id": "009"}]
[root@localhost ~]#
如果上一条命令忘记添加
sudo
来执行,可以结合!!
来使用。还可以后面结合管道符使用。$ yum update
Loaded plugins: priorities, update-motd, upgrade-helper
You need to be root to perform this command.
$ sudo !!
sudo yum update
Loaded plugins: priorities, update-motd, upgrade-helper
$ ls
dir dir1 dir2 file file1 file2 hello.txt
$ !! | grep file
ls | grep file
file
file1
file2
!!:s^oldstring^newstring
^
是分隔符,用后面的newstring字符串替换前面的oldstring字符串。[root@localhost ~]# ll /etc/httpd/conf.d
total 16
-rw-r--r-- 1 root root 2926 Nov 4 2020 autoindex.conf
-rw-r--r-- 1 root root 400 Nov 4 2020 README
-rw-r--r-- 1 root root 1252 Nov 4 2020 userdir.conf
-rw-r--r-- 1 root root 764 Nov 4 2020 welcome.conf
[root@localhost ~]# !!:s^conf.d^conf
ll /etc/httpd/conf
total 28
-rw-r--r-- 1 root root 11899 Nov 4 2020 httpd.conf
-rw-r--r-- 1 root root 13064 Nov 4 2020 magic
[root@localhost ~]#
总 结
END
相关文章