globbing
- 概述
globbing是bash常見特性之一;Bash通過globbing來執行文件名的擴展;globbing不會使用標準的RE集(標準正則表達式),通過識別和擴展通配符來完成匹配文件名(整個文件名匹配,非部分),即元字符;globbing在解釋元字符有重要的限制,且有些元字符意義和RE集里的不相同。 - globbing中使用的元字符及意義
- *:匹配任意長度的任意字符
- ?:匹配任意單個字符
- []:匹配指定范圍內的任意單個字符
- 幾種常見的特殊格式
[a-z]或[A-Z]:所有字母,不區分大小寫
[0-9]:所有的數字
[a-z0-9] :所有字母和數字
[[:upper:]]:所有大寫字母,外側中括號表示范圍的,內層中括號是固定格式
[[:lower:]]:所有小寫字母
[[:alpha:]]:所有字母
[[:digit:]]:所有數字
[[:alnum:]]:所有字母和數字
[[:space:]]:所有空白字符
[[:punct:]]:所有標點符號 - [^]:匹配指定范圍外的任意單個字符
- 演示
顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間出現一位任意字符的文件或目錄本身
[root@localhost ~]# ll /var \\查看目錄下符合條件的文件名稱,加粗部分(lib和log目錄)
總用量 12
drwxr-xr-x. 2 root root 19 7月 13 23:38 account
drwxr-xr-x. 2 root root 6 11月 5 2016 adm
drwxr-xr-x. 13 root root 159 7月 22 22:27 cache
drwxr-xr-x. 2 root root 6 11月 7 2016 crash
drwxr-xr-x. 3 root root 34 5月 30 06:34 db
drwxr-xr-x. 3 root root 18 5月 30 06:34 empty
drwxr-xr-x. 2 root root 6 11月 5 2016 games
drwxr-xr-x. 2 root root 6 11月 5 2016 gopher
drwxr-xr-x. 3 root root 18 5月 30 06:32 kerberos
drwxr-xr-x. 52 root root 4096 7月 22 22:37 lib
drwxr-xr-x. 2 root root 6 11月 5 2016 local
lrwxrwxrwx. 1 root root 11 5月 30 06:31 lock -> ../run/lock
drwxr-xr-x. 17 root root 4096 7月 23 00:01 log
lrwxrwxrwx. 1 root root 10 5月 30 06:31 mail -> spool/mail
drwxr-xr-x. 2 root root 6 11月 5 2016 nis
drwxr-xr-x. 2 root root 6 11月 5 2016 opt
drwxr-xr-x. 2 root root 6 11月 5 2016 preserve
lrwxrwxrwx. 1 root root 6 5月 30 06:31 run -> ../run
drwxr-xr-x. 12 root root 140 7月 13 23:37 spool
drwxrwxrwt. 7 root root 4096 7月 22 22:52 tmp
drwxr-xr-x. 2 root root 6 11月 5 2016 y
[root@localhost ~]# ls /var/l?[[:lower:]]
/var/lib:
AccountsService bluetooth .....
/var/log:
anaconda cron .....
[root@localhost ~]# ls -d /var/l?[[:lower:]] \\加-d選項,只顯示文件或目錄本身
/var/lib /var/log
[root@localhost ~]# touch /var/ldy \/var下沒有符合條件的文件,創建文件,進行匹配演示
[root@localhost ~]# touch /var/ldw \/var下沒有符合條件的文件,創建文件,進行匹配演示
[root@localhost ~]# ls /var
account adm cache crash db empty games gopher kerberos ldw ldy lib local lock log mail nis opt preserve run spool tmp yp
[root@localhost ~]# ls -d /var/l?[[:lower:]] \\加-d選項,只顯示文件或目錄(不顯示其下的內容)
/var/ldw /var/ldy /var/lib /var/log
[root@localhost ~]# ls -dl /var/l?[[:lower:]]
-rw-r--r--. 1 root root 0 7月 23 11:00 /var/ldw
-rw-r--r--. 1 root root 0 7月 23 11:00 /var/ldy
drwxr-xr-x. 52 root root 4096 7月 22 22:37 /var/lib
drwxr-xr-x. 17 root root 4096 7月 23 00:01 /var/log
[root@localhost ~]# ls -l /var/l?[[:lower:]]
-rw-r--r--. 1 root root 0 7月 23 11:00 /var/ldw
-rw-r--r--. 1 root root 0 7月 23 11:00 /var/ldy
/var/lib:
總用量 0
drwxr-xr-x. 4 root root 32 7月 13 23:30 AccountsService
drwxr-xr-x. 2 root root 26 7月 22 22:35 alsa
.......
/var/log:
總用量 14716
drwxr-xr-x. 2 root root 176 5月 30 06:41 anaconda
drwx------. 2 root root 23 5月 30 06:49 audit
......
[root@localhost ~]#
顯示/etc目錄下,以任意一位數字開頭,且非數字結尾的文件或目錄
[root@localhost mytest]# ls etc \/etc目錄下沒有符合的文件,故在/tmp/mytest/etc下隨便創建如下文件,進行演示
1xq1 2xq2 3xqs 44xqs 5xq. 6xl xqr
[root@localhost mytest]# ls etc/[[:digit:]][^[:digit:]]
etc/3xqs etc/44xqs etc/5xq. etc/6xl
[root@localhost mytest]# ls etc/[0-9][^0-9] \\etc下全是文件就不用-d選項了
etc/3xqs etc/44xqs etc/5xq. etc/6xl
顯示/etc目錄下,以非字母開頭,后邊跟一個字母及其它任意長度字符的文件或目錄
[root@localhost etc]# ls
12dy 1d12 1Dy12 dy1 m3plus
[root@localhost etc]# ls [^a-z][a-z]*
1d12 1Dy12
[root@localhost etc]# ls [^[:alpha:]][[:alpha:]]*
1d12 1Dy12
[root@localhost etc]#
復制/etc/目錄下,所有以m開頭,以非數字結尾的文件或目錄至/tmp/mg目錄
[root@localhost mytest]# ls etc \\加粗的兩個文件,不是目錄
1x9d 2xq2 44xqs 4X5ds 5xq. 6xl @d8sd mi1 mi2 mi2plus mi3plus+ xqr
[root@localhost mytest]# mkdir /tmp/mg
[root@localhost mytest]# cp -vr etc/m[^0-9] /tmp/mg \\多源復制需要確保mg是目錄且存在
"etc/mi2plus" -> "/tmp/mg/mi2plus"
"etc/mi3plus+" -> "/tmp/mg/mi3plus+"
[root@localhost mytest]# ls /tmp/mg
mi2plus mi3plus+
[root@localhost mytest]# mkdir etc/m3plus \\新建目錄,測試使用
[root@localhost mytest]# ls etc
1x9d 2xq2 44xqs 4X5ds 5xq. 6xl @d8sd m3plus mi1 mi2 mi2plus mi3plus+ xqr
[root@localhost mytest]# cp etc/{mi1,mi2} etc/m3plus/ \\多源復制需要確保m3plus是目錄且存在
[root@localhost mytest]# ls etc/m[^-9]
etc/mi1 etc/mi2 etc/mi2plus etc/mi3plus+
etc/m3plus:
mi1 mi2
[root@localhost mytest]# cp -v etc/m[^0-9] /tmp/mg \\沒有-r選項,雖然匹配了目錄,但m3plus復制失敗
cp: 略過目錄"etc/m3plus"
"etc/mi2plus" -> "/tmp/mg/mi2plus"
"etc/mi3plus+" -> "/tmp/mg/mi3plus+"
[root@localhost mytest]# ls /tmp/mg
mi2plus mi3plus+
[root@localhost mytest]# !rm \\history命令歷史用法,直接調用歷史命令并執行
rm /tmp/mg/m
rm:是否刪除普通空文件 "/tmp/mg/mi2plus"?yes
rm:是否刪除普通空文件 "/tmp/mg/mi3plus+"?yes
[root@localhost mytest]# cp -vr etc/m[^0-9] /tmp/mg \-r選項,復制目錄命令成功
"etc/m3plus" -> "/tmp/mg/m3plus"
"etc/m3plus/mi1" -> "/tmp/mg/m3plus/mi1"
"etc/m3plus/mi2" -> "/tmp/mg/m3plus/mi2"
"etc/mi2plus" -> "/tmp/mg/mi2plus"
"etc/mi3plus+" -> "/tmp/mg/mi3plus+"
[root@localhost mytest]# ls /tmp/mg
m3plus mi2plus mi3plus+
[root@localhost mytest]# tree /tmp/mg
/tmp/mg
├── m3plus
│ ├── mi1
│ └── mi2
├── mi2plus
└── mi3plus+
1 directory, 4 files
[root@localhost mytest]# ls /tmp/mg/m[^-9]
/tmp/mg/mi2plus /tmp/mg/mi3plus+
/tmp/mg/m3plus:
mi1 mi2
[root@localhost mytest]# ls -d /tmp/mg/m*[^-9]
/tmp/mg/m3plus /tmp/mg/mi2plus /tmp/mg/mi3plus+
[root@localhost mytest]#
復制/usr/share/man目錄下,所有以man開頭,后跟一個數字結尾的文件或目錄/tmp/man/目錄下
[root@localhost mytest]# ls -d /usr/share/man/man*[0-9] \\本次就只匹配出來,不進行復制操作了
/usr/share/man/man1 /usr/share/man/man4 /usr/share/man/man7
/usr/share/man/man2 /usr/share/man/man5 /usr/share/man/man8
/usr/share/man/man3 /usr/share/man/man6 /usr/share/man/man9
[root@localhost mytest]#
復制/etc目錄下,所有以.conf結尾,且以m,n,r,p開頭的文件或目錄至/tmp/conf.d/目錄下
[root@localhost mytest]# ls /etc/[mnrp]*.conf \\本次就只匹配出來,不進行復制操作了
/etc/man_db.conf /etc/nsswitch.conf /etc/radvd.conf /etc/rsyslog.conf
/etc/mke2fs.conf /etc/numad.conf /etc/request-key.conf
/etc/mtools.conf /etc/pbm2ppa.conf /etc/resolv.conf
/etc/nfsmount.conf /etc/pnm2ppa.conf /etc/rsyncd.conf