在 Linux 上用 fd 替换 find

fd 命令是一个流行的、用户友好的 find 命令替代方案。
52 位读者喜欢这篇文章。
How to find files in Linux

Lewis Cowles, CC BY-SA 4.0

许多 Linux 程序员每天都在使用 find 命令。但是 find 提供的文件系统条目集有限,如果你必须执行大量的 find 操作,它甚至不是很快速。因此,我更喜欢使用 Rust 编写的 fd 命令,因为它提供了适用于大多数用例的合理默认值。

正如它的 README 所说,“fd 是一个在文件系统中查找条目的程序。 它是 find 的一个简单、快速且用户友好的替代方案。”它具有并行目录遍历功能,因此可以一次搜索多个目录。 它支持正则表达式 (regex) 和基于 glob 的模式。

安装 fd

在 Linux 上,您可以从您的软件存储库安装 fd(可在 Repology 上的 fd 页面上找到可用软件包的列表。)例如,在 Fedora 上

$ sudo dnf install fd-find

在 macOS 上,使用 MacPortsHomebrew

或者,您可以使用 Rust 的 Cargo 包管理器

$ cargo install fd-find

使用 fd

要进行简单搜索,请在任何参数之后运行 fd,例如

$ fd sh
registry/src/github.com-1ecc6299db9ec823/cc-1.0.67/src/bin/gcc-shim.rs
registry/src/github.com-1ecc6299db9ec823/exa-0.10.1/completions/completions.bash
registry/src/github.com-1ecc6299db9ec823/exa-0.10.1/completions/completions.fish
registry/src/github.com-1ecc6299db9ec823/exa-0.10.1/completions/completions.zsh
registry/src/github.com-1ecc6299db9ec823/exa-0.10.1/xtests/run.sh
registry/src/github.com-1ecc6299db9ec823/git2-0.13.18/src/stash.rs
registry/src/github.com-1ecc6299db9ec823/libc-0.2.94/src/unix/solarish
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/cmake/SelectHashes.cmake
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/include/git2/stash.h
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/include/git2/sys/hashsig.h
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/script/backport.sh
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/script/leaks.sh
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/script/valgrind.sh
registry/src/github.com-1ecc6299db9ec823/libgit2-sys-0.12.19+1.1.0/libgit2/src/config_snapshot.c
[...]

如果要搜索特定目录,请将目录路径作为第二个参数提供给 fd,例如

$ fd passwd /etc
/etc/pam.d/passwd
/etc/passwd
/etc/passwd-
/etc/security/opasswd

要搜索特定文件扩展名,请使用 -e 选项。 例如

$ fd . '/home/ssur/exa' -e md
/home/ssur/exa/README.md
/home/ssur/exa/devtools/README.md
/home/ssur/exa/man/exa.1.md
/home/ssur/exa/man/exa_colors.5.md
/home/ssur/exa/xtests/README.md
$ 

您还可以通过提供 -x-X 来执行命令。

  • -x/--exec 选项为每个搜索结果(并行地)运行一个外部命令。
  • -X/--exec-batch 选项使用所有搜索结果作为参数启动一次外部命令。

例如,要递归查找所有 ZIP 压缩包并解压它们

$ fd -e zip -x unzip

或者,要列出特定目录下在过去 _n_ 天内更改过的所有文件,请使用 --changed-within 选项

$ fd . '/home/ssur/Work/' --changed-within 10d
/home/ssur/Work/wildfly/connector/src/main/java/org/jboss/as/connector/subsystems/data_sources/JdbcDriverAdd.java
/home/ssur/Work/wildfly/connector/src/main/java/org/jboss/as/connector/subsystems/data_sources/JdbcExample.java
[...]

要搜索在特定天数之前更改过的所有文件,请使用 --changed-before _n_ 选项

$ fd . '/home/ssur/Work/' --changed-before 365d

在这里,. 充当通配符条目,指示 fd 返回所有文件。

要了解有关 fd 更多功能的资讯,请查阅 GitHub 上的文档

结论

我特别喜欢 fd 的一点是,默认情况下搜索模式不区分大小写,即使您对要查找的内容了解不准确,也更容易找到内容。 更好的是,如果模式包含大写字符,它会*自动*切换为区分大小写。

另一个好处是它使用颜色编码来突出显示不同的文件类型。

如果您已经在使用这个令人惊叹的 Rust 工具,请在评论中告诉我们您的想法。

接下来阅读什么
Sudeshna is from Kolkata and currently working for the Red Hat Middleware team in Pune, India. She loves to explore different open source projects and programs. She started contributing to Open Source a couple of years back as an Hacktober Participant since then she has been an avid blogger at Dzone on Python and Data Science. She enjoys her free time stargazing and loves continental cuisine. She is an RHCSA and is working her way to RHCA.
Sudeshna 来自加尔各答,目前在印度浦那的 Red Hat 中间件团队工作。她喜欢探索不同的开源项目和程序。几年前,她作为 Hacktober 参与者开始为开源做贡献,从那以后,她一直是 Dzone 上关于 Python 和数据科学的活跃博主。她喜欢在空闲时间观星,并且喜欢大陆菜。

评论已关闭。

知识共享许可协议本作品采用知识共享署名-相同方式共享 4.0 国际许可协议授权。
© . All rights reserved.