文件通配符与命令行扩展

* 匹配零个或多个字符
? 匹配任何单个字符
~ 当前用户家目录
~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

相关推荐

  • Linux自动备份脚本

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1541163        今天网上一个朋友问了我一个shell的题目,让我帮他做下。下面是题目以及解题思路。 题目:…

    Linux干货 2016-08-15
  • 搭建缓存功能的WEB服务集群

    搭建缓存功能的WEB服务集群 实验简介 本文主要介绍双主模型的nginx proxy高可用集群的搭建方式。实验环境: 使用nfs服务器提供页面数据共享 使用单独的mariadb服务器提供关系型数据库 使用两台httpd服务器处理动态的php和静态页面资源 使用两台nginx服务器处理图片资源 使用两台varnish服务器作缓存处理 使用两台nginx作代理 …

    Linux干货 2017-07-15
  • ansible学习笔记之1

    ansible学习笔记之1 ansible学习笔记之1 ansible 基础 ad-hoc 基础概念 ansible学习笔记之1 说说运维工具的类型 > 运维工具按是否需要有代理程序来划分分为两类:      agent(需要代理工具):          基于专用的age…

    2016-11-21
  • LNMP内网部署wiki

    需求:内部人员经常到查阅资料,考虑在内网搭建wiki站点。 实验拓扑: 实验环境:        Nginx,PHP:192.168.198.160,10.0.0.07        MySQL:10.0.0.8 软件包:  &n…

    Linux干货 2015-10-15