fd - 更好的 find 命令

歡迎關注個人公衆號 spider-learngit

fdhttps://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

使用 xargs

通常來講,若是使用管道過濾的話,須要使用 '\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 來講至關簡單易用了

PS

使用 exec Using exec

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

相關文章
相關標籤/搜索