fd-file-finding

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

fd File Finding

fd 文件查找工具

Expert knowledge for using
fd
as a fast, user-friendly alternative to
find
with smart defaults and powerful filtering.
以下是使用
fd
的专业知识,它是
find
的快速、易用替代方案,具备智能默认配置和强大的过滤能力。

Core Expertise

核心优势

fd Advantages
  • Fast parallel execution (written in Rust)
  • Colorized output by default
  • Respects
    .gitignore
    automatically
  • Smart case-insensitive search
  • Simpler syntax than
    find
  • Regular expression support
fd的优点
  • 快速并行执行(基于Rust编写)
  • 默认带彩色输出
  • 自动遵守
    .gitignore
    规则
  • 智能不区分大小写搜索
  • 语法比
    find
    更简单
  • 支持正则表达式

Basic Usage

基础用法

Simple File Search

简单文件搜索

bash
undefined
bash
undefined

Find all files named config

Find all files named config

fd config
fd config

Find files with extension

Find files with extension

fd -e rs # All Rust files fd -e md # All Markdown files fd -e js -e ts # JavaScript and TypeScript
fd -e rs # All Rust files fd -e md # All Markdown files fd -e js -e ts # JavaScript and TypeScript

Case-sensitive search

Case-sensitive search

fd -s Config # Only exact case match
undefined
fd -s Config # Only exact case match
undefined

Pattern Matching

模式匹配

bash
undefined
bash
undefined

Regex patterns

Regex patterns

fd '^test_.*.py$' # Python test files fd '.config$' # Files ending in .config fd '^[A-Z]' # Files starting with uppercase
fd '^test_.*.py$' # Python test files fd '.config$' # Files ending in .config fd '^[A-Z]' # Files starting with uppercase

Glob patterns

Glob patterns

fd '.lua' # All Lua files fd 'test-.js' # test-*.js files
undefined
fd '.lua' # All Lua files fd 'test-.js' # test-*.js files
undefined

Advanced Filtering

高级过滤

Type Filtering

类型过滤

bash
undefined
bash
undefined

Search only 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
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

Multiple types

fd -t f -t l pattern # Files and symlinks
undefined
fd -t f -t l pattern # Files and symlinks
undefined

Depth Control

深度控制

bash
undefined
bash
undefined

Limit search depth

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
fd -d 1 pattern # Only current directory fd -d 3 pattern # Max 3 levels deep fd --max-depth 2 pattern # Alternative syntax

Minimum depth

Minimum depth

fd --min-depth 2 pattern # Skip current directory
undefined
fd --min-depth 2 pattern # Skip current directory
undefined

Hidden and Ignored Files

隐藏文件与被忽略文件

bash
undefined
bash
undefined

Include hidden files

Include hidden files

fd -H pattern # Include hidden files (starting with .)
fd -H pattern # Include hidden files (starting with .)

Include ignored files

Include ignored files

fd -I pattern # Include .gitignore'd files fd -u pattern # Unrestricted: hidden + ignored
fd -I pattern # Include .gitignore'd files fd -u pattern # Unrestricted: hidden + ignored

Show all files

Show all files

fd -H -I pattern # Show everything
undefined
fd -H -I pattern # Show everything
undefined

Size Filtering

大小过滤

bash
undefined
bash
undefined

File size filters

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
undefined
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
undefined

Modification Time

修改时间

bash
undefined
bash
undefined

Files modified recently

Files modified recently

fd --changed-within 1d # Last 24 hours fd --changed-within 2w # Last 2 weeks fd --changed-within 3m # Last 3 months
fd --changed-within 1d # Last 24 hours fd --changed-within 2w # Last 2 weeks fd --changed-within 3m # Last 3 months

Files modified before

Files modified before

fd --changed-before 1y # Older than 1 year
undefined
fd --changed-before 1y # Older than 1 year
undefined

Execution and Processing

执行与处理

Execute Commands

执行命令

bash
undefined
bash
undefined

Execute command for each result

Execute command for each result

fd -e jpg -x convert {} {.}.png # Convert all JPG to PNG
fd -e jpg -x convert {} {.}.png # Convert all JPG to PNG

Parallel execution

Parallel execution

fd -e rs -x rustfmt # Format all Rust files
fd -e rs -x rustfmt # Format all Rust files

