歡迎關注個人公衆號 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