通配符glob

所謂的通配符glob,即便用特定的字符(稱之爲元字符),可實現對相應的文件名進行匹配,實現快速引用多個文件的功能。詳細介紹請查看幫助:man 7 glob
通配符相關的元字符以下:python

  • :匹配任意長度的任意字符;例如:p可匹配p開頭的全部文件
  • ?:匹配任意單個字符;例如:p?可匹配p後面出現單個字符的文件,如:pi
  • [ ]:中括號匹配指定集合內的任意單個字符
  • 例如:
    • [kyle]:表示k,y,l,e四個字符中任意一個
    • [0-9]:匹配單個數字
    • [[:upper:]]:匹配任意單個全部大寫字母
    • [[:lower:]]:匹配任意單個全部小寫字母
    • [[:digit:]]:匹配任意單個全部數字,等價於[0-9]
    • [[:alpha:]]:匹配任意單個字母
    • [[:alnum:]]:匹配任意單個字母和數字
    • [[:space:]]:匹配單個空白字符
    • [[:punctl:]]:匹配單個標點符號
    • [^]:匹配指定集合外的任意單個字符

[a-c]可不是純小寫,順序是a.A.b.B.cgit

[root@Centos7 data]# touch {a..z}.txt
[root@Centos7 data]# touch {A..Z}.txt
[root@Centos7 data]# ls
a.txt  c.txt  e.txt  g.txt  i.txt  k.txt  m.txt  o.txt  q.txt  s.txt  u.txt  w.txt  y.txt
A.txt  C.txt  E.txt  G.txt  I.txt  K.txt  M.txt  O.txt  Q.txt  S.txt  U.txt  W.txt  Y.txt
b.txt  d.txt  f.txt  h.txt  j.txt  l.txt  n.txt  p.txt  r.txt  t.txt  v.txt  x.txt  z.txt
B.txt  D.txt  F.txt  H.txt  J.txt  L.txt  N.txt  P.txt  R.txt  T.txt  V.txt  X.txt  Z.txt

在建立一個0-9的文件,加以區分ide

[root@Centos7 data]# ls
0.txt  5.txt  a.txt  C.txt  f.txt  H.txt  k.txt  M.txt  p.txt  R.txt  u.txt  W.txt  z.txt
1.txt  6.txt  A.txt  d.txt  F.txt  i.txt  K.txt  n.txt  P.txt  s.txt  U.txt  x.txt  Z.txt
2.txt  7.txt  b.txt  D.txt  g.txt  I.txt  l.txt  N.txt  q.txt  S.txt  v.txt  X.txt
3.txt  8.txt  B.txt  e.txt  G.txt  j.txt  L.txt  o.txt  Q.txt  t.txt  V.txt  y.txt
4.txt  9.txt  c.txt  E.txt  h.txt  J.txt  m.txt  O.txt  r.txt  T.txt  w.txt  Y.txt

實驗環境已經搭建好,咱們開始驗證
數字驗證post

[root@Centos7 data]# ls [0-8].txt
0.txt  1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt

數字驗證結果沒問題,接下來是字母驗證spa

[root@Centos7 data]# ls [a-c].txt
a.txt  A.txt  b.txt  B.txt  c.txt
[root@Centos7 data]# ls [a-C].txt
a.txt  A.txt  b.txt  B.txt  c.txt  C.txt
[root@Centos7 data]# ls [A-c].txt
A.txt  b.txt  B.txt  c.txt
[root@Centos7 data]# ls [A-C].txt
A.txt  b.txt  B.txt  c.txt  C.txt

[a-c]:顯示的是a,A,b,B,c
[a-C]:顯示的是a,A,b,B,c,C
[A-c]:顯示的是A,b,B,c
[A-C]:顯示的是A,b,B,c,C
所以得出,顯示的結果爲a,A,b,B,c,C,d,D … 按此順序下去code

l.的別名及用法
先查看l.的別名cdn

[root@Centos7 data]# alias
alias cdnet='cd /etc/sysconfig/network-scripts/'
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde’

[root@Centos7 ~]# type l.
l. 是 `ls -d .* --color=auto' 的別名

l. = ls -d .*
表示顯示當前目錄下的隱藏文件或目錄ip

[root@Centos7 data]# mkdir .test
[root@Centos7 data]# touch .txt
[root@Centos7 data]# l.
.  ..  .a  .b  .c  .test  .txt

隱藏文件沒法經過rm -rf ./*刪除rem

[root@Centos7 data]# rm -rf ./*
[root@Centos7 data]# ls -a
.  ..  .a  .b  .c  .test  .txt       #沒法刪除
[root@Centos7 data]# rm -rf ./.*     #使用這個(./.*)能刪除
rm: refusing to remove "." or ".." directory: skipping "./."
rm: refusing to remove "." or ".." directory: skipping "./.."
[root@Centos7 data]# ls -a
[root@Centos7 data]#

[root@Centos7 data]# mkdir .test
[root@Centos7 data]# touch .txt
[root@Centos7 data]# l.
.  ..  .test  .txt
[root@Centos7 data]# rm -r /data/.[^.]*            #使用通配符的方式刪除
rm: remove directory ‘/data/.test’? y
rm: remove regular empty file ‘/data/.txt’? y
[root@Centos7 data]# l.
.  ..

ls -d */
只顯示當前目錄下的子目錄it

[root@Centos7 data]# mkdir test{1..5}
[root@Centos7 data]# touch file{1..5}.txt
[root@Centos7 data]# ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt  test1  test2  test3  test4  test5

環境已經弄好,開始印證

[root@Centos7 data]# ls -d       #不帶參數,表示當前目錄,因此顯示爲點(.)
.
[root@Centos7 data]# ls -d *     #*表示任意,因此會羅列出當前的全部文件或目錄;注意這裏即使子目錄裏有文件,也不會羅列出來,由於ls帶了一個-d的選項
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt  test1  test2  test3  test4  test5
[root@Centos7 data]# ls -d */    #/能夠當作是目錄標識,那麼此結果顯示就只有目錄
test1/  test2/  test3/  test4/  test5/

實戰訓練
一、顯示/etc目錄下,以非字母開頭,後面跟了一個字母以及其餘任意長度任意字符的文件或目錄

[root@Centos7 ~]# ls -d /etc/[^[:alpha:]][[:alpha:]]*
[root@Centos7 ~]# ls -d /etc/[^a-Z][a-Z]*

二、複製/etc目錄下全部以p開頭,以非數字結尾的文件或目錄到/tmp/mytest1目錄中

[root@Centos7 ~]# mkdir /tmp/mytest1
[root@Centos7 ~]# cp -r /etc/p*[^0-9] /tmp/mytest1/
[root@Centos7 ~]# ls /tmp/mytest1/
pam.d      passwd-       pki       pnm2ppa.conf  ppp             profile    pulse
papersize  pbm2ppa.conf  plymouth  popt.d        prelink.conf.d  profile.d  purple
passwd     pinforc       pm        postfix       printcap        protocols  python

[root@Centos7 mytest1]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1/
[root@Centos7 mytest1]# ls /tmp/mytest1/
pam.d      passwd-       pki       pnm2ppa.conf  ppp             profile    pulse
papersize  pbm2ppa.conf  plymouth  popt.d        prelink.conf.d  profile.d  purple
passwd     pinforc       pm        postfix       printcap        protocols  python

三、將/etc/issue文件中的內容轉換爲大寫後保存至/tmp/issue.out文件中

[root@Centos7 ~]# cat /etc/issue |tr a-z A-Z >/tmp/issue.out
[root@Centos7 ~]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M

[root@Centos7 ~]# cat /etc/issue |tr [[:lower:]] [[:upper:]] >/tmp/issue.out
[root@Centos7 ~]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M

[root@Centos7 mytest1]# tr [[:lower:]] [[:upper:]] < /etc/issue > /tmp/issue.out
[root@Centos7 mytest1]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M
相關文章
相關標籤/搜索