fd Linux find命令的强大替代方案

时间:2020-03-05 15:26:42  来源:igfitidea点击:

Linux目录树obeys文件系统层次结构标准,允许Linux用户和开发人员在标准目录中找到系统文件。
由于Linux是一个非常面向文件的操作系统,因此我们知道如何查找文件非常重要。
它存在一些可以帮助工作的可用命令。
在本文中,将介绍如何使用Linux系统上的FD命令找到文件。

什么是fd命令?

FD是Linux Find命令的简单,快速和用户友好的替代品。
它具有着色终端输出的特殊性,可以忽略隐藏的目录/文件。
它还支持正则表达式的使用。

安装FD工具

FD命令需要安装生锈的软件包管理器货物。

安装 rust 包和 cargo命令

所以我们将首先安装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.

最终开始,我们需要在路径环境变量中的货物的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

我们可以搜索以"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

让我们使用-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

我们可以看到对于同一研究,它显示此时间相同的结果加上更多其他信息。
它显示了包含单词书的每个路径的结果

可以使用没有任何参数的命令,它将打印终端每行上的目录中包含的每个文件

# 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命令可以很有用的是找到文件而不是其他命令。