article/linux/glob.md
2022-11-01 22:55:36 +08:00

38 lines
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## glob 简介
`glob` 是用于匹配符合指定模式的文件集合的一种语言, 类似于正则表达式, 但更加简单。
**Update-09-22:** 前两天阮一峰老师更新了他的博客,内容讲的就是 `Glob`, 强烈推荐。
- [命令行通配符教程 - 阮一峰](https://links.jianshu.com/go?to=http%3A%2F%2Fwww.ruanyifeng.com%2Fblog%2F2018%2F09%2Fbash-wildcards.html)
## glob 语法
`glob` 的语法很简单:
| 通配符 | 描述 | 例子 | 匹配 | 不匹配 |
| -------------- | ------------------------------------ | ------------------------- | ----------------------------------------- | --------------------------------- |
| `*` | 匹配任意数量的任何字符,包括无 | `Law*` | `Law`, `Laws`, `Lawyer` | `GrokLaw`, `La`, `aw` |
| `?` | 匹配任何 **单个** 字符 | `?at` | `Cat`, `cat`, `Bat`, `bat` | `at` |
| `[abc]` | 匹配括号中给出的一个字符 | `[CB]at` | `Cat`, `Bat` | `cat`, `bat` |
| `[a-z]` | 匹配括号中给出的范围中的一个字符 | `Letter[0-9]` | `Letter0`, `Letter1``Letter9` | `Letters`, `Letter`, `Letter10` |
| `[!abc]` | 匹配括号中未给出的一个字符 | `[!C]at` | `Bat`, `bat`, `cat` | `Cat` |
| `[!a-z]` | 匹配不在括号内给定范围内的一个字符 | `Letter[!3-5]` | `Letter1`… | `Letter3``Letter5`, `Letterxx` |
| `{a..z}` | 匹配括号中给出的一个字符,等同于[abc] | `{CB}at` | `Cat`, `Bat` | `cat`, `bat` |
| `{start..end}` | 会匹配连续范围的字符 | `d{a..d}g` | `dag`, `dbg`, `dcg`, `ddg` | |
| | | `.{mp{3..4},m4{a,b,p,v}}` | `.mp3` `.mp4` `.m4a` `.m4b` `.m4p` `.m4v` | |
## 4. 注意事项
通配符有一些使用注意点,不可不知。
**1通配符是先解释再执行。**
Bash 接收到命令以后,发现里面有通配符,会进行通配符扩展,然后再执行命令。
> 作者:永往直前
> 链接https://www.jianshu.com/p/d7a97b17ee5a
> 来源:简书
> 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。