美文网首页
fd - 更好的 find 命令

fd - 更好的 find 命令

作者: 6de098b75a36 | 来源:发表于2018-04-05 20:35 被阅读161次

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

相关文章

  • fd - 更好的 find 命令

    fd(https://github.com/sharkdp/fd) 是 find 命令的一个更现代的替换。 对比一...

  • find命令、文件名后缀

    目录 一、 find命令二、 文件名后缀 一、 find命令 find命令find命令用来在指定目录下查找文件,其...

  • 文件搜索命令 find grep

    文件搜索命令 find <非常强大> find 命令 find [搜索范围] [搜索条件] 搜索文件 find /...

  • Linux系统find命令使用几点摘录

    find命令选项 Find命令的一般形式: find pathname -options [-print ][-e...

  • AI 1期 多姿多彩的图形

    巩固复习 神笔运动命令 神笔命令函数pen.fd.bk.rt.lt 前进(forward)pen.fd() 括号中...

  • find--linux

    Linux中find常见用法示例(转) find命令的参数; pathname: find命令所查找的目录路径。例...

  • MacOS通过命令行搜索文件

    1、通过Find命令搜索文件(注:效果一般) find命令非常高效,并且使用简单。find命令来自unix,mac...

  • linux学习第五周命令与文件权限

    上次内容回顾0.1 find 命令练习0.2 find 命令0.3 find命令及应用场景 今日内容 设置别名2....

  • linux 命令行整理2

    这次我们来看看Linux的查找文件的命令:find 命令格式: find 范围 匹配条件 比如:find /etc...

  • 碎碎念 b

    Linux中的搜索命令 文件搜索命令locate 命令搜索命令 忘记是啥了 find命令,其中find命令有好多后...

网友评论

      本文标题:fd - 更好的 find 命令

      本文链接:https://www.haomeiwen.com/subject/sovdhftx.html