关于 fd-file-finding
fd-file-finding 是面向开发者的 fd 命令参考技能,用于按名称、路径或属性查找文件。它介绍忽略规则、类型与时间筛选,以及将搜索结果直接交给命令处理。
主要功能
- 01按名称、扩展名和模式查找
- 02按类型、深度和路径筛选
- 03包含隐藏或忽略文件
- 04按大小和修改时间筛选
- 05对结果执行单项或批量命令
适用场景
- 01定位配置文件或测试文件
- 02查找近期修改或体积较大的文件
- 03将匹配文件交给格式化或搜索命令
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ laurigates/ claude-plugins --skill fd-file-finding安装后在 Claude Code 中输入/fd-file-finding确认已加载。
详细介绍
fd 文件查找
掌握使用 fd 的专业知识:将其作为 find 的快速、易用替代工具,享受智能默认设置和强大的筛选功能。
何时使用此技能
| 在以下情况下使用此技能…… | 在以下情况下改用 rg-code-search…… |
|---|---|
| 按名称、扩展名或路径模式查找文件 | 在文件内容中搜索文本或正则表达式 |
按修改时间(--changed-within)或大小(--size)筛选 | 按文件类型(-t py、-t js)筛选匹配项 |
查找要传入 -x / -X 执行的文件 | 审查多个文件中的源代码模式 |
| 在以下情况下使用此技能…… | 在以下情况下改用 jq-json-processing…… |
|---|---|
| 查找磁盘上的 JSON、YAML 或其他文件 | 查询或转换这些文件的内容 |
| 为后续批处理构建文件列表 | 提取字段、筛选数组或重塑 JSON |
核心专长
fd 的优势
- 快速并行执行(以 Rust 编写)
- 默认输出彩色内容
- 自动遵循
.gitignore - 智能、不区分大小写的搜索
- 语法比
find更简单 - 支持正则表达式
基本用法
简单文件搜索
# Find all files named config
fd config
# Find files with extension
fd -e rs # All Rust files
fd -e md # All Markdown files
fd -e js -e ts # JavaScript and TypeScript
# Case-sensitive search
fd -s Config # Only exact case match
模式匹配
# Regex patterns
fd '^test_.*\.py$' # Python test files
fd '\.config$' # Files ending in .config
fd '^[A-Z]' # Files starting with uppercase
# Glob patterns
fd '*.lua' # All Lua files
fd 'test-*.js' # test-*.js files
高级筛选
按类型筛选
# Search only files
fd -t f pattern # Files only
fd -t d pattern # Directories only
fd -t l pattern # Symlinks only
fd -t x pattern # Executable files
# Multiple types
fd -t f -t l pattern # Files and symlinks
深度控制
# Limit search depth
fd -d 1 pattern # Only current directory
fd -d 3 pattern # Max 3 levels deep
fd --max-depth 2 pattern # Alternative syntax
# Minimum depth
fd --min-depth 2 pattern # Skip current directory
隐藏文件和忽略文件
# Include hidden files
fd -H pattern # Include hidden files (starting with .)
# Include ignored files
fd -I pattern # Include .gitignore'd files
fd -u pattern # Unrestricted: hidden + ignored
# Show all files
fd -H -I pattern # Show everything
按大小筛选
# File size filters
fd --size +10m # Files larger than 10 MB
fd --size -1k # Files smaller than 1 KB
fd --size +100k --size -10m # Between 100 KB and 10 MB
修改时间
# Files modified recently
fd --changed-within 1d # Last 24 hours
fd --changed-within 2w # Last 2 weeks
fd --changed-within 3m # Last 3 months
# Files modified before
fd --changed-before 1y # Older than 1 year
执行和处理
执行命令
# Execute command for each result
fd -e jpg -x convert {} {.}.png # Convert all JPG to PNG
# Parallel execution
fd -e rs -x rustfmt # Format all Rust files
# Execute with multiple results
fd -e md -X wc -l # Word count on all Markdown files
输出格式
# Custom output format using placeholders
fd -e tf --format '{//}' # Parent directory of each file
fd -e rs --format '{/}' # Filename without directory
fd -e md --format '{.}' # Path without extension
# Placeholders:
# {} - Full path (default)
# {/} - Basename (filename only)
# {//} - Parent directory
# {.} - Path without extension
# {/.} - Basename without extension
与其他工具集成
# Prefer fd's native execution over xargs when possible:
fd -e log -x rm # Delete all log files (native)
fd -e rs -X wc -l # Count lines in Rust files (batch)
# Use with rg for powerful search
fd -e py -x rg "import numpy" {} # Find numpy imports in Python files
# Open files in editor
fd -e md -X nvim # Open all Markdown in Neovim (batch)
# When xargs IS useful: complex pipelines or non-fd inputs
cat filelist.txt | xargs rg "TODO" # Process file from external list
常见模式
开发工作流
# Find test files
fd -e test.js -e spec.js # JavaScript tests
fd '^test_.*\.py$' # Python tests
fd '_test\.go$' # Go tests
# Find configuration files
fd -g '*.config.js' # Config files
fd -g '.env*' # Environment files
fd -g '*rc' -H # RC files (include hidden)
# Find source files
fd -e rs -e toml -t f # Rust project files
fd -e py --exclude __pycache__ # Python excluding cache
fd -e ts -e tsx src/ # TypeScript in src/
清理操作
# Find and remove
fd -e pyc -x rm # Remove Python bytecode
fd node_modules -t d -x rm -rf # Remove node_modules
fd -g '*.log' --changed-before 30d -X rm # Remove old logs
# Find large files
fd --size +100m -t f # Files over 100 MB
fd --size +1g -t f -x du -h # Size of files over 1 GB
基于路径的搜索
# Search in specific directories
fd pattern src/ # Only in src/
fd pattern src/ tests/ # Multiple directories
# Exclude paths
fd -e rs -E target/ # Exclude target directory
fd -e js -E node_modules -E dist # Exclude multiple paths
# Full path matching
fd -p src/components/.*\.tsx$ # Match full path
查找包含特定文件的目录
# Find all directories with Terraform configs
fd -t f 'main\.tf$' --format '{//}'
# Find all directories with package.json
fd -t f '^package\.json$' --format '{//}'
# Find Go module directories
fd -t f '^go\.mod$' --format '{//}'
# Find Python project roots (with pyproject.toml)
fd -t f '^pyproject\.toml$' --format '{//}'
# Find Cargo.toml directories (Rust projects)
fd -t f '^Cargo\.toml$' --format '{//}'
注意: 使用 --format '{//}',而不是通过管道传给 xargs,这样更快、更简单。
最佳实践
何时使用 fd
- 按名称或模式查找文件
- 搜索时遵循 gitignore 规则
- 快速遍历目录
- 按类型搜索
- 按时间查询文件
何时改用 find
- 复杂的布尔逻辑
- 需要 POSIX 兼容性
- 高级权限检查
- 非标准文件属性
性能提示
- 如果顺序很重要,使用
-j 1进行顺序搜索 - 配合
--max-depth限制范围 - 使用
-t f跳过目录处理 - 利用 gitignore 加快在仓库中的搜索
与 rg 集成
# Prefer native execution over xargs
fd -e py -x rg "class.*Test" {} # Find test classes in Python
fd -e rs -x rg "TODO" {} # Find TODOs in Rust files
fd -e md -x rg "# " {} # Find headers in Markdown
使用 fd 的内置执行功能
# fd can execute directly — no need for xargs
fd -t f 'main\.tf$' --format '{//}' # Find dirs containing main.tf
fd -e log -x rm # Delete all .log files
快速参考
基本选项
| 选项 | 用途 | 示例 |
|---|---|---|
-e EXT | 按扩展名筛选 | fd -e rs |
-t TYPE | 按类型筛选(f/d/l/x) | fd -t d |
-d DEPTH | 最大搜索深度 | fd -d 3 |
-H | 包含隐藏文件 | fd -H .env |
-I | 包含忽略文件 | fd -I build |
-u | 不受限制(不忽略) | fd -u pattern |
-E PATH | 排除路径 | fd -E node_modules |
-x CMD | 执行命令 | fd -e log -x rm |
-X CMD | 批量执行 | fd -e md -X cat |
-s | 区分大小写 | fd -s Config |
-g GLOB | Glob 模式 | fd -g '*.json' |
--format FMT | 自定义输出格式 | fd -e tf --format '{//}' |
时间单位
s= 秒m= 分钟h= 小时d= 天w= 周y= 年
大小单位
b= 字节k= 千字节m= 兆字节g= 吉字节t= 太字节
常见命令模式
# Find recently modified source files
fd -e rs --changed-within 1d
# Find large files in current directory
fd -d 1 -t f --size +10m
# Find executable scripts
fd -t x -e sh
# Find config files including hidden
fd -H -g '*config*'
# Find and count lines
fd -e py -X wc -l
# Find files excluding build artifacts
fd -e js -E dist -E node_modules -E build
# Find all Terraform/IaC project directories
fd -t f 'main\.tf$' --format '{//}'
# Find all Node.js project roots
fd -t f '^package\.json$' --format '{//}'
因此,fd 是开发工作流中快速、直观地查找文件的首选工具。
评论
支持 Markdown