shell中的通配符

为了清理一些日志文件,这些文件有部分特征,通过使用shell通配符可以匹配到,当然正则也可以,而且更强大。这里先说shell的通配符
一贯的拿来主义,这里就又去copy别人的了。
转载地址:http://blog.csdn.net/miss_acha/article/details/24462519

  1. shell支持的通配符

        shell支持一组通配符用于处理数据,但是要和正则表达式区别开来。shell的通配符实现的功能比较简单,常用于文件名匹配,远不及正则表达式强大和广泛。不过对于日常使用linux还是有非常大的帮助的。下表取自《鸟哥的linux私房菜》

符号意义
*代表『 0 个到无穷多个』任意字符
?代表『一定有一个』任意字符
[ ]同样代表『一定有一个在括号内』的字符(非任意字符)。例如 [abcd] 代表『一定有一个字符, 可能是 a, b, c, d 这四个任何一个』
[ - ]若有减号在中括号内时,代表『在编码顺序内的所有字符』。例如 [0-9] 代表 0 到 9 之间的所有数字,因为数字的语系编码是连续的!
[^ ]若中括号内的第一个字符为指数符号 (^) ,那表示『反向选择』,例如 [^abc] 代表 一定有一个字符,只要是非 a, b, c 的其他字符就接受的意思。


2. shell中的特殊字符(以bash为例)

        通配符常用于文件名匹配,而特殊字符的则协助shell完成各种具体工作。下表取自《鸟哥的linux私房菜》

符号内容
#批注符号:这个最常被使用在 script 当中,视为说明!在后的数据均不运行
\跳脱符号:将『特殊字符或通配符』还原成一般字符
|管道 (pipe):分隔两个管线命令的界定(后两节介绍);
;连续命令下达分隔符:连续性命令的界定 (注意!与管线命令并不相同)
~用户的家目录
$取用变量前导符:亦即是变量之前需要加的变量取代值
&工作控制 (job control):将命令变成背景下工作
!逻辑运算意义上的『非』 not 的意思!
/目录符号:路径分隔的符号
>, >>数据流重导向:输出导向,分别是『取代』与『累加』
<, <<数据流重导向:输入导向 (这两个留待下节介绍)
’ ‘单引号,不具有变量置换的功能
" "具有变量置换的功能!
` `两个『 ` 』中间为可以先运行的命令,亦可使用 $( )
( )在中间为子 shell 的起始与结束
{ }在中间为命令区块的组合!

3.下面写个很简单的脚本来生成一些文件

#!/bash/bin
for((i=1;i<=10;i++))
do 
   x=2017_7_$i.log
   touch $x
done
#执行下脚本看看
[root@VM_157_218_centos test_shell_dir]# ls
2017_7_1.log   2017_7_2.log  2017_7_4.log  2017_7_6.log  2017_7_8.log 
2017_7_10.log  2017_7_3.log  2017_7_5.log  2017_7_7.log  2017_7_9.log  test.sh
#成功生成了。
1*号的使用
[root@VM_157_218_centos test_shell_dir]# touch 2017_6 #新建一个相似文件
[root@VM_157_218_centos test_shell_dir]# ls 2017*6*  #这样的就匹配到了
2017_6  2017_7_6.log              
2)  ?号的使用
[root@VM_157_218_centos test_shell_dir]# ls   #生成类似文件
2017_7_1.log   2017_7_3.log  2017_7_6.log  2017_7_9.log   2017_8_2.log  2017_8_5.log  2017_8_8.log  2017_7_10.log  2017_7_4.log  2017_7_7.log  2017_8_1.log   2017_8_3.log  2017_8_6.log  2017_8_9.log 2017_7_2.log   2017_7_5.log  2017_7_8.log  2017_8_10.log  2017_8_4.log  2017_8_7.log 
#现在我想找每个月九号的日志文件,可以这样。
[root@VM_157_218_centos test_shell_dir]# ls 2017_?_9*
2017_7_9.log  2017_8_9.log
3) []的使用
[root@VM_157_218_centos test_shell_dir]# ls
2017_8_1.log   2017_8_2.log  2017_8_4.log  2017_8_6.log  2017_8_8.log 
2017_8_10.log  2017_8_3.log  2017_8_5.log  2017_8_7.log  2017_8_9.log  test.sh
#现在我要把8月7号之前的日志删了。
[root@VM_157_218_centos test_shell_dir]# rm -f 2017_8_[123456]*
[root@VM_157_218_centos test_shell_dir]# ls
2017_8_7.log  2017_8_8.log  2017_8_9.log   test.sh
4)  [^]号的使用
#现在我把除今天以外的所有日志删除,那么可以
[root@VM_157_218_centos test_shell_dir]# sh creat_log_file.sh 
[root@VM_157_218_centos test_shell_dir]# ls
2017_8_1.log   2017_8_2.log  2017_8_4.log  2017_8_6.log  2017_8_8.log 
2017_8_10.log  2017_8_3.log  2017_8_5.log  2017_8_7.log  2017_8_9.log  test.sh
[root@VM_157_218_centos test_shell_dir]# rm -f 2017_8_[^9]*
[root@VM_157_218_centos test_shell_dir]# ls
2017_8_9.log    test.sh

今天先写到这,有问题再写。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值