在前面的博文中,介绍了cp、mv、mkdir、rm、ln命令,都是Linux中经常使用到的命令。
它们是用来对文件和文件夹进行操作的。
它们完成的任务,使用图形界面也可做做到,比如拖拽文件或文件夹,剪切、删除文件等。
使用命令行,并没有比图形界面操作更方便快捷。
但使用命令行的好处是功能更强大和灵活,很多命令行能做到的事,图形界面无法做到。
记得为这些命令设计的很多参数了吧,用了这些参数可以满足各种情况的需求。
对于shell命令的强大之处,这里介绍一下其中一个重要特性:通配符。
在shell中,使用文件名是非常频繁的。所以为了快速对一组文件名进行操作,就提供了一些特殊的字符来协助。
这些特殊字符就叫做通配符。英文名Wildcards或globbing。
使用特殊组合的字符,让我们可以选择一组文件。
Wildcard |
Meaning |
* |
Matches any characters |
? |
Matches any single character |
[characters] |
Matches any character that is a member of the set characters |
[!characters] |
Matches any character that is not a member of the set characters |
[[:class:]] |
Matches any character that is a member of the specified class |
下面是常用的character class:
Character Class |
Meaning |
[:alnum:] |
Matches any alphanumeric character |
[:alpha:] |
Matches any alphabetic character |
[:digit:] |
Matches any numeral |
[:lower:] |
Matches any lowercase letter |
[:upper:] |
Matches any uppercase letter |
使用这些通配符的组合,就可以构造出复杂的选择条件,得到指定组合的文件名。
下面举一些各种模式的例子:
Pattern |
Matches |
* |
All files |
g* |
Any file beginning with "g" |
b*.txt |
Any file beginning with “b” followed by any characters and ending with “.txt” |
Data??? |
Any file beginning with “Data” followed by exactly three characters |
[abc]* |
Any file beginning with either an “a”, a “b”, or a “c” |
BACKUP.[0-9][0-9][0-9] |
Any file beginning with “BACKUP.” followed by exactly three numerals |
[[:upper:]]* |
Any file beginning with an uppercase letter |
[![:digit:]]* |
Any file not beginning with a numeral |
*[[:lower:]123] |
Any file ending with a lowercase letter or the numerals “1”, “2”, or “3” |
在上面的表述中,有一种范围表示法,在老的Unix和Linux中,都是支持的,现在也还是能用的,但请小心点。
比如[A-Z]、[A-z]就表示A-Z之间的任意字符和A-z之间的任意字符。
因为按照ASCII码顺序,数字在字母前,大写字母在小写字母前面,所以A-z之间的字符都包含在内,包括几个非字母字符。
而用[1-z],就包含了数字、大小写字母。
因为这种灵活性,在使用时最好指定清晰明确的范围。
在shell命令使用中,需要用文件名作为参数的地方,都可以使用通配符。
在GUI的文件操作模式下,有的也支持在地址栏输入通配符,来选择或显示相应的文件。