文件名通配globbing內容及演示

globbing
  • 概述
    globbing是bash常見特性之一;Bash通過globbing來執行文件名的擴展;globbing不會使用標準的RE集(標準正則表達式),通過識別和擴展通配符來完成匹配文件名(整個文件名匹配,非部分),即元字符;globbing在解釋元字符有重要的限制,且有些元字符意義和RE集里的不相同。
  • globbing中使用的元字符及意義
  1. *:匹配任意長度的任意字符
  2. ?:匹配任意單個字符
  3. []:匹配指定范圍內的任意單個字符
    - 幾種常見的特殊格式
    [a-z]或[A-Z]:所有字母,不區分大小寫
    [0-9]:所有的數字
    [a-z0-9] :所有字母和數字
    [[:upper:]]:所有大寫字母,外側中括號表示范圍的,內層中括號是固定格式
    [[:lower:]]:所有小寫字母
    [[:alpha:]]:所有字母
    [[:digit:]]:所有數字
    [[:alnum:]]:所有字母和數字
    [[:space:]]:所有空白字符
    [[:punct:]]:所有標點符號
  4. [^]:匹配指定范圍外的任意單個字符
  • 演示
    顯示/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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,578評論 6 544
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,701評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,691評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,974評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,694評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,026評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,015評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,193評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,719評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,442評論 3 360
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,668評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,151評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,846評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,255評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,592評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,394評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,635評論 2 380

推薦閱讀更多精彩內容