跟我学之Linux Shell 第4天 文件名匹配

上一篇 / 下一篇  2008-08-14 15:34:14 / 个人分类:Shell

查看( 162 ) / 评论( 3 )
第4天 文件名匹配
我们在使用命令时,很多情况下其操作对象是文件名符合某种模式规则的文件。
如:
   匹配文件名中的任何字符串
   匹配文件名中的单个字符
   匹配文件命中的字母或数字

下面我们就来学习这些字符串模式匹配规则。
1、使用“ * ”
使用星号“ * ”可以匹配文件名中的任何字符串。
例:
    $ls mk*
        /bin/mkdir  /bin/mknod  /bin/mktemp

给出的文件名匹配模式是mk*,把*放在文件末尾,意思是文件名以mk开头,后面可以跟随任何字符串,包括空字符串。

    $ls *.png
    gnome-about-logo.png             im-chooser.png
    gnome-aorta.png                  invest-big.png
给出的文件名匹配模式*.png,把*放在开头,意思是匹配所有以 .png 结尾的文件。

    $ls me*.jpg
    medium.jpg
给出的文件名匹配模式me*.jpg,把*放在中间,意思是匹配所有以me开头,.jpg结尾,中间可以是任何字符串的文件。

在使用 cd 命令切换路径时,也使用星号,这样可以省去输入整个路径名的麻烦,大家可以尝试一下哦。

2、使用“ ? ”
使用问号“ ? ”可以匹配文件名中的单个字符。
例:
    $ls /bin/?a*
    /bin/basename  /bin/cat   /bin/false  /bin/mail  /bin/tar
    /bin/bash      /bin/date  /bin/gawk   /bin/raw   /bin/taskset


    $ls /bin/ch???
    /bin/chgrp  /bin/chmod  /bin/chown


    $ls /bin/d??e
    /bin/date


3、使用[...]和[!...]
使用 [...] 可以用来匹配方括号 [ ] 中的任何字符。在这一方法中,还可以使用一个横杠 -来连

接两个字母或数字,以此来表示一个范围。
例:
    $ls /bin/[ck]*
    /bin/cat    /bin/chmod  /bin/cp    /bin/csh  /bin/kbd_mode  /bin/ksh
    /bin/chgrp  /bin/chown  /bin/cpio  /bin/cut  /bin/kill
列出以 c或k开头的文件名

    $ls /etc/[a-n]*.conf
    /etc/autofs_ldap_auth.conf  /etc/idmapd.conf    /etc/logrotate.conf
    /etc/cdrecord.conf          /etc/initlog.conf   /etc/mke2fs.conf
    /etc/conman.conf            /etc/jwhois.conf    /etc/modprobe.conf
    /etc/dhcp6c.conf            /etc/kdump.conf     /etc/mtools.conf
    /etc/dhcpd.conf             /etc/krb5.conf      /etc/multipath.conf
    /etc/esd.conf               /etc/ldap.conf      /etc/nscd.conf
    /etc/gpm-root.conf          /etc/ld.so.conf     /etc/nsswitch.conf
    /etc/grub.conf              /etc/lftp.conf      /etc/ntp.conf
    /etc/gssapi_mech.conf       /etc/libaudit.conf
    /etc/host.conf              /etc/libuser.conf
列出/etc/目录中以字母a到n开头的,并且以.conf结尾的文件

    $ls /etc/[!a-n]*.conf
    /etc/oddjobd.conf       /etc/sestatus.conf   /etc/wvdial.conf
    /etc/pam_smb.conf       /etc/smartd.conf     /etc/xinetd.conf
    /etc/prelink.conf       /etc/smi.conf        /etc/yum.conf
    /etc/prozilla.conf      /etc/sysctl.conf     /etc/yumex.conf
    /etc/reader.conf        /etc/syslog.conf     /etc/yumex.profiles.conf
    /etc/resolv.conf        /etc/updatedb.conf
    /etc/scrollkeeper.conf  /etc/warnquota.conf

列出/etc/目录中不是以字母a到n开头的,并且以.conf结尾的文件,其中 ! 是非的意思


为了列出所有以大写字母开头的文件名,怎么用?
为了列出所有以数字开头的文件名,怎么用?

TAG: Shell

Imcomplete zhull1052 发布于2008-08-28 08:39:08
顶一下,呵呵
firebird799发布于2008-08-29 11:26:01
ee3









WWE   钢筋连接   婷美   富铤 Fortei  杰豪 gihooo
我来说两句

(可选)

日历

« 2008-11-23  
      1
2345678
9101112131415
16171819202122
23242526272829
30      

数据统计

  • 访问量: 1121
  • 日志数: 11
  • 图片数: 2
  • 建立时间: 2006-01-10
  • 更新时间: 2008-08-14

RSS订阅

Open Toolbar