fd
fd(find entries in the filesystem) 是一个用于在命令行中快速查找文件和目录的工具, 而不需要使用复杂的正则表达式。它类似于find命令, 但具有更简洁的语法和更友好的用户界面。 可以与 shell 结合使用, 通过在命令行中指定搜索模式和目录路径, 来查找匹配的文件和目录。它支持递归搜索, 并提供一些有用的选项来过滤和定制搜索结果
# base
fd [选项] 搜索模式 [目录]
1
其中, 搜索模式 是要查找的文件或目录的名称模式。目录 是要在其中进行搜索的起始目录路径。如果未指定目录路径, 则默认从当前目录开始递归搜索
# common options
man fd, fd --help: help-i: case-sensitive-a: 搜索隐藏文件和目录-t <类型>: 只搜索指定类型的文件或目录(例如, -t f 用于只搜索文件)-e <扩展名>: 只搜索具有指定扩展名的文件-x <文件系统>: 排除指定文件系统的搜索结果-s: 按文件大小进行排序-0, --print0: Separate search results by the null character (instead of newlines).-d, --max-depth: Limit directory traversal to at most d levels of depth. By default, there is no limit on the search depth-p, --full-path: By default, the search pattern is only matched against the filename (or directory name). Using this flag, the pattern is matched against the full path-E, --exclude: Exclude files/directories that match the given glob pattern
# case
fd -e log . ../: search for all files inside the '../' directory, use a match-all patternfd . '../'fd -e apk -0 . ../ | xargs -0 ls -lt | head -n 3: apk 倒叙,仅显示 3 个
fd -e txt --exclude "*.log": 这个命令会查找所有 .txt 文件,但排除所有以 .log 结尾的文件fd --exclude "node_modules": 排除某个特定目录下的文件
# git 相关
fd -t d -H .git: 查找当前目录下所有包含.git文件夹的目录(即 git 管理的项目)fd -t d -H .git -d 1: 只查找当前目录下一级子目录中的 git 项目fd -t d -H .git --max-depth 2: 查找当前目录下最多2层深度的 git 项目fd -t d -H .git -x basename: 只显示 git 项目的文件夹名称(不显示路径)fd -t d -H .git -d 2 --exclude "*gitlab*": 查找 git 项目但排除包含 gitlab 的目录fd -t d -H .git -d 2 --exclude "*gitlab*" | sed 's|/.git/$||': 提取所有 git 项目的名称(去掉路径和 .git 后缀)
上次更新: 2025/07/21, 21:50:24