shell中的通配符
来源:互联网 发布:外贸邦海关数据有用吗 编辑:程序博客网 时间:2024/06/05 09:04
为了清理一些日志文件,这些文件有部分特征,通过使用shell通配符可以匹配到,当然正则也可以,而且更强大。这里先说shell的通配符
一贯的拿来主义,这里就又去copy别人的了。
转载地址:http://blog.csdn.net/miss_acha/article/details/24462519
- 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/binfor((i=1;i<=10;i++))do x=2017_7_$i.log touch $xdone#执行下脚本看看[root@VM_157_218_centos test_shell_dir]# ls2017_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.log3) []的使用[root@VM_157_218_centos test_shell_dir]# ls2017_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]# ls2017_8_7.log 2017_8_8.log 2017_8_9.log test.sh4) [^]号的使用#现在我把除今天以外的所有日志删除,那么可以[root@VM_157_218_centos test_shell_dir]# sh creat_log_file.sh [root@VM_157_218_centos test_shell_dir]# ls2017_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]# ls2017_8_9.log test.sh
今天先写到这,有问题再写。
阅读全文
0 0
- 十、Shell中的通配符
- shell中的通配符
- shell中的通配符
- shell中的通配符
- shell中的通配符与正则表达式
- shell中的通配符以及特殊符
- shell中的通配符和转义字符
- Shell 通配符
- shell 通配符
- shell通配符
- SHELL 通配符
- shell 通配符
- shell通配符
- shell通配符
- shell通配符
- 《shell中的通配符,特殊字符和正则表达式》
- 正则表达式与 Linux shell 中的通配符(wildcard)
- shell 中的通配符和正规表示法(正则表达式)
- Laravel 使用insert插入数据,字段created_at为0000?
- 数组操作小技巧
- 浅谈Android中的MVVM模式
- 2017暑假集训 div1 最短路(1)
- 获取文本框 、单选、多选框的值
- shell中的通配符
- hiho一下 第158周 非法二进制数
- 完美三角形
- Cocoapods updating local specs repositories
- 通讯录(使用数据库)
- 1008. Elevator (20)
- 发个声
- tensorflow中softmax_cross_entropy_with_logits函数的实现
- (转载)阿里面试回来,想和Java程序员谈一谈