fd – Linux Find 命令的強大替代品

Linux 目錄樹遵循 Filesystem Hierarchy Standard,它允許 Linux 用戶和開發人員在標準目錄中定位系統文件。 由於 Linux 是一個非常面向文件的操作系統,因此了解如何查找文件非常重要。 它存在一些可以幫助工作的可用命令。 在本文中,我將向您展示如何使用 fd 命令 在您的 Linux 系統上。

fd 命令是什麼?

fd 是 Linux find 命令的簡單、快速且用戶友好的替代方案。 它具有為終端輸出著色的特殊性,並且可以忽略隱藏的目錄/文件。 它還支持使用正則表達式。

安裝 fd 工具

fd 命令需要 Rust 的包管理器 貨物 要安裝。

安裝 rust 包和貨物命令

所以我們先安裝rustc和cargo

# curl https://sh.rustup.rs -sSf | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust programming 
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin 
directory, located at:

  /root/.cargo/bin

This path will then be added to your PATH environment variable by modifying the
profile files located at:

  /root/.profile
  /root/.bash_profile

You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

首先,您需要在 PATH 環境變量中包含 Cargo 的 bin 目錄 ($HOME/.cargo/bin)。 您可以通過運行在當前的 shell 中配置它

# source $HOME/.cargo/env

您可以檢查如下

# echo $PATH
/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

現在我們可以安裝 fd

安裝 fd 命令

現在已經安裝了貨物,我們可以安裝 fd 命令

# cargo install fd-find
 Updating registry `https://github.com/rust-lang/crates.io-index`
 Downloading fd-find v3.0.0
 Installing fd-find v3.0.0
 Downloading ignore v0.2.2
..........................
..........................
Compiling fd-find v3.0.0
 Finished release [optimized] target(s) in 186.48 secs
 Installing /root/.cargo/bin/fd

如何使用 fd 命令查找文件

在使用 fd 命令之前,我們需要了解它是如何工作的,這可以通過閱讀命令的幫助來實現

# fd --help
fd 3.0.0

USAGE:
    fd [FLAGS/OPTIONS] [<pattern>] [<path>]

FLAGS:
    -s, --case-sensitive    Case-sensitive search (default: smart case)
    -p, --full-path         Search full path (default: file-/dirname only)
    -H, --hidden            Search hidden files and directories
    -I, --no-ignore         Do not respect .(git)ignore files
    -L, --follow            Follow symlinks
    -0, --print0            Separate results by the null character
    -a, --absolute-path     Show absolute instead of relative paths
    -n, --no-color          Do not colorize output
    -h, --help              Prints help information
    -V, --version           Prints version information

OPTIONS:
    -d, --max-depth     Set maximum search depth (default: none)
    -j, --threads     The number of threads used for searching

ARGS:
        the search pattern, a regular expression (optional)
           the root directory for the filesystem search (optional)

現在我們已經了解了命令的概念,我們可以嘗試查找如下文件

# fd book
cargo/src/doc/book
cargo/src/doc/book/book.toml

我們可以搜索以 example 用“jpeg”

# fd jpeg$
Pictures/centos-logo.png.jpeg
Pictures/redhat.jpeg
Pictures/mozilla.jpeg

或找到以 bbb 開頭的文件

# fd ^bbb Pictures
Pictures/BigBluebutton/bbb-check-final-config.jpg
Pictures/BigBluebutton/bbb-check-final-config.png
Pictures/BigBluebutton/bbb-check.png
Pictures/BigBluebutton/bbb-conf-check.jpg

我們可以包括隱藏文件夾進行研究

# fd -H config
config
.gitconfig
.config
.ssh/config

讓我們先 example 帶-p參數查看結果

# fd -p book
cargo/src/doc/book
cargo/src/doc/book/README.md
cargo/src/doc/book/book.toml
cargo/src/doc/book/src
......................
......................
cargo/src/doc/book/src/reference/specifying-dependencies.md
cargo/src/doc/book/theme
cargo/src/doc/book/theme/favicon.png

您可以看到,對於相同的研究,它這次顯示相同的結果以及更多其他信息。 它顯示了包含單詞 book 的每個路徑的結果

可以使用不帶任何參數的命令,它將打印終端每一行目錄中包含的每個文件

# fd
gifcurry-linux-2.1.0.0.tar.gz
funny-cat.mkv
plage
jumpfm
labor
gifcurry-linux-2.1.0.0
duplicate
...................
...................
electron-quick-start/main.js
electron-quick-start/renderer.js
electron-quick-start/index.html
electron-quick-start/README.md
electron-quick-start/package.json
electron-quick-start/LICENSE.md
jumpfm/build/icons
jumpfm/build/superhero.png.ico
jumpfm/build/superhero.png.icns
jumpfm/ts/files.ts
jumpfm/fonts/RobotoMono-Regular.ttf
labor/package/pack6.txt
gifcurry-linux-2.1.0.0/packages/00-index.tar
gifcurry-linux-2.1.0.0/packages/00-index.cache

fd 命令比其他命令更有助於查找文件。 著色結果對於查看結果非常有用,並且可能出於某種原因停用結果著色。