Linux 命令行小技巧 – !叹号的用处
2023-09-27 14:29:14 时间
bash 的历史记录里保留了输入的命令行记录。以下是如何充分利用该记录和! 符号的使用教程。 |
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"}]
重复历史记录中的第 n 个命令
重复 bash 历史记录中的第 n 个命令:
[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 ~]#
http://www.cctime.com/html/2022-7-15/1624162.htm
https://www.51cto.com/it/news/2022/0722/33307.html
http://www.techweb.com.cn/article/2022-07-21/2898956.shtml
如果上一条命令忘记添加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 ~]#
总结
这些是每个系统管理员都应该熟悉的命令。这些命令可以节省大量时间。
相关文章
- linux中,当执行rpm -e删除一个软件包时,都做了些什么事
- linux检测远程端口是否打开
- stm32mp1 linux最小根文件系统制作教程(stm32mp157开发板实操)
- Linux集群和自动化维3.6.2 命令行入口fab命令详解
- 如何在Linux上使用命令行管理密码
- Linux基础:如何在命令行中查看目录的大小
- Linux有问必答:如何在Linux命令行中刻录ISO或NRG镜像到DVD
- Linux 命令行工具使用小贴士及技巧(二)
- Linux 有问必答:如何在桌面版 Ubuntu 中用命令行更改系统代理设置
- Linux下查看某个进程的线程数量(转)
- 【Linux操作系统】环境变量和命令行参数
- 在VMware Workstation上安装Kali Linux
- Linux 安装基于(PHP5.5)memcache扩展
- [LINUX]ContOS7最小化安装之后出现的cmdNOTFound所需要安装的软件
- Linux磁盘分区(一):添加
- linux系统编程:自己动手写一个cp命令
- linux input子系统
- Linux | 调试器GDB的详细教程【纯命令行调试】
- 【嵌入式Linux基础入门】番外篇:Linux新系统快速配置指南
- 【嵌入式Linux驱动进阶】U-boot 学习
- redis在linux下安装并測试(在spring下调用)
- 【历史上的今天】2 月 22 日:Red Hat Enterprise Linux 问世;BASIC 语言作者出生;计算机协会创始人诞生
- FreeSwitch SIP(1):linux下编译安装v1.4
- 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---11
- 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---41
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---40
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---37
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---32
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---30
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---05
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---56
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---50
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---20
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---11