文件通配符与命令行扩展

* 匹配零个或多个字符
? 匹配任何单个字符
~ 当前用户家目录
~mage 用户mage家目录
~+ 当前工作目录
~- 前一个工作目录
[0-9] 匹配数字范围
[a-z] 字母
[A-Z]字母
         [a-Z] 会以aAbBcC…小大小大列出,特别要注意
[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

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/88301

(0)
上一篇 2017-11-11 18:13
下一篇 2017-11-12 13:10

相关推荐

  • 千万不要把 bool 设计成函数参数

    我们有很多Coding Style 或 代码规范。但这一条可能会经常被我们所遗忘,就是我们经常会在函数的参数里使用bool参数,这会大大地降低代码的可读性。不信?我们先来看看下面的代码。 当你读到下面的代码,你会觉得这个代码是什么意思? widget->repaint(false); 是不要repaint吗?还是别的什么意思?看了文档后,我们才知道这个…

    Linux干货 2016-07-10
  • Find工具实例

      1、显示当前系统上root、fadora或user1用户的默认shell。         [root@localhost ~]# grep -E "^(root|fadora|user1)\>"&n…

    Linux干货 2016-11-27
  • python之psutil模块

    python 安装psutil 来实现获取系统信息  # yum -y install python*pip # yum -y groupinstall “Development Tools # yum -y install python34-devel.x86_64 # pip3 install –upgrade pip # pip3 inst…

    Linux干货 2017-03-08
  • Mozart的剑(文本处理工具)——贰剑(head、tail、cut、sort、uniq、wc、diff、paste、patch)

    有点拖了,没有好好整理之前的内容,拖延症害死人….. 这次介绍一些有趣的小文本处理工具,可以方便截取文本内容、排序、备份之类的。 head 用法:head [选项]… [文件]… head[OPTION]…[FILE]… 默认将每个指定文件的头10行显示到标准输出。如果指定了多于一个文件,在每一段输…

    Linux干货 2017-08-02
  • 简述计算机网络中的物理层

          简要说明物理层在网络传输中做了哪些工作解决的问题      物理层是计算机网络OSI模型中最低的一层。物理层规定:为传输数据所需要的物理链路创建、维持、拆除,而提供具有机械的,电子的,功能的和规范的特性。简单的说,物理层确保原始的数据比特流可在各种物理媒体上传输。使得…

    Linux干货 2017-05-08