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