Execute with multiple results

Execute with multiple results

fd -e md -X wc -l # Word count on all Markdown files
undefined
fd -e md -X wc -l # Word count on all Markdown files
undefined

Output Formatting

输出格式化

bash
undefined
bash
undefined

Custom output format using placeholders

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
fd -e tf --format '{//}' # Parent directory of each file fd -e rs --format '{/}' # Filename without directory fd -e md --format '{.}' # Path without extension

Placeholders:

Placeholders:

{} - Full path (default)

{} - Full path (default)

{/} - Basename (filename only)

{/} - Basename (filename only)

{//} - Parent directory

{//} - Parent directory

{.} - Path without extension

{.} - Path without extension

{/.} - Basename without extension

{/.} - Basename without extension

undefined
undefined

Integration with Other Tools

与其他工具集成

bash
undefined
bash
undefined

Prefer fd's native execution over xargs when possible:

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)
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

Use with rg for powerful search

fd -e py -x rg "import numpy" {} # Find numpy imports in Python files
fd -e py -x rg "import numpy" {} # Find numpy imports in Python files

Open files in editor

Open files in editor

fd -e md -X nvim # Open all Markdown in Neovim (batch)
fd -e md -X nvim # Open all Markdown in Neovim (batch)

When xargs IS useful: complex pipelines or non-fd inputs

When xargs IS useful: complex pipelines or non-fd inputs

cat filelist.txt | xargs rg "TODO" # Process file from external list
undefined
cat filelist.txt | xargs rg "TODO" # Process file from external list
undefined

Common Patterns

常用场景

Development Workflows

开发工作流

bash
undefined
bash
undefined

Find test files

Find test files

fd -e test.js -e spec.js # JavaScript tests fd '^test_.*.py$' # Python tests fd '_test.go$' # Go tests
fd -e test.js -e spec.js # JavaScript tests fd '^test_.*.py$' # Python tests fd '_test.go$' # Go tests

Find configuration files

Find configuration files

fd -g '.config.js' # Config files fd -g '.env' # Environment files fd -g '*rc' -H # RC files (include hidden)
fd -g '.config.js' # Config files fd -g '.env' # Environment files fd -g '*rc' -H # RC files (include hidden)

Find source files

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/
undefined
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/
undefined

Cleanup Operations

清理操作

bash
undefined
bash
undefined

Find and remove

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
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

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
undefined
fd --size +100m -t f # Files over 100 MB fd --size +1g -t f -x du -h # Size of files over 1 GB
undefined

Path-Based Search

基于路径的搜索

bash
undefined
bash
undefined

Search in specific directories

Search in specific directories

fd pattern src/ # Only in src/ fd pattern src/ tests/ # Multiple directories
fd pattern src/ # Only in src/ fd pattern src/ tests/ # Multiple directories

Exclude paths

Exclude paths

fd -e rs -E target/ # Exclude target directory fd -e js -E node_modules -E dist # Exclude multiple paths
fd -e rs -E target/ # Exclude target directory fd -e js -E node_modules -E dist # Exclude multiple paths

Full path matching

Full path matching

fd -p src/components/.*.tsx$ # Match full path
undefined
fd -p src/components/.*.tsx$ # Match full path
undefined

Find Directories Containing Specific Files

查找包含特定文件的目录

bash
undefined
bash
undefined

Find all directories with Terraform configs

Find all directories with Terraform configs

fd -t f 'main.tf$' --format '{//}'
fd -t f 'main.tf$' --format '{//}'

Find all directories with package.json

Find all directories with package.json

fd -t f '^package.json$' --format '{//}'
fd -t f '^package.json$' --format '{//}'

Find Go module directories

Find Go module directories

fd -t f '^go.mod$' --format '{//}'
fd -t f '^go.mod$' --format '{//}'

Find Python project roots (with pyproject.toml)

Find Python project roots (with pyproject.toml)

fd -t f '^pyproject.toml$' --format '{//}'
fd -t f '^pyproject.toml$' --format '{//}'

Find Cargo.toml directories (Rust projects)

Find Cargo.toml directories (Rust projects)

fd -t f '^Cargo.toml$' --format '{//}'

**Note:** Use `--format '{//}'` instead of piping to xargs - it's faster and simpler.
fd -t f '^Cargo.toml$' --format '{//}'

**注意:** 优先使用`--format '{//}'`而非管道传给xargs,速度更快也更简单。

Best Practices

最佳实践

When to Use fd
  • Finding files by name or pattern
  • Searching with gitignore awareness
  • Fast directory traversal
  • Type-specific searches
  • Time-based file queries
When to Use find Instead
  • Complex boolean logic
  • POSIX compatibility required
  • Advanced permission checks
  • Non-standard file attributes
Performance Tips
  • Use
    -j 1
    for sequential search if order matters
  • Combine with
    --max-depth
    to limit scope
  • Use
    -t f
    to skip directory processing
  • Leverage gitignore for faster searches in repos
Integration with rg
bash
undefined
什么时候使用fd
  • 按名称或模式查找文件
  • 需要识别gitignore规则的搜索
  • 快速目录遍历
  • 按特定类型搜索
  • 基于时间的文件查询
什么时候该用find
  • 需要复杂的布尔逻辑
  • 要求POSIX兼容性
  • 高级权限检查
  • 非标准文件属性查询
性能提示
  • 如果顺序很重要,使用
    -j 1
    进行顺序搜索
  • 结合
    --max-depth
    限制搜索范围
  • 使用
    -t f
    跳过目录处理
  • 在代码仓库中利用gitignore规则加快搜索速度
与rg集成
bash
undefined

Prefer native execution over xargs

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

**Use fd's Built-in Execution**
```bash
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内置执行能力**
```bash

fd can execute directly — no need for xargs

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
undefined
fd -t f 'main.tf$' --format '{//}' # Find dirs containing main.tf fd -e log -x rm # Delete all .log files
undefined

Quick Reference

快速参考

Essential Options

核心选项

OptionPurposeExample
-e EXT
Filter by extension
fd -e rs
-t TYPE
Filter by type (f/d/l/x)
fd -t d
-d DEPTH
Max search depth
fd -d 3
-H
Include hidden files
fd -H .env
-I
Include ignored files
fd -I build
-u
Unrestricted (no ignore)
fd -u pattern
-E PATH
Exclude path
fd -E node_modules
-x CMD
Execute command
fd -e log -x rm
-X CMD
Batch execute
fd -e md -X cat
-s
Case-sensitive
fd -s Config
-g GLOB
Glob pattern
fd -g '*.json'
--format FMT
Custom output format
fd -e tf --format '{//}'
选项用途示例
-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 '{//}'

Time Units

时间单位

  • s
    = seconds
  • m
    = minutes
  • h
    = hours
  • d
    = days
  • w
    = weeks
  • y
    = years
  • s
    = 秒
  • m
    = 分钟
  • h
    = 小时
  • d
    = 天
  • w
    = 周
  • y
    = 年

Size Units

大小单位

  • b
    = bytes
  • k
    = kilobytes
  • m
    = megabytes
  • g
    = gigabytes
  • t
    = terabytes
  • b
    = 字节
  • k
    = 千字节
  • m
    = 兆字节
  • g
    = 吉字节
  • t
    = 太字节

Common Command Patterns

常用命令模式

bash
undefined
bash
undefined

Find recently modified source files

Find recently modified source files

fd -e rs --changed-within 1d
fd -e rs --changed-within 1d

Find large files in current directory

Find large files in current directory

fd -d 1 -t f --size +10m
fd -d 1 -t f --size +10m

Find executable scripts

Find executable scripts

fd -t x -e sh
fd -t x -e sh

Find config files including hidden

Find config files including hidden

fd -H -g 'config'
fd -H -g 'config'

Find and count lines

Find and count lines

fd -e py -X wc -l
fd -e py -X wc -l

Find files excluding build artifacts

Find files excluding build artifacts

fd -e js -E dist -E node_modules -E build
fd -e js -E dist -E node_modules -E build

Find all Terraform/IaC project directories

Find all Terraform/IaC project directories

fd -t f 'main.tf$' --format '{//}'
fd -t f 'main.tf$' --format '{//}'

Find all Node.js project roots

Find all Node.js project roots

fd -t f '^package.json$' --format '{//}'

This makes fd the preferred tool for fast, intuitive file finding in development workflows.
fd -t f '^package.json$' --format '{//}'

这使得fd成为开发工作流中快速、直观查找文件的首选工具。