通配符glob

所谓的通配符glob,即使用特定的字符(称之为元字符),可实现对相应的文件名进行匹配,实现快速引用多个文件的功能。详细介绍请查看帮助:man 7 glob
通配符相关的元字符如下:

  • :匹配任意长度的任意字符;例如: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.c

[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的文件,加以区分

[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

实验环境已经搭建好,我们开始验证
数字验证

[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

数字验证结果没问题,接下来是字母验证

[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 … 按此顺序下去

l.的别名及用法
先查看l.的别名

[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 .*
表示显示当前目录下的隐藏文件或目录

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

隐藏文件无法通过rm -rf ./*删除

[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 */
只显示当前目录下的子目录

[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/

实战训练
1、显示/etc目录下,以非字母开头,后面跟了一个字母以及其他任意长度任意字符的文件或目录

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

2、复制/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

3、将/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

猜你喜欢

转载自blog.51cto.com/14812296/2507411