通配符 and [] 中括号的用法
文件通配符
- 非预定义的字符类:
* 匹配零个或多个字符
? 匹配任何单个字符
~ 当前用户家目录
~username 用户username家目录
~+ 当前工作目录
~- 前一个工作目录
[0-9] 匹配数字范围
[a-z] 字母
[A-Z] 字母
[wang] 匹配列表中的任何的一个字符
[^wang] 匹配列表中的所有字符以外的字符
- 预定义的字符类
man 7 glob
[:digit:] 任意数字,相当于0-9
[:lower:] 任意小写字母
[:upper:] 任意大写字母
[:alpha:] 任意大小写字母
[:alnum:] 任意数字或字母
[:blank:] 水平空白字符
[:space:] 水平或垂直空白字符
[:punct:] 标点符号
[:print:] 可打印字符
[:cntrl:] 控制(非打印)字符
[:graph:] 图形字符
[:xdigit:] 十六进制字符
[^[:upper:]] 排除大写字母
[^[:lower:]] 排除小写字母
[^[:digit:]] 排除数字
示例
- 预定义的字符类示例
通过
ls f[a-Z]
查看到的是大小写混起来的,排列顺序是 a A b B ... z Z要想单独查看大写字母或小写字母命名的文件,需要使用
[[:lower:]]
或[[:upper:]]
由于
[:lower:]
等价于a-z
,[:upper:]
等价于A-Z
,所以需要载外面加上[]
[[email protected] app]$ls f[a-z]
fa fB fd fE fg fH fj fK fm fN fp fQ fs fT fv fW fy
fA fc fD ff fG fi fJ fl fM fo fP fr fS fu fV fx fY
fb fC fe fF fh fI fk fL fn fO fq fR ft fU fw fX fz
[[email protected] app]$ls f[[:lower:]]
fa fc fe fg fi fk fm fo fq fs fu fw fy
fb fd ff fh fj fl fn fp fr ft fv fx fz
[[email protected] app]$
[[email protected] app]$ls f[A-Z]
fA fc fD ff fG fi fJ fl fM fo fP fr fS fu fV fx fY
fb fC fe fF fh fI fk fL fn fO fq fR ft fU fw fX fz
fB fd fE fg fH fj fK fm fN fp fQ fs fT fv fW fy fZ
[[email protected] app]$ls f[[:upper:]]
fA fC fE fG fI fK fM fO fQ fS fU fW fY
fB fD fF fH fJ fL fN fP fR fT fV fX fZ
[[email protected] app]$
- 预定义的字符类和其他的混用
[[email protected] app]$ls f[[:lower:]357]
f3 f7 fb fd ff fh fj fl fn fp fr ft fv fx fz
f5 fa fc fe fg fi fk fm fo fq fs fu fw fy
- 排除
[[email protected] app]$ls f[^[:upper:]]
f1 f3 f5 f7 f9 fb fd ff fh fj fl fn fp fr ft fv fx fz
f2 f4 f6 f8 fa fc fe fg fi fk fm fo fq fs fu fw fy
[[email protected] app]$ls f[^[:lower:]]
f1 f3 f5 f7 f9 fB fD fF fH fJ fL fN fP fR fT fV fX fZ
f2 f4 f6 f8 fA fC fE fG fI fK fM fO fQ fS fU fW fY
[[email protected] app]$ls f[^[:digit:]]
fa fB fd fE fg fH fj fK fm fN fp fQ fs fT fv fW fy fZ
fA fc fD ff fG fi fJ fl fM fo fP fr fS fu fV fx fY
fb fC fe fF fh fI fk fL fn fO fq fR ft fU fw fX fz
[[email protected] app]$ls f[^0-9]
fa fB fd fE fg fH fj fK fm fN fp fQ fs fT fv fW fy fZ
fA fc fD ff fG fi fJ fl fM fo fP fr fS fu fV fx fY
fb fC fe fF fh fI fk fL fn fO fq fR ft fU fw fX fz
[[email protected] app]$
- 仅显示但当前目录下的隐藏文件或文件夹
ls -d .[^.]*
ls -d .*
l.
l.
其实就是ls -d .*
的别名
[[email protected] ~]$ls -d .[^.]*
.abrt .cache .gconf .gtk-bookmarks .local .tcshrc
.bash_history .config .gconfd .gvfs .nautilus .viminfo
.bash_logout .cshrc .gnome2 .ICEauthority .pulse
.bash_profile .dbus .gnote .imsettings.log .pulse-cookie
.bashrc .esd_auth .gnupg .lesshst .ssh
[[email protected] ~]$
[[email protected] ~]$ls -d .*
. .bashrc .gconf .gvfs .pulse
.. .cache .gconfd .ICEauthority .pulse-cookie
.abrt .config .gnome2 .imsettings.log .ssh
.bash_history .cshrc .gnote .lesshst .tcshrc
.bash_logout .dbus .gnupg .local .viminfo
.bash_profile .esd_auth .gtk-bookmarks .nautilus
[[email protected] ~]$
[[email protected] ~]$l.
. .bashrc .gconf .gvfs .pulse
.. .cache .gconfd .ICEauthority .pulse-cookie
.abrt .config .gnome2 .imsettings.log .ssh
.bash_history .cshrc .gnote .lesshst .tcshrc
.bash_logout .dbus .gnupg .local .viminfo
.bash_profile .esd_auth .gtk-bookmarks .nautilus
[[email protected] ~]$
https://www.cnblogs.com/shichangming/p/10011073.html
原文地址:https://www.cnblogs.com/shichangming/p/10011073.html
时间: 2024-11-04 22:41:30