Linux 文件通配符与命令行扩展

简介:

* 匹配零个或多个字符

? 匹配任何单个字符

当前用户家目录

~mage 用户mage家目录

~+ 当前工作目录

~- 前一个工作目录

[0-9] 匹配数字范围

[a-z]: 字母

[A-Z]: 字母

[wang] 匹配列表中的任何的一个字符

[^wang]匹配列表中的所有字符以外的字符

其他特殊字符

\   跳脱符号:将『特殊字符或通配符』还原成一般字符

| 管道 (pipe):分个两个管道命令的界定;

;    连续指令下达分隔符: (注意!与管道命令不相同)

&       工作控制 (job control):将指令变成成背景下工作

!       逻辑运算意义上的『非』 not 癿意思

预定义的字符类:man 7 glob

[:digit:]:任意数字,相当于0-9

[:lower:]:任意小写字母

[:upper:]: 任意大写字母

[:alpha:]: 任意大小写字母

[:alnum:]:任意数字或字母

[:blank:]:水平空白字符

[:space:]:水平或垂直空白字符

[:punct:]:标点符号

[:print:]:可打印字符

[:cntrl:]:控制(非打印)字符

[:graph:]:图形字符

[:xdigit:]:十六进制字符

1、显示/test目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目

方法1

#ls -d /test/l*[0-9]*[[:lower:]]

/test/ll4898ufjAj  /test/ll4ufjAAj  /test/ll4ufjAj  /test/ll4ufjAjc

方法2

#ls -d /test/l*[[:digit:]]*[[:lower:]]

/test/ll4898ufjAj  /test/ll4ufjAAj  /test/ll4ufjAj  /test/ll4ufjAjc

方法3

#ls -d /test/l*[^a-zA-z]*[[:lower:]]

/test/ll4898ufjAj  /test/ll4ufjAAj  /test/ll4ufjAj  /test/ll4ufjAjc

2、显示/test目录下以任意一位数字开头,且以非数字结尾的文件或目录

方法1

#ls -d /test/[[:digit:]]*[[:alpha:]]

/test/112prueiruenjfdkeIEJI88.conf

方法2

#ls -d /test/[0-9]*[^0-9]

/test/112prueiruenjfdkeIEJI88.conf

方法3

#ls -d /test/[[:digit:]]*[a-zA-Z]

/test/112prueiruenjfdkeIEJI88.conf

3、显示/test/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录

方法1

#ls -d /test/[0-9][[:alpha:]]*

/test/0ll4ufjAjBB007

方法2

#ls -d /test/[^[:alpha:]][[:alpha:]]*

/test/0ll4ufjAjBB007

方法3

#ls -d /test/[^a-zA-Z][a-zA-Z]*

/test/0ll4ufjAjBB007

4、显示/test/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录

#ls -d /test/rc[0-6]*

/test/rc1rjeirie  /test/rc2jidf9fdjfd  /test/rc556kkfjdjfkd

5、显示/etc目录下,所有以.d结尾的文件或目录

#ls -d /etc/*.d

/etc/bash_completion.d  /etc/logrotate.d     /etc/rc1.d          /etc/rwtab.d

/etc/chkconfig.d        /etc/lsb-release.d   /etc/rc2.d          /etc/sane.d

/etc/cron.d             /etc/makedev.d       /etc/rc3.d          /etc/setuptool.d

/etc/depmod.d           /etc/modprobe.d      /etc/rc4.d          /etc/statetab.d

6、显示/test目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录

#ls -d /test/[mnrp]*.conf

/test/mrueiruenjfdkeIEJI88.conf  /test/prueiruenjfdkeIEJI88.conf

/test/nrueiruenjfdkeIEJI88.conf  /test/rrueiruenjfdkeIEJI88.conf

7、只显示/root下的隐藏文件和目录

#ls -d /root/.[^.]*

/root/.abrt          /root/.cshrc     /root/.gtk-bookmarks   /root/.nautilus

/root/.bash_history  /root/.dbus      /root/.gvfs            /root/.pulse

/root/.bash_logout   /root/.esd_auth  /root/.history         /root/.pulse-cookie

/root/.bash_profile  /root/.gconf     /root/.ICEauthority    /root/.ssh

8、只显示/root下的隐藏目录

#ls -d /root/.[^.]*/

/root/.abrt/    /root/.dbus/    /root/.gnote/  /root/.local/     /root/.ssh/

/root/.cache/   /root/.gconf/   /root/.gnupg/  /root/.nautilus/

/root/.config/  /root/.gnome2/  /root/.gvfs/   /root/.pulse/

9、只显示/etc下的非隐藏目录

#ls -d /etc/[^.]*/

/etc/abrt/               /etc/gcrypt/          /etc/ntp/               /etc/reader.conf.d/

/etc/acpi/               /etc/gdm/             /etc/obex-data-server/  /etc/redhat-lsb/

括号扩展: { }

重复字符串的简化形式

分号分隔具体内容

#touch file{1,3,5};ls file*

file1  file3  file5

{起始值..结束值}

#echo {0..10}

0 1 2 3 4 5 6 7 8 9 10

#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

#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

#echo {a..Z}

a ` _ ^ ]  [ Z

{格式..最大值..步进数}

#echo {000..10..1}

000 001 002 003 004 005 006 007 008 009 010



本文转自 ljpwinxp 51CTO博客,原文链接:http://blog.51cto.com/191226139/1981393

相关文章
|
7天前
|
Linux 开发工具
Linux E325: 注意 发现交换文件 “*.swp“
Linux E325: 注意 发现交换文件 “*.swp“
14 0
|
2天前
|
Linux Shell Python
如何计算 Linux 上文件中的空行数?
【5月更文挑战第11天】
7 0
|
2天前
|
Linux API
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
|
2天前
|
Linux
linux中wait与waitpid函数使用场景及扩展
linux中wait与waitpid函数使用场景及扩展
|
3天前
|
Linux
如何在 Linux 中递归更改文件的权限?
【5月更文挑战第10天】
14 3
|
3天前
|
Linux
Linux如何查询较大文件的方法
【5月更文挑战第8天】Linux如何查询较大文件的方法
6 0
|
4天前
|
Linux 程序员 Shell
【Linux】详解core dump文件的作用以及用法&&ubuntu20.04下无法形成core dump文件的解决办法
【Linux】详解core dump文件的作用以及用法&&ubuntu20.04下无法形成core dump文件的解决办法
|
4天前
|
Linux Shell
Linux操作系统下查找大文件或目录的技巧
Linux操作系统下查找大文件或目录的技巧
10 2
|
4天前
|
算法 Linux
Linux:文件增删 & 文件压缩指令
Linux:文件增删 & 文件压缩指令
7 0
|
6天前
|
缓存 监控 前端开发
如何在 Linux 命令行中检查 CPU 使用率
【5月更文挑战第8天】
16 0