Linux通配符

1. 文件通配符模式 wildcard pattern

文件通配符可以用来匹配符合条件的多个文件,方便批量管理文件

通配符采有特定的符号,表示特定的含义,此特符号称为元 meta 字符

常见的通配符如下:

header 1header 2
*匹配零个或多个字符,但不匹配 “.” 开头的文件,即隐藏文件
?匹配任何单个字符
~当前用户家目录
~song用户song家目录
~+和.当前工作目录
~-前一个工作目录
[0-9]匹配数字范围
[a-z]一个字母
[A-Z]一个字母
[song]匹配列表中的任何的一个字符
[^song]匹配列表中的所有字符以外的字符
[^a-z]匹配列表中的所有字符以外的字符

别外还有在Linux系统中预定义的字符类:man 7 glob

header 1header 2
[:digit:]任意数字,相当于0-9
[:lower:]任意小写字母,表示 a-z
[:upper:]任意大写字母,表示 A-Z
[:alpha:]任意大小写字母
[:alnum:]任意数字或字母
[:blank:]水平空白字符
[:space:]水平或垂直空白字符
[:punct:]标点符号
[:print:]可打印字符
[:cntrl:]控制(非打印)字符
[:graph:]图形字符
[:xdigit:]十六进制字符

示例

[root@centos /tmp]#touch f{a..z}.txt
[root@centos /tmp]#touch f{A..Z}.txt
[root@centos /tmp]#ls f[a..z].txt
fa.txt  fz.txt
[root@centos /tmp]#ls f[a-z].txt      # 没有 fZ.txt     
fA.txt  fF.txt  fK.txt  fP.txt  fU.txt  fa.txt  ff.txt  fk.txt  fp.txt  fu.txt  fz.txt
fB.txt  fG.txt  fL.txt  fQ.txt  fV.txt  fb.txt  fg.txt  fl.txt  fq.txt  fv.txt
fC.txt  fH.txt  fM.txt  fR.txt  fW.txt  fc.txt  fh.txt  fm.txt  fr.txt  fw.txt
fD.txt  fI.txt  fN.txt  fS.txt  fX.txt  fd.txt  fi.txt  fn.txt  fs.txt  fx.txt
fE.txt  fJ.txt  fO.txt  fT.txt  fY.txt  fe.txt  fj.txt  fo.txt  ft.txt  fy.txt
[root@centos /tmp]#ls f[A-Z].txt       # 没有fa.txt
fA.txt  fF.txt  fK.txt  fP.txt  fU.txt  fZ.txt  ff.txt  fk.txt  fp.txt  fu.txt  fz.txt
fB.txt  fG.txt  fL.txt  fQ.txt  fV.txt  fb.txt  fg.txt  fl.txt  fq.txt  fv.txt
fC.txt  fH.txt  fM.txt  fR.txt  fW.txt  fc.txt  fh.txt  fm.txt  fr.txt  fw.txt
fD.txt  fI.txt  fN.txt  fS.txt  fX.txt  fd.txt  fi.txt  fn.txt  fs.txt  fx.txt
fE.txt  fJ.txt  fO.txt  fT.txt  fY.txt  fe.txt  fj.txt  fo.txt  ft.txt  fy.txt

示例

[root@centos /tmp]#ls f[[:lower:]].txt
fa.txt  fd.txt  fg.txt  fj.txt  fm.txt  fp.txt  fs.txt  fv.txt  fy.txt
fb.txt  fe.txt  fh.txt  fk.txt  fn.txt  fq.txt  ft.txt  fw.txt  fz.txt
fc.txt  ff.txt  fi.txt  fl.txt  fo.txt  fr.txt  fu.txt  fx.txt
[root@centos /tmp]#ls f[[:upper:]].txt
fA.txt  fD.txt  fG.txt  fJ.txt  fM.txt  fP.txt  fS.txt  fV.txt  fY.txt
fB.txt  fE.txt  fH.txt  fK.txt  fN.txt  fQ.txt  fT.txt  fW.txt  fZ.txt
fC.txt  fF.txt  fI.txt  fL.txt  fO.txt  fR.txt  fU.txt  fX.txt
[root@centos /tmp]#

示例: [ ] 和 { }

[root@centos /data]#touch file{a..z}.txt file{A..Z}.txt file{0..9}.txt
[root@centos /data]#ls file{0..9}.txt
file0.txt  file2.txt  file4.txt  file6.txt  file8.txt
file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
[root@centos /data]#ls file[0-9].txt
file0.txt  file2.txt  file4.txt  file6.txt  file8.txt
file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
[root@centos /data]#ls file[a-c].txt
fileA.txt  fileB.txt  filea.txt  fileb.txt  filec.txt
[root@centos /data]#ls file[C-E].txt
fileC.txt  fileD.txt  fileE.txt  filed.txt  filee.txt
[root@centos /data]#ls file[song].txt
fileg.txt  filen.txt  fileo.txt  files.txt
[root@centos /data]#ls file[^song].txt
file0.txt  file7.txt  fileE.txt  fileL.txt  fileS.txt  fileZ.txt  fileh.txt  fileq.txt  filey.txt
file1.txt  file8.txt  fileF.txt  fileM.txt  fileT.txt  filea.txt  filei.txt  filer.txt  filez.txt
file2.txt  file9.txt  fileG.txt  fileN.txt  fileU.txt  fileb.txt  filej.txt  filet.txt
file3.txt  fileA.txt  fileH.txt  fileO.txt  fileV.txt  filec.txt  filek.txt  fileu.txt
file4.txt  fileB.txt  fileI.txt  fileP.txt  fileW.txt  filed.txt  filel.txt  filev.txt
file5.txt  fileC.txt  fileJ.txt  fileQ.txt  fileX.txt  filee.txt  filem.txt  filew.txt
file6.txt  fileD.txt  fileK.txt  fileR.txt  fileY.txt  filef.txt  filep.txt  filex.txt
[root@centos /data]#

示例:比较有无*的功能区别

[root@centos /tmp]#ls -a *
14:21:20.log  2021-03-25.log  a.log  b.log  c.log  d.log  ks-script-6_og6455
[root@centos /tmp]#ls -a
.   .ICE-unix   .X11-unix  .font-unix    2021-03-25.log  b.log  d.log
..  .Test-unix  .XIM-unix  14:21:20.log  a.log           c.log  ks-script-6_og6455
[root@centos /tmp]#

示例

[root@centos /tmp]#ls -d /etc/*/
/etc/NetworkManager/     /etc/gcrypt/          /etc/pam.d/           /etc/rwtab.d/
/etc/X11/                /etc/gnupg/           /etc/pkcs11/          /etc/sasl2/
/etc/alternatives/       /etc/groff/           /etc/pki/             /etc/security/
/etc/audit/              /etc/grub.d/          /etc/plymouth/        /etc/selinux/
/etc/authselect/         /etc/gss/             /etc/pm/              /etc/skel/
/etc/bash_completion.d/  /etc/init.d/          /etc/polkit-1/        /etc/ssh/
/etc/binfmt.d/           /etc/iproute2/        /etc/popt.d/          /etc/ssl/
/etc/chkconfig.d/        /etc/kdump/           /etc/postfix/         /etc/sssd/
/etc/cifs-utils/         /etc/kernel/          /etc/prelink.conf.d/  /etc/sudoers.d/
/etc/cron.d/             /etc/krb5.conf.d/     /etc/profile.d/       /etc/sysconfig/
/etc/cron.daily/         /etc/ld.so.conf.d/    /etc/rc.d/            /etc/sysctl.d/
/etc/cron.hourly/        /etc/libnl/           /etc/rc0.d/           /etc/systemd/
/etc/cron.monthly/       /etc/libreport/       /etc/rc1.d/           /etc/terminfo/
/etc/cron.weekly/        /etc/libssh/          /etc/rc2.d/           /etc/tmpfiles.d/
/etc/crypto-policies/    /etc/logrotate.d/     /etc/rc3.d/           /etc/tuned/
/etc/dbus-1/             /etc/lsb-release.d/   /etc/rc4.d/           /etc/udev/
/etc/default/            /etc/microcode_ctl/   /etc/rc5.d/           /etc/unbound/
/etc/depmod.d/           /etc/modprobe.d/      /etc/rc6.d/           /etc/xdg/
/etc/dhcp/               /etc/modules-load.d/  /etc/redhat-lsb/      /etc/xinetd.d/
/etc/dnf/                /etc/nftables/        /etc/rhsm/            /etc/yum.repos.d/
/etc/dracut.conf.d/      /etc/openldap/        /etc/rpm/             /etc/yum/
/etc/firewalld/          /etc/opt/             /etc/rsyslog.d/
[root@centos /tmp]#l.
.  ..  .ICE-unix  .Test-unix  .X11-unix  .XIM-unix  .font-unix
[root@centos /tmp]#

示例

[root@centos /tmp]#touch file*.log
[root@centos /tmp]#ls
 14:21:20.log   2021-03-25.log   a.log   b.log   c.log   d.log  'file*.log'   ks-script-6_og6455
[root@centos /tmp]#touch file1.log
[root@centos /tmp]#ls file*.log
'file*.log'   file1.log
[root@centos /tmp]#ls 'file*.log'
'file*.log'

2. {}与[]这两个符号有什么区别

在通配符中,{}表示生产序列,[]表示括号表达式

  • {} 最常用的就是生成序列

  • [] 最常用的功能是正则表达式中的筐,表示或者

2.1 {} 花括号,大括号,生产序列

{} 可以实现打印重复字符串的简化形式

{元素1,元素2,元素3} 

{元素1..元素2}

示例

[root@centos8 ~]#echo file{1,3,5,7,9}
file1 file3 file5 file7 file9
[root@centos8 ~]#echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@centos8 ~]#echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@centos8 ~]#echo {1..10..2}
1 3 5 7 9
[root@centos8 ~]#echo {000..20..2}
000 002 004 006 008 010 012 014 016 018 020
[root@centos8 ~]#echo {a..z..2}
a c e g i k m o q s u w y
[root@centos8 ~]#echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
[root@centos8 ~]#

关闭和启用{}的扩展功能

[root@centos8 ~]#echo $-
himBHs
[root@centos8 ~]#echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@centos8 ~]#set +B
[root@centos8 ~]#echo $-
himHs
[root@centos8 ~]#echo {1..10}
{1..10}
[root@centos8 ~]#set -B
[root@centos8 ~]#echo $-
himBHs
[root@centos8 ~]#echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@centos8 ~]#

2.2 []表示括号表达式

  • [abc]表示a或者b或者c,即字母abc中的任何一个

  • [a-z]表示字母a到z中的任何一个字母

[root@centos8 temp]#touch time{1..9}
[root@centos8 temp]#ll
total 0
drwxr-xr-x  2 root root 123 Apr 29 09:10 ./
drwxr-xr-x. 6 root root 153 Apr 29 09:10 ../
-rw-r--r--  1 root root   0 Apr 29 09:10 time1
-rw-r--r--  1 root root   0 Apr 29 09:10 time2
-rw-r--r--  1 root root   0 Apr 29 09:10 time3
-rw-r--r--  1 root root   0 Apr 29 09:10 time4
-rw-r--r--  1 root root   0 Apr 29 09:10 time5
-rw-r--r--  1 root root   0 Apr 29 09:10 time6
-rw-r--r--  1 root root   0 Apr 29 09:10 time7
-rw-r--r--  1 root root   0 Apr 29 09:10 time8
-rw-r--r--  1 root root   0 Apr 29 09:10 time9
[root@centos8 temp]#ll time[0-5]
-rw-r--r-- 1 root root 0 Apr 29 09:10 time1
-rw-r--r-- 1 root root 0 Apr 29 09:10 time2
-rw-r--r-- 1 root root 0 Apr 29 09:10 time3
-rw-r--r-- 1 root root 0 Apr 29 09:10 time4
-rw-r--r-- 1 root root 0 Apr 29 09:10 time5
[root@centos8 temp]#

3. 正则表达式

正则表达式主要用于在文件中查找内容

  • {n,m} 花括号 重复前面一个字符n次到m次

  • [] 表示筐 [abc] 表示a或者b或者c

其实在正则表达式中和通配符中[]的意思是类似的。都表示一个筐 筐里面的东西

示例:[abc]:表示 a 或者 b 或者 c 其中任意一个字母

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值