这篇文章主要用来介绍通配符,由于自己经常和正则表达式搞混,所以特地写了这篇文章,来梳理一些。
通配符和正则的区别
通配符主要匹配文件名,而正则主要匹配文件中的内容;
常用的通配符介绍
主要有* 、?、[]这三种;
-
- 匹配零个或多个字符
如a*b,与之匹配的可以是aab,ab,a123b,abc.....
- 匹配零个或多个字符
- ? 匹配任意单个字符
如a?b,与之匹配的可以是aab,abb,a1b,a2b...... - [] 匹配指定范围内的任意单个字符
如[wang] 即匹配w、a、n、g等任意一个字符; - [^] 匹配指定范围以外的任意单个字符
如[^wang] 排除列表中的所有字符,i与[]相反,即取反; - 字符集的表示
注意:对于下面的字符集,如果想要表示其中任意一个字符,需要在外面再加一个[]才可以;
[0-9]: 任意数字
[a-z]: 注意,这里不仅仅是小写字母,而是a,A ,b,B,c,C...z这样的顺序,即:小写、大写
[A-Z]: 注意,这里不仅仅表示大写字母,而是A,b,B,c,C...z,Z的 顺序
[:digit:]:任意数字,相当于0-9
[:lower:]:任意小写字母
[:upper:]: 任意大写字母
[:alpha:]: 任意大小写字母
[:alnum:]:任意数字或字母
[:blank:]:水平空白字符
[:space:]:水平或垂直空白字符,包含空格
[:punct:]:标点符号
更多内容,可以man 7 glob查看
练习:
1. 显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录
(1)环境准备:
[[email protected] var ]#touch l2223343434a
[[email protected] var ]#touch l22233sddfd34a
[[email protected] var ]#touch l22dfdf34a
(2)方法一:
[[email protected] var ]#ls /var/l*[[:digit:]]*[[:lower:]]
/var/l2223343434a /var/l22233sddfd34a /var/l22dfdf34a
或
[[email protected] var ]#ls -d /var/l*[0-9]*[[:lower:]]
/var/l2223343434a /var/l22233sddfd34a /var/l22dfdf34a
2. 显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录
(1)准备环境:
[[email protected] var ]#touch /etc/133rdfdf
[[email protected] var ]#touch /etc/123xin
[[email protected] var ]#touch /etc/123yuanhonli
[[email protected] var ]#touch /etc/abc_xn
[[email protected] var ]#touch /etc/1axc_xn
(2)解答:
[[email protected] var ]#ls /etc/[0-9]*[^0-9]
/etc/123xin /etc/123yuanhonli /etc/133rdfdf /etc/1axc_xn
或
[[email protected] var ]#ls /etc/[0-9]*[^[:digit:]]
/etc/123xin /etc/123yuanhonli /etc/133rdfdf /etc/1axc_xn
3. 显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录
(1准备环境:
[[email protected] var ]#touch /etc/1axc_xn
[[email protected] var ]#touch /etc/_222b
[[email protected] var ]#touch /etc/*b22b
[[email protected] var ]#touch /etc/xinb22b
(2)解答:
[[email protected] var ]#ls /etc/[^[:alpha:]][[:alpha:]]*
/etc/1axc_xn /etc/*b22b
4. 显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录
解答:
[[email protected] var ]#ls -d /etc/rc[0-6]* ##注意添加-d选项,否则显示所有目录下的文件
/etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d
5. 显示/etc目录下,所有以.d结尾的文件或目录
解答:
[[email protected] var ]#ls -d /etc/*.d
/etc/bash_completion.d /etc/logrotate.d /etc/rc0.d /etc/rsyslog.d
/etc/chkconfig.d /etc/lsb-release.d /etc/rc1.d /etc/rwtab.d
/etc/cron.d /etc/makedev.d /etc/rc2.d /etc/sane.d
/etc/depmod.d /etc/modprobe.d /etc/rc3.d /etc/setuptool.d
/etc/dnsmasq.d /etc/oddjobd.conf.d /etc/rc4.d /etc/statetab.d
/etc/dracut.conf.d /etc/pam.d /etc/rc5.d /etc/sudoers.d
/etc/init.d /etc/popt.d /etc/rc6.d /etc/sysctl.d
/etc/latrace.d /etc/prelink.conf.d /etc/rc.d /etc/xinetd.d
/etc/ld.so.conf.d /etc/profile.d /etc/request-key.d /etc/yum.repos.d
[[email protected] var ]#
6. 显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录
解答:
[[email protected] var ]#ls -d /etc/[mnrp]*.conf
/etc/mke2fs.conf /etc/ntp.conf /etc/prelink.conf /etc/rsyslog.conf
/etc/mtools.conf /etc/pbm2ppa.conf /etc/readahead.conf
/etc/nfsmount.conf /etc/pm-utils-hd-apm-restore.conf /etc/request-key.conf
/etc/nsswitch.conf /etc/pnm2ppa.conf /etc/resolv.conf
7. 只显示/root下的隐藏文件和目录
[[email protected] var ]#ls -d /root/.*
/root/. /root/.cache /root/.gnote /root/.local /root/.thumbnails
/root/.. /root/.config /root/.gnupg /root/.nautilus /root/.viminfo
/root/.abrt /root/.cshrc /root/.gtk-bookmarks /root/.pulse /root/.Xauthority
/root/.bash_history /root/.dbus /root/.gvfs /root/.pulse-cookie
/root/.bash_logout /root/.esd_auth /root/.ICEauthority /root/.ssh
/root/.bash_profile /root/.gconf /root/.icons /root/.tcshrc
/root/.bashrc /root/.gnome2 /root/.imsettings.log /root/.themes
[[email protected] var ]#
8. 只显示/etc下的非隐藏目录
解答:
[[email protected] var ]#ls -d /etc/[^.]*
/etc/123xin /etc/host.conf /etc/prelink.cache
/etc/123yuanhonli /etc/hosts /etc/prelink.conf
/etc/133rdfdf /etc/hosts.allow /etc/prelink.conf.d
/etc/1axc_xn /etc/hosts.deny /etc/printcap
/etc/_222b /etc/hp /etc/profile
/etc/abc_xn /etc/httpd /etc/profile.d
/etc/abrt /etc/idmapd.conf /etc/protocols
原文地址:http://blog.51cto.com/13668904/2160984
时间: 2024-11-07 19:12:15