Github每日精选(第36期):find简单、快速和用户友好的替代工具fd

fd

fd是一个在文件系统中查找条目的程序。 虽然它的目标不是支持所有find的强大功能,但它为大多数用例提供了合理的(自以为是的)默认值。

安装

ubuntu下:

如果您运行 Ubuntu 19.04 (Disco Dingo) 或更新版本,您可以安装 官方维护的软件包:

sudo apt install fd-find

请注意,调用二进制文件是fdfind因为二进制名称fd已被另一个包使用。建议安装后fd通过执行 command 添加一个链接ln -s $(which fdfind) ~/.local/bin/fd,以便和fd本文档一样使用。确保它$HOME/.local/bin在您的$PATH.

如果您使用旧版本的 Ubuntu,您可以从发布页面.deb下载最新的软件包 并通过以下方式安装:

sudo dpkg -i fd_8.4.0_amd64.deb  # adapt version number and architecture

命令行选项

这是fd -h 的输出。要查看完整的命令行选项集,请使用fd --help其中还包括更详细的帮助文本。

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

FLAGS:
    -H, --hidden            Search hidden files and directories
    -I, --no-ignore         Do not respect .(git|fd)ignore files
    -s, --case-sensitive    Case-sensitive search (default: smart case)
    -i, --ignore-case       Case-insensitive search (default: smart case)
    -g, --glob              Glob-based search (default: regular expression)
    -a, --absolute-path     Show absolute instead of relative paths
    -l, --list-details      Use a long listing format with file metadata
    -L, --follow            Follow symbolic links
    -p, --full-path         Search full abs. path (default: filename only)
    -h, --help              Prints help information
    -V, --version           Prints version information

OPTIONS:
    -d, --max-depth <depth>            Set maximum search depth (default: none)
    -t, --type <filetype>...           Filter by type: file (f), directory (d), symlink (l),
                                       executable (x), empty (e), socket (s), pipe (p)
    -e, --extension <ext>...           Filter by file extension
    -x, --exec <cmd>                   Execute a command for each search result
    -X, --exec-batch <cmd>             Execute a command with all search results at once
    -E, --exclude <pattern>...         Exclude entries that match the given glob pattern
    -c, --color <when>                 When to use colors: never, *auto*, always
    -S, --size <size>...               Limit results based on the size of files
        --changed-within <date|dur>    Filter by file modification time (newer than)
        --changed-before <date|dur>    Filter by file modification time (older than)
    -o, --owner <user:group>           Filter by owning user and/or group

ARGS:
    <pattern>    the search pattern (a regular expression, unless '--glob' is used; optional)
    <path>...    the root directory for the filesystem search (optional)

用法

  • 简单搜索

fd旨在查找文件系统中的条目。您可以执行的最基本搜索是使用单个参数运行fd :搜索模式。例如,假设您要查找您的旧脚本(包括名称netflix):

> fd netfl
Software/python/imdb-ratings/netflix-details.py

如果只使用这样的单个参数调用,fd会递归搜索当前目录以查找包含pattern的任何条目netfl。

  • 正则表达式搜索

搜索模式被视为正则表达式。在这里,我们搜索以 开头x和结尾的条目rc:

> cd /etc
> fd '^x.*rc$'
X11/xinit/xinitrc
X11/xinit/xserverrc

fd使用的正则表达式语法在此处记录。

  • 指定根目录

如果我们想搜索一个特定的目录,它可以作为fd的第二个参数给出:

> fd passwd /etc
/etc/default/passwd
/etc/pam.d/passwd
/etc/passwd
  • 递归列出所有文件

fd可以不带参数调用。这对于以递归方式快速了解当前目录中的所有条目非常有用(类似于ls -R):

> cd fd/tests
> fd
testenv
testenv/mod.rs
tests.rs

如果要使用此功能列出给定目录中的所有文件,则必须使用包罗万象的模式,例如.or ^:

> fd . fd/tests/
testenv
testenv/mod.rs
tests.rs
  • 搜索特定的文件扩展名

通常,我们对特定类型的所有文件感兴趣。这可以通过-e(or --extension) 选项来完成。在这里,我们搜索 fd 存储库中的所有 Markdown 文件:

> cd fd
> fd -e md
CONTRIBUTING.md
README.md

该-e选项可以与搜索模式结合使用:

> fd -e rs mod
src/fshelper/mod.rs
src/lscolors/mod.rs
tests/testenv/mod.rs
  • 搜索特定文件名

要查找具有完全提供的搜索模式的文件,请使用-g(or --glob) 选项:

> fd -g libc.so /usr
/usr/lib32/libc.so
/usr/lib/libc.so
  • 隐藏和忽略的文件

默认情况下,fd不会搜索隐藏目录,也不会在搜索结果中显示隐藏文件。要禁用此行为,我们可以使用-H(or --hidden) 选项:

> fd pre-commit
> fd -H pre-commit
.git/hooks/pre-commit.sample

如果我们在作为 Git 存储库(或包含 Git 存储库)的目录中工作,fd.gitignore不会搜索与其中一种模式匹配的文件夹(并且不会显示文件) 。要禁用此行为,我们可以使用-I(or --no-ignore) 选项:

> fd num_cpu
> fd -I num_cpu
target/debug/deps/libnum_cpus-f5ce7ef99006aa05.rlib

要真正搜索所有文件和目录,只需结合隐藏和忽略功能即可显示所有内容 ( -HI)。

  • 匹配完整路径

默认情况下,fd只匹配每个文件的文件名。但是,使用–full-pathor-p选项,您可以匹配完整路径。

> fd -p -g '**/.git/config'
> fd -p '.*/lesson-\d+/[a-z]+.(jpg|png)'

你可能感兴趣的:(Github每日精选,github,ubuntu,服务器,文件查找)