文件通配符模式
文件通配符可用来匹配符合条件的多个文件,方便批量管理文件。
通配符采用特定的符号,表示特定的含义,该类符号成为元字符(meta)
常见的通配符如下
* 匹配零个或多个字符,但不匹配以"."开头的文件,即隐藏文件
? 匹配任何单个字符
~ 当前用户家目录
~dawn 用户dawn家目录
~+ 和. 当前工作目录
~- 上一个工作目录
[0-9] 匹配数字范围
[a-z] 匹配小写字母范围
[A-Z] 匹配大写字母范围
[wang] 匹配列表中的任何一个字符
[^wang] 匹配列表中的除列表以外的字符
按照aAbBcC...zZ的顺序排列的,这个组合,最后只剩一个Z
[^a-z] 匹配列表中的所有字符以外的字符,ps:这里有个巨坑,一定要看案例
范例
[root@centos8 etc]#echo $OLDPWD
/data
[root@centos8 etc]#ll -/linux.txt
ls: invalid option -- '/'
Try 'ls --help' for more information.
[root@centos8 etc]#ll ~-/linux.txt
-rw-r--r--. 1 root root 0 Jul 30 15:15 /data/linux.txt
#找出所有的文件目录,不包含隐藏目录文件
[root@centos8 data]#ls -d /etc/*/
#找出所有的隐藏文件
[root@centos8 data]#l.
[root@centos8 data]#touch file*.log
[root@centos8 data]#touch file1.log
[root@centos8 data]#ls file*.log
[root@centos8 data]#ls 'file*.log'
'file*.log'
[root@centos8 data]#ls `file*.log`
[root@centos8 data]#touch file{a..z}.txt file{A..Z}.txt file{0..3}.txt
[root@centos8 data]#ll file{0..3}.txt
-rw-r--r--. 1 root root 0 Jul 30 15:18 file0.txt
-rw-r--r--. 1 root root 0 Jul 30 15:18 file1.txt
-rw-r--r--. 1 root root 0 Jul 30 15:18 file2.txt
-rw-r--r--. 1 root root 0 Jul 30 15:18 file3.txt
[root@centos8 data]#ll file[0-3].txt
-rw-r--r--. 1 root root 0 Jul 30 15:18 file0.txt
-rw-r--r--. 1 root root 0 Jul 30 15:18 file1.txt
-rw-r--r--. 1 root root 0 Jul 30 15:18 file2.txt
-rw-r--r--. 1 root root 0 Jul 30 15:18 file3.txt
[root@centos8 data]#ls file[a-c].txt
filea.txt fileA.txt fileb.txt fileB.txt filec.txt
[root@centos8 data]#ls file[C-E].txt
fileC.txt filed.txt fileD.txt filee.txt fileE.txt
[root@centos8 data]#ls file[a-c].txt
filea.txt fileA.txt fileb.txt fileB.txt filec.txt
[root@centos8 data]#ls file[C-E].txt
fileC.txt filed.txt fileD.txt filee.txt fileE.txt
巨坑在等我,完全没想到
#以为会找到3个,其实有5个,惊不惊喜?意不意外?
[root@centos8 data]#ls file[x-z].txt
filex.txt fileX.txt filey.txt fileY.txt filez.txt
#以为都不会找到,但是剩下了fileZ.txt
[root@centos8 data]#ls file[^a-z].txt
fileZ.txt
总结:[a-z]或[^a-z]的字母查找顺序是aAbBcC...xXyYzX
Linux系统中预定的字符类:man 7 glob
以下[]都不可省略
[:digit:] 任意数字,等同0-9
[:lower:] 任意小写字母,等同a-z
[:upper:] 任意大写字母,等同A-Z
[:alpha:] 任意大小写字母
[:alnum:] 任意数字或字母
[:blank:] 水平空白字符
[:space:] 水平或垂直空白字符
[:punct:] 标点符号
[:print:] 可打印字符
[:cntrl:] 控制(非打印)字符
[:graph:] 图形字符
[:xdigit:] 十六进制字符