欢迎关注个人公众号 spider-learngit
fd
(https://github.com/sharkdp/fd) 是 find
命令的一个更现代的替换。github
OLD正则表达式
-> % find . -name "*hello*" ./courses/hello_world.go ./courses/chapter_01/hello_world.go ./courses/chapter_01/hello_world ./examples/01_hello_world.go
NEWexpress
-> % fd hello courses/chapter_01/hello_world courses/chapter_01/hello_world.go courses/hello_world.go examples/01_hello_world.go
好比说查找符合 \d{2}_ti
模式的文件。find
使用的正则表达式很是古老,好比说在这里咱们不能使用 \d
,也不能使用 {x}
这种语法。所以咱们须要对咱们的正则表达式作一些改写。关于find
支持的正则表达式这里就不展开了。ide
fd
默认就是使用的正则表达式做为模式,而且默认匹配的是文件名;而 find
默认匹配的是完整路径。工具
OLDcode
-> % find . -regex ".*[0-9][0-9]_ti.*" ./examples/33_tickers.go ./examples/48_time.go ./examples/28_timeouts.go ./examples/50_time_format.go ./examples/32_timers.go
NEWorm
-> % fd '\d{2}_ti' examples/28_timeouts.go examples/32_timers.go examples/33_tickers.go examples/48_time.go examples/50_time_format.go
find
的语法是 find DIRECTORY OPTIONS
;而 fd
的语法是 fd PATTERN [DIRECTORY]
。注意其中目录是可选的。这点我的认为很是好,由于大多数状况下,咱们是在当前目录查找,每次都要写 .
很是烦。文档
OLD字符串
-> % find examples -name "*hello*" examples/01_hello_world.go
NEW
-> % fd hello examples examples/01_hello_world.go
find 会打印帮助信息,而 fd 则会显示当前目录的全部文件。
OLD
-> % find usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression] find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]
NEW
-> % fd courses courses/chapter_01 courses/chapter_01/chapter_1.md courses/chapter_01/chapter_1.pdf courses/chapter_01/hello_world courses/chapter_01/hello_world.go
这是一个很常见的需求,find
中须要使用 -name "*.xxx"
来过滤,而 fd
直接提供了 -e
选项。
OLD
-> % find . -name "*.md" ./courses/chapter_01/chapter_1.md ./courses/chapter_1.md
NEW
-> % fd -e md courses/chapter_01/chapter_1.md courses/chapter_1.md
.gitignore
中的文件find
并无提供对 .gitingnore
文件的原生支持,更好的方法多是使用 git ls-files
。而做为一个现代工具,fd
则默认状况下就会过滤 gitignore
文件,更多状况请查阅文档。
可使用 -I
来包含这些文件,使用 -H
添加隐藏文件。
OLD
-> % git ls-files | grep xxx
NEW
-> % fd xxx
OLD
-> % find . -path ./examples -prune -o -name '*.go' ./courses/hello_world.go ./courses/chapter_01/hello_world.go ./examples
NEW
-> % fd -E examples '.go$' courses/chapter_01/hello_world.go courses/hello_world.go
通常来讲,若是使用管道过滤的话,须要使用 '\0' 来做为字符串结尾,避免一些潜在的空格引发的问题。
在 find
中须要使用 -print0
来调整输出 '\0' 结尾的字符串,在 xargs
中须要使用 -0
表示接收这种字符串。而在 fd
中,和 xargs
保持了一直,使用 -0
参数就能够了。
OLD
-> % find . -name "*.go" -print0 | xargs -0 wc -l 7 ./courses/hello_world.go 7 ./courses/chapter_01/hello_world.go 50 ./examples/07_switch.go ...
NEW
-> % fd -0 -e go | xargs -0 wc -l 7 courses/chapter_01/hello_world.go 7 courses/hello_world.go 7 examples/01_hello_world.go ...
总之,fd 命令相对于 find 来讲至关简单易用了
OLD
-> % find . -name "*.md" -exec wc -l {} \; 114 ./courses/chapter_01/chapter_1.md 114 ./courses/chapter_1.md
NEW
You could also omit the {}
-> % fd -e md --exec wc -l {} 114 courses/chapter_1.md 114 courses/chapter_01/chapter_1.md
欢迎关注个人公众号 spider-learn