fd - 更好的 find 命令


歡迎關注我的公眾號 spider-learn

fdhttps://github.com/sharkdp/fd)find 命令的一個更現代的替換。

對比一下

查找名字含有某個字符的文件

OLD

-> % find . -name "*hello*"
./courses/hello_world.go
./courses/chapter_01/hello_world.go
./courses/chapter_01/hello_world
./examples/01_hello_world.go

NEW

-> % 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支持的正則表達式這里就不展開了。

fd 默認就是使用的正則表達式作為模式,並且默認匹配的是文件名;而 find 默認匹配的是完整路徑。

OLD

-> % 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

NEW

-> % 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM