当需要处理一组具有共同属性的文件时,Shell提供一种文件名生成机制,使用户能够利用元字符(或称通配符)实行模式匹配,最终生成一个具有同一属性文件列表。
例如,可以使用下列命令列出当前目录中的所有C程序文件。
ls -l *.c
与文件生成有关的元字符
wildcard | description |
* | Match any characters, whatever the amount, and characters string including empty |
? | Match a arbitrary character |
[] | Define a range of characters, the characters should be included by this range |
[!] or[^] | Define a range of characters, the characters should be excluded by this range |
例1,列出当前目录下任何一消协字母为其市政府的文件名。
ls -l [a-z]*
例2,列出当前目录汇总文件名前四位字符为ftpm,后三个字符为任何字符的所有文件。
ls -l ftpm???
例3,列出当前目录中以p或w为首字符的所有文件,可以使用ls命令:
ls -l [pw]*
例4,列出当前目录中受字符为答谢字母的所有文件
ls -l [A-Z]*
转义字符
1. "/*" "/?" "/$" "//" "/'" "/"", 分别表示* ? $ / ' "
2. ls -l '[Bb]*'
如果加上单引号',就将[Bb]*作为字符串来解释