8.4 通配符

时间:2022-04-27
本文章向大家介绍8.4 通配符,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
  • ls *.txt
    • *表示通配,不分字符,也不分几个
[root@hf-01 ~]# ls
111  123  1_heard.txt  1_sorft.txt  234  2.txt.bak  3.txt  anaconda-ks.cfg
[root@hf-01 ~]# ls *.txt        //以.txt结尾的文件都会列出来
1_heard.txt  1_sorft.txt  3.txt
[root@hf-01 ~]# ls *txt        //以txt结尾的文件都会列出来
1_heard.txt  1_sorft.txt  3.txt
[root@hf-01 ~]# ls *txt*        //包含txt的都会列出来
1_heard.txt  1_sorft.txt  2.txt.bak  3.txt
[root@hf-01 ~]# ls 1*        //只要1开头的都会列出来
1_heard.txt  1_sorft.txt

111:

123:
[root@hf-01 ~]# 
  • ls ?.txt
    • ?与* 相对比,? 表示一个任意的字符
    • 会看到(例子)只列出一个字符的.txt文件
[root@hf-01 ~]# touch 1.txt 2.txt
[root@hf-01 ~]# ls ?.txt
1.txt  2.txt  3.txt
[root@hf-01 ~]# touch a.txt bb.txt
[root@hf-01 ~]# ls ?.txt
1.txt  2.txt  3.txt  a.txt
[root@hf-01 ~]# 
  • ls [0-9].txt
    • []这里面可以写一个范围
[root@hf-01 ~]# ls
111  1_heard.txt  1.txt  2.txt      3.txt            a.txt
123  1_sorft.txt  234    2.txt.bak  anaconda-ks.cfg  bb.txt
[root@hf-01 ~]# ls [0-3].txt
1.txt  2.txt  3.txt

    可以把0,1,2,3这四个数字,任意一个都会满足这个条件,[]方括号中的字符只会取一个,就是“或者”的意思

[root@hf-01 ~]# ls [23].txt
2.txt  3.txt
[root@hf-01 ~]# ls [13].txt
1.txt  3.txt

在方括号中可以写范围[0-9a-zA-Z]
  • ls {1,2}.txt
    • 也是或者的意思,这个范围当中的一个
[root@hf-01 ~]# ls {1,2,3}.txt
1.txt  2.txt  3.txt
[root@hf-01 ~]# 

{1,2,3}.txt和[1-3].txt表达意思一样,或者。只是在{}需要用  ,  逗号隔开