Linux通配符

Linux通配符
说明:通配符是bash的内置功能,几乎适用于全部Linux命令。
*  匹配任意(0个或多个)字符或字符串,包括空字符串。
?  匹配任意1个字符,有且只有一个字符。
[abcd]  匹配abcd中任何一个字符,abcd也能够是其余任意不连续字符。
[a-z]  匹配a到z之间的任意一个字符,字符先后要连续,也能够用连续数字,即[1-9]。
[!abcd]  表示不匹配括号里面的任何一个字符,也能够写为 [!a-d],这里的 "!"号能够用"^" 替代,即[^abcd]


通配符"*" 示例:
[root@testdb62 test]# touch exec.sh liang.log guo.log liang.sql a.sql
[root@testdb62 test]# ls 
a.sql  exec.sh  guo.log  liang.log  liang.sql

查看全部结尾为 log 结尾的文件
[root@testdb62 test]# ls *.log
guo.log  liang.log

查看全部结尾为 sh 结尾的文件
[root@testdb62 test]# ls *.sh
exec.sh


通配符"?" 示例:
[root@testdb62 test]# ls ?.sh
ls: 没法访问?.sh: 没有那个文件或目录
[root@testdb62 test]# touch a.sh
[root@testdb62 test]# ls ?.sh
a.sh
[root@testdb62 test]# ls ???.sh
ls: 没法访问???.sh: 没有那个文件或目录
[root@testdb62 test]# ls ???.log
guo.log


通配符"[abcd]" 示例:
[root@testdb62 test]# ls [abcd].sh
a.sh
[root@testdb62 test]# ls gu[opq].log
guo.log


通配符"[a-z]" 示例:
[root@testdb62 test]# touch c.sh d.sh cd.sh 
[root@testdb62 test]# ls 
a.sh  cd.sh  c.sh  d.sh  exec.sh  guo.log  liang.log  liang.sql
[root@testdb62 test]# ls [a-z].sh
a.sh  c.sh  d.sh
[root@testdb62 test]# ls [a-z]???.sh
exec.sh
[root@testdb62 test]# touch 1 
[root@testdb62 test]# touch 2
[root@testdb62 test]# ls [1-9]
1  2


通配符"[!abcd]" 示例:
[root@testdb62 test]# touch a b c d e f 
[root@testdb62 test]# ls [^abcd]
1  2  e  f
[root@testdb62 test]# ls 
1  2  a  a.sh  b  c  cd.sh  c.sh  d  d.sh  e  exec.sh  f  guo.log  liang.log  liang.sql
[root@testdb62 test]# ls [!abcd]
1  2  e  f
[root@testdb62 test]# ls [!a-d]
1  2  e  f
[root@testdb62 test]# ls [^1-9]
a  b  c  d  e  f
相关文章
相关标签/搜索