主要内容:
1、shell下文件名替换字符;
2、具体例子;
shell提供了字符串模式匹配规则,称作元字符,利用元字符可方便地按照模式匹配文件。
一、替换字符、
1、
*: 匹配文件名中的任何字符串,包括空字符串。
?: 匹配文件名中的任何单个字符。
[...]: 匹配[ ]中所包含的任何字符。
[!...]: 匹配[ ]中非感叹号!之后的字符。
当s h e l l遇到上述字符时,就会把它们当作特殊字符,而不是文件名中的普通字符,这样用户就可以用它们来匹配相应的文件名。
二、例子;
1、列出以fi开头的文件;
- [admin@localhost ccd]$
-
[admin@localhost ccd]$ ls
-
\ abc cbcd c.txt d.txt fi.txt ll.or ma234Y45du May mayD ua244yu4m uo
-
267uy.txt b.txt cc dir.txt file grep.txt lls.or may MaY tee uh
-
[admin@localhost ccd]$ ls fi*
-
file fi.txt
-
[admin@localhost ccd]$
2、列出以.txt为结尾的文件;
- [admin@localhost ccd]$ ls
-
\ abc cbcd c.txt d.txt fi.txt ll.or ma234Y45du May mayD ua244yu4m uo
-
267uy.txt b.txt cc dir.txt file grep.txt lls.or may MaY tee uh
-
[admin@localhost ccd]$ ls *.txt
-
267uy.txt b.txt c.txt dir.txt d.txt fi.txt grep.txt
-
[admin@localhost ccd]$
3、列出以u开头后任意字符文件;
- [admin@localhost ccd]$ ls
-
\ abc cbcd c.txt d.txt fi.txt ll.or ma234Y45du May mayD ua244yu4m uo
-
267uy.txt b.txt cc dir.txt file grep.txt lls.or may MaY tee uh
-
[admin@localhost ccd]$ ls u?
-
uh:
-
123.txt helo oMod orcc orodu or.txt our.fdsfs OUT st.txt t.txt urhere.txt uu
-
-
uo:
-
[admin@localhost ccd]$
4、列出以m或M开头跟任意字符后为y或者Y的文件;
- [admin@localhost ccd]$ ls
- \ abc cbcd c.txt d.txt fi.txt ll.or ma234Y45du May mayD ua244yu4m uo
- 267uy.txt b.txt cc dir.txt file grep.txt lls.or may MaY tee uh
- [admin@localhost ccd]$
- [admin@localhost ccd]$ ls -l [mM]?[Yy]*
- may:
- total 4
- drwxrwxr-x 2 admin admin 4096 2012-02-09 17:42 cc
- May:
- total 0
- MaY:
- total 0
- mayD:
- total 4
- drwxrwxr-x 2 admin admin 4096 2012-02-09 17:46 uu
- [admin@localhost ccd]$
5、列出以非字母开头的文件;
- [admin@localhost ccd]$
-
[admin@localhost ccd]$ ls
-
\ abc cbcd c.txt d.txt fi.txt ll.or ma234Y45du May mayD ua244yu4m uo
-
267uy.txt b.txt cc dir.txt file grep.txt lls.or may MaY tee uh
-
[admin@localhost ccd]$ ls -l [!a-zA-Z]*
-
-rw-rw-r-- 1 admin admin 0 2012-02-09 16:59 267uy.txt
-
-
\:
-
total 0
-
[admin@localhost ccd]$
6、m或u开头后跟任意字符,之后为两个数字- [admin@localhost ccd]$ ls
-
\ abc cbcd c.txt d.txt fi.txt ll.or ma234Y45du May mayD ua244yu4m uo
-
267uy.txt b.txt cc dir.txt file grep.txt lls.or may MaY tee uh
-
[admin@localhost ccd]$ ls -l [mu]?[0-9][0-9]*
-
ma234Y45du:
-
total 0
-
-
ua244yu4m:
-
total 0
-
[admin@localhost ccd]$
阅读(884) | 评论(0) | 转发(0) |