码迷,mamicode.com
首页 > 其他好文 > 详细

通配符

时间:2015-02-01 17:43:59      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

Shell入门到精通
第2章 文件置换
1、使用*号
可以使用*号匹配文件中的任意字符串
# cd /root/
# ls *.log
# ls i*
# ls *.sys*

2、使用?号
?号可以匹配任意的单个字符
# ls ??stall.log
install.log
# ls ???tall.log
install.log
[# ls ????all.log
install.log
[# ls ?????ll.log
install.log
[# ls ??????l.log
install.log
# ls ???????.log


3、使用[...]和[!...]
使用[...]可以匹配方括号[]中的任意字符
或者还可一使用 -表示两个字符或者数据的范围.
# ls [c]*
# ls [a-z].txt
a.txt A.txt b.txt B.txt c.txt C.txt
# ls [A-Z].txt
A.txt b.txt B.txt c.txt C.txt
# ls [0-9].txt
1.txt 2.txt 3.txt
# ls [!0-9].txt !表示取非
a.txt A.txt b.txt B.txt c.txt C.txt
# ls [!0-9].txt
a.txt A.txt b.txt B.txt c.txt C.txt
# ls [!a-z].txt
1.txt 2.txt 3.txt

4、编写脚本
(1)、编写一个脚本名字为gongda1.sh
需求如下:
使用*号匹配/root下面所有的文件或者目录,将输出内容放入/tmp/root.log文件中
并且告诉用户匹配命令成功,输出其中关于in开头的关键的文件
===========================
#!/bin/bash
ls /root/* >/tmp/root.log
if [ $?==0 ]
then
cd /root
ls [in]*
fi
~
============================
(2)、编写一个脚本名为gongda2.sh
需求如下:
使用?号和* 匹配sh的文件,并且要出内容放入/tmp/root1.log文件当中
并且告诉用户有多少个这种文件?
============================
#!/bin/bash
ls /root/*.sh >/tmp/root1.log
cat /tmp/root1.log |wc -l

=============================
(3)、编写一个脚本名为gongda3.sh
需求如下:
使用[...] 匹配.b开头的文件,要求输出内容放入/tmp/root2.lo文件当中
并且告诉用户有多少个这种文件?








通配符

标签:

原文地址:http://www.cnblogs.com/blog-acf/p/4265678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!