通配符和特殊符号学习
学习通配符或是正则:记住
| 正则,通配符是为了解决某问题 |
| 先是有问题场景,再琢磨如何写通配符 |
| 不要先去写奇怪的通配符或是正则,再去琢磨它是什么含义 |
什么是通配符
| 当你再查找特定文件名,却记不住如何拼写时,通配符是帮你寻找的神器 |
| |
| 通配符是专门用于处理文件名的特殊字符,而不是文件内容 |
| |
| 可以方便查找类似,但是不相同的文件名 |
| |
| 通配符是shell的内置语法,大部分linux命令都认识通配符 |
| |
| 注意:文件一般是不带特殊字符的 |
| 基本字符 通配符 模糊搜索的字符内容 |
| wenjie * wenjie 1 |
| wenjie jie |
| wenjie wenjie |
| |
| li ? liwenjie |
| liwenjie、 |
| liwenjie.. |
| liwenjie123 |
| |
通配符语法
| * 匹配任意字符,可以再字符串中使用星号(*) |
| “wh*”将找到 what,white,why,但是找不到awhile或者是watch |
| |
| |
| ? 在特定位置中匹配单个字母 |
| “b?ll” 可以找到ball,bell,bill |
| |
| |
| [] 匹配方括号中的字符 |
| “b[ae]ll” 将找到ball,bell,但找不到bill |
| |
| |
| ! 在方括号中排除字符 |
| “b[!ae]ll” 将找到bill,bull,但找不到ball,bell |
| “Like“[!a]*”” 将找到不到以字母a开头的所以项目 |
| |
| |
| - 匹配一个范围内的字符,记住以升序指定字符(A到Z,而不是Z到A) |
| “b[a-c]d” 将找到bad,bbd,bcd |
| |
| |
| # 匹配任意单个数字字符 |
| “1#3” 将找到 103,113,123 |
| |
| |
| ^ 同感叹号,在方括号中排除字符 |
| b[^ae]ll 找不到ball,找不到bell,能找到bcll,bbll |
| |
| * 匹配任意(0或多个)字符串,包括空字符串 |
| ? 匹配任意1个字符,且只有一个字符 |
| [abcd] 匹配方括号里的abcd任意一个字符,abcd可以是其他不连续的字符如[aqwd] |
| [a-z] 匹配方括号里a到z之间的任意一个字符,也可以是连续的数字[1-9] |
| [!abcd] 不匹配方括号里的任意字符,也可以写为连续的字符,如[!a-z],也就是不匹配a到z任意的一个字符,且!符可以用^替代,写成[^abcd] |
练习通配符
| [root@bogon ~]# mkdir /test ; cd /test |
| [root@bogon test]# touch yuchao.txt yuc.txt cc.txt yuchao01.log yuyu.log cc.sh |
| |
| |
* 匹配任意字符
| 找出所有txt结尾的文件 |
| [root@bogon test] |
| cc.txt yuchao.txt yuc.txt |
| |
| |
| 查看以sh结尾的文件 |
| [root@bogon test] |
| cc.sh |
| |
| 查看以yu开头、txt结尾的文件 |
| [root@bogon test] |
| yuchao.txt yuc.txt |
| |
| |
| 查看以yu开头、log结尾的文件 |
| [root@bogon test] |
| yuchao01.log yuyu.log |
| |
| |
| 查看所有以yu开头的文件 |
| [root@bogon test] |
| yuchao01.log yuchao.txt yuc.txt yuyu.log |
| |
| |
| 找出以c开头的文件 |
| [root@bogon test] |
| cc.sh cc.txt |
| |
| |
| |
? 匹配任意一个字符(用得少)
| [root@bogon test] |
| ls: cannot access ?.sh: No such file or directory |
| 这个报错了,为什么?因为没有一个文件叫做 ?.sh |
| |
| 去创建一个 |
| [root@bogon test] |
| [root@bogon test] |
| c.sh |
| 作用不大,只用于特定场景 |
| |
| 如何查找特定字符数量的文件? |
| 比如找出sh脚本文件 |
| [root@bogon test] |
| cc.sh c.sh |
| |
| |
| 你只能多写几个问号 |
| [root@bogon test] |
| c.sh |
| [root@bogon test] |
| cc.sh |
| |
| |
| 找出所有txt |
| [root@bogon test] |
| cc.txt |
| [root@bogon test] |
| yuchao.txt |
| [root@bogon test] |
| yuc.txt |
| |
| |
| |
[ ] 匹配方括号里的内容
| 创建测试数据 |
| |
| [root@bogon test] |
| [root@bogon test] |
| a.log cc.sh c.log d.log f.log yuchao01.log yuc.txt |
| b.log cc.txt c.sh e.log g.log yuchao.txt yuyu.log |
| |
| |
| 找出文件名是a-z之间任意一个字符的txt |
| [root@bogon test] |
| ls: cannot access [a-z].txt: No such file or directory |
| 报错了,因为没有 |
| |
| |
| 找出a-z之间任意一个字符的log |
| [root@bogon test] |
| a.log b.log c.log d.log e.log f.log g.log |
| |
| |
| 找出a-c之间任意一个字符的log |
| [root@bogon test] |
| a.log b.log c.log |
| |
| |
| |
| 创建测试数据 |
| |
| [root@bogon test] |
| [root@bogon test] |
| [root@bogon test] |
| a.log cc.txt d.log g.log yc3.log yu1.log yu4.log yuchao.txt |
| b.log c.log e.log yc1.log yc4.log yu2.log yu5.log yuc.txt |
| cc.sh c.sh f.log yc2.log yc5.log yu3.log yuchao01.log yuyu.log |
| |
| |
| |
| 找出以yu1 yu2 yu3 相关的log文件 |
| [root@bogon test] |
| yu1.log yu2.log yu3.log |
| |
| |
| 找出以y开头相关的log |
| [root@bogon test] |
| yc1.log yc3.log yc5.log yu2.log yu4.log yuchao01.log |
| yc2.log yc4.log yu1.log yu3.log yu5.log yuyu.log |
| |
| |
| |
| 只找出文件名是三个字母的log文件 |
| [root@bogon test] |
| yc1.log yc3.log yc5.log yu2.log yu4.log |
| yc2.log yc4.log yu1.log yu3.log yu5.log |
| |
| |
[!abcd] 取反方括号的内容
| 找出除了以abcd开头的log文件 |
| |
| [root@bogon test] |
| e.log g.log yc2.log yc4.log yu1.log yu3.log yu5.log yuyu.log |
| f.log yc1.log yc3.log yc5.log yu2.log yu4.log yuchao01.log |
| |
| 作用同上,必须是连续的数字 |
| [root@bogon test] |
| e.log g.log yc2.log yc4.log yu1.log yu3.log yu5.log yuyu.log |
| f.log yc1.log yc3.log yc5.log yu2.log yu4.log yuchao01.log |
| |
| |
| |
| 找出除了abcd开头的单个字母的log文件 |
| [root@bogon test] |
| e.log f.log g.log |
| |
| |
| 排除所有以yu开头的文件 |
| [root@bogon test] |
| a.log b.log cc.sh cc.txt c.log c.sh d.log e.log f.log g.log |
| |
| 排除所有以yu开头的sh文件 |
| [root@bogon test] |
| cc.sh c.sh |
| |
| |
通配符 find练习
| 搜索/etc下所有包含hosts相关字符的文件 |
| |
| [root@bogon ~] |
| /etc/hosts |
| /etc/hosts.allow |
| /etc/hosts.deny |
| |
| |
| 搜索/etc下的以ifcfg开头的文件(网卡配置文件) |
| |
| [root@bogon ~] |
| /etc/sysconfig/network-scripts/ifcfg-lo |
| /etc/sysconfig/network-scripts/ifcfg-ens33 |
| |
| |
| 只查找以数字结尾的网卡配置文件 |
| |
| [root@bogon ~] |
| /etc/sysconfig/network-scripts/ifcfg-ens33 |
| |
| |
| 找到系统中的第一块到第四块磁盘,注意磁盘的语法命名 |
| [root@bogon ~] |
| /dev/sdd |
| /dev/sdc |
| /dev/sda |
| /dev/sdb |
| |
| |
| |
| 注意*是和上面不同的,没分盘,所以只找到了他自己本身 |
| [root@bogon ~] |
| /dev/sdb |
| |
| |
| |
通配符练习二
| [root@bogon test_shell]# touch {a..h}.log |
| [root@bogon test_shell]# touch {1..10}.txt |
| [root@bogon test_shell]# ls |
| 10.txt 2.txt 4.txt 6.txt 8.txt a.log c.log e.log g.log |
| 1.txt 3.txt 5.txt 7.txt 9.txt b.log d.log f.log h.log |
| |
| |
| 找出a到e的log文件 |
| |
| [root@bogon test_shell]# ls [a-e].log |
| a.log b.log c.log d.log e.log |
| |
| |
| 找出除了3到5的txt文件 |
| |
| [root@bogon test_shell]# ls [!3-5].txt |
| 1.txt 2.txt 6.txt 7.txt 8.txt 9.txt |
| |
| |
| 找出除了2,5,8,9的txt文件 |
| [root@bogon test_shell]# ls [!2,5,8,9].txt |
| 1.txt 3.txt 4.txt 6.txt 7.txt |
| 同理写法 |
| [root@bogon test_shell]# ls [^2,5,8,9].txt |
| 1.txt 3.txt 4.txt 6.txt 7.txt |
| |
| |
| 找出除了a,e,f的log文件 |
| [root@bogon test_shell]# ls [!a,e,f].log |
| b.log c.log d.log g.log h.log |
| 同理写法 |
| [root@bogon test_shell]# ls [^a,e,f].log |
| b.log c.log d.log g.log h.log |
| |
| |
| |
特殊符号
| 什么是特殊符号 |
| |
| 比起通配符来说,linux的特殊符号更加复杂,杂乱无章,但是一个专业的linux运维 |
| 熟能生巧,这些都不是问题 |
| 路径相关 |
| |
| 符号 作用 |
| ~ 当前登录用户的家目录 |
| - 上一次工作路径 |
| . 当前工作路径,或表示隐藏文件 .yuchao.linux |
| .. 上一级目录 |
| |
| 引号相关 |
| '' 单引号,所见即所得 |
| "" 双引号,可以解析变量以及引用命令 |
| `` 反引号,可以解析命令 |
| 无引号,一般我们都省略双引号去写linux命令,但是会有歧义,比如空格,建议写引号 |
| |
| |
| 重定向符号 |
| > stdout覆盖重定向 |
| >> stdout追加重定向 |
| < stdin重定向 |
| |
| 2> stderr重定向 |
| 2>> stderr追加重定向 |
| |
| |
| 命令执行 |
| command1 && command2 |
| |
| command1 || command2 |
| |
| command1 ; command2 |
| |
| \ |
| |
| $() |
| |
| `` |
| |
| | #管道符 |
| |
| {} #生成序列 |
| |
引号练习
| 单引号 |
| 例如使用date练习 |
| |
| 单引号是无法识别date命令的 |
| [root@bogon test_shell] |
| Sat Apr 9 17:27:52 CST 2022 |
| [root@bogon test_shell] |
| date |
| [root@bogon test_shell] |
| $(date) |
| 单引号是所见即所得,单引号里面是什么,输入就是什么,没有任何改变,特殊符号也都失去了其他作用。 |
| |
| |
| 反引号 |
| 反引号中的linux命令是可以执行的,且反引号中只能写可执行的命令 |
| [root@bogon test_shell] |
| Sat Apr 9 17:29:18 CST 2022 |
| [root@bogon test_shell] |
| Sat Apr 9 17:29:28 CST 2022 |
| [root@bogon test_shell] |
| /test_shell |
| |
| [root@bogon test_shell] |
| 超哥还得是你啊超哥 |
| |
| 系统会首先执行反引号里的命令,然后再进行下一步的处理 |
| |
| |
| |
| |
| 双引号 |
| |
| [root@bogon test_shell] |
| date |
| [root@bogon test_shell] |
| Sat Apr 9 17:32:01 CST 2022 |
| |
| [root@bogon test_shell] |
| /usr/local/mysql/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin |
| [root@bogon test_shell] |
| [root@bogon test_shell] |
| 于超老师的外号是超哥 |
| |
| [root@bogon test_shell] |
| 于超老师的外号是超哥,当前的linux工作路径是/test_shell |
| |
| 当输出双引号内的内容时,如果内容里有linux命令,或者变量,特殊转义符等 |
| 会优先解析变量,命令,转义字符,然后得到最终的内容 |
| |
| |
| 无引号 |
| |
| 没有引号、很难确定字符串的边界,很容易出现各种故障 |
| |
| [root@bogon test_shell] |
| 于超老师的外号是 超哥 |
| [root@bogon test_shell] |
| 于超老师的外号是 超哥 当前的工作路径是 /test_shell |
| |
| [root@bogon test_shell] |
| 于超老师的外号是 超哥 当前的工作路径是 /test_shell |
| [root@bogon test_shell] |
| 于超老师的外号是 $name 当前的工作路径是 $PWD |
| |
| |
| |
特殊符号练习
| ; 分号 |
| 表示命令的结束 |
| 多个命令之间的分隔符 |
| 某些配置文件的注释符 |
| [root@bogon test_shell] |
| /test_shell |
| /test_shell/hehe |
| [root@bogon hehe] |
| |
| |
| |
| 注释符号 |
| 如命令的注释 |
| [root@bogon hehe] |
| [root@bogon hehe] |
| [root@bogon hehe] |
| |
| 如配置文件的注释符 |
| |
| |
| |
| |
| |
| |
| |
| |
| | 管道符 |
| Linux管道符 | 用于传输数据,对linux命令的处理结果再次处理,直到得到最终结果 |
| [root@bogon hehe] |
| inet 192.168.0.167 netmask 255.255.255.0 broadcast 192.168.0.255 |
| inet6 fe80::eb65:9865:3f31:2f68 prefixlen 64 scopeid 0x20<link> |
| inet 127.0.0.1 netmask 255.0.0.0 |
| inet6 ::1 prefixlen 128 scopeid 0x10<host> |
| |
| [root@bogon hehe] |
| tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1275/sshd |
| tcp6 0 0 :::22 :::* LISTEN 1275/sshd |
| |
| 能一次出结果的命令,尽量不要二次加工,效率并不高 |
| |
| |
| && 符 |
| 创建目录成功后,才执行后面的命令 |
| [root@bogon hehe] |
| [root@bogon chaoge_data] |
| |
| [root@bogon chaoge_data] |
| mkdir: cannot create directory ‘/chaoge_data’: File exists |
| 如果前面命令失败,后面命令不执行 |
| |
| [root@bogon chaoge_data] |
| mkdir: cannot create directory ‘/chaoge_data’: File exists |
| |
| |
| 只有前面成功,后面才执行 |
| [root@bogon chaoge_data] |
| /chaoge_data |
| |
| |
| || 符 |
| 只有前面命令失败、才执行后面命令 |
| |
| [root@bogon chaoge_data] |
| mkdir: cannot create directory ‘/chaoge_data’: File exists |
| /chaoge_data |
| |
| |
| 前面成功了,后面不执行 |
| [root@bogon chaoge_data] |
| [root@bogon chaoge_data] |
| |
| |
| |
| $() 符 |
| |
| 引用系统时间,创建文件名,脚本常见做法 |
| |
| [root@bogon chaoge_data] |
| [root@bogon chaoge_data] |
| [root@bogon chaoge_data] |
| total 0 |
| -rw-r--r--. 1 root root 0 Apr 9 18:20 nginx_access_2022-04-09.log |
| |
| |
| 找到当前的txt文件,然后删掉 |
| [root@bogon test_shell] |
| |
| |
| 等同写法 |
| [root@bogon test_shell] |
| |
| |
| |
| {} 符 |
| |
| 生成连续的数字序列 |
| [root@bogon test_shell] |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
| |
| |
| 生成字母序列 |
| [root@bogon test_shell] |
| a b c d e f g h i j k l m n o p q r s t u v w x y z |
| |
| |
| 特殊用法,拷贝文件的简写 |
| [root@bogon test_shell] |
| -rw-r--r--. 1 root root 310 Mar 2 15:41 /etc/sysconfig/network-scripts/ifcfg-ens33 |
| |
| |
| 作为变量的分隔符 |
| |
| [root@bogon test_shell] |
| [root@bogon test_shell] |
| total 0 |
| drwxr-xr-x. 2 root root 6 Apr 9 18:09 hehe |
| -rw-r--r--. 1 root root 0 Apr 9 18:27 超哥_2022-04-09.log |
| |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!