fd - 高速でユーザーフレンドリーな検索代替ツール チートシート
fd - 高速でユーザーフレンドリーな検索代替ツール チートシート
インストール
| プラットフォーム | コマンド |
|---|---|
| Ubuntu/Debian | sudo apt install fd-find (binary: fdfind) |
| Arch Linux | sudo pacman -S fd |
| Fedora/RHEL | sudo dnf install fd-find |
| Alpine Linux | apk add fd |
| macOS (Homebrew) | brew install fd |
| macOS (MacPorts) | sudo port install fd |
| Windows (Scoop) | scoop install fd |
| Windows (Chocolatey) | choco install fd |
| Windows (Winget) | winget install sharkdp.fd |
| Cargo (All platforms) | cargo install fd-find |
| Snap | sudo snap install fd |
基本コマンド
| コマンド | 説明 |
|---|---|
fd filename | ”filename”に一致するファイル/ディレクトリを検索 |
fd pattern /path | 特定のディレクトリでパターンを検索 |
fd -e txt | .txt 拡張子を持つすべてのファイルを見つける |
fd -e js -e ts | 複数の拡張子(.js または .ts)を持つファイルを見つける |
fd -t f pattern | ファイル(ディレクトリではない)のみを見つける |
fd -t d dirname | ディレクトリのみを見つける |
fd -t x | 実行可能なファイルのみを見つける |
fd -t l | シンボリックリンクのみを見つける |
fd -s Pattern | 大文字と小文字を区別した検索(デフォルトは大文字と小文字を区別しない) |
fd -d 3 pattern | 検索深度を3レベルに制限する |
fd -H pattern | 検索に隠しファイルを含める |
fd -I pattern | .gitignore のルールを無視しない |
fd -a pattern | 相対パスではなく絶対パスを表示 |
fd -l pattern | Show detailed listing (like ls -l) |
fd -0 pattern | Use null separator (for piping to xargs -0) |
高度な使用法
| コマンド | 説明 |
|---|---|
fd '^test.*\.js$' | 正規表現パターン(“test”で始まり、.jsで終わるファイル) |
fd -g '*.{js,ts}' | glob パターンを regex の代わりに使用する |
fd -F 'exact.string' | 固定文字列検索(正規表現の解釈なし) |
fd -E node_modules -E '*.tmp' pattern | 検索から特定のパターンを除外 |
fd --no-ignore pattern | .gitignore、.fdignore、または .ignore ファイルを使用しないでください |
fd -L pattern | 検索中にシンボリックリンクを辿る |
fd --changed-within 2d | 過去2日以内に変更されたファイル |
fd --changed-before 30d | 過去30日間に変更されていないファイル |
fd --size +100m | 100MB以上のファイル |
fd --size +10m --size -100m | 10MB から 100MB のファイル |
fd --owner username | 特定のユーザーが所有するファイル |
fd --max-results 100 | 出力を100結果に制限する |
fd -x rm {} | 各結果でコマンドを実行(ファイルを削除) |
fd -X convert {} {.}.png | 複数の引数を使用してコマンドを実行(一括変換) |
fd --color never pattern | カラー出力を無効にする |
fd --strip-cwd-prefix pattern | Remove ./ prefix from results |
fd -H -t d "^\." | ドット(.)で始まる隠しディレクトリを見つける |
fd -t f -x du -h {} | すべての一致に対してファイルサイズを表示 |
fd pattern --threads 1 | シングルスレッド(デバッグ用)を使用 |
fd --base-directory /path pattern | 検索のベースディレクトリを変更 |
設定
設定ファイル
グローバル無視ファイルの場所:
- Linux/macOS:
~/.config/fd/ignoreまたは~/.fdignore - Windows:
%APPDATA%\fd\ignore
プロジェクト固有: .fdignoreプロジェクトルートに
.fdignore ファイルの例
# Ignore dependency directories
node_modules/
vendor/
target/
# Ignore build outputs
build/
dist/
*.o
*.pyc
# Ignore IDE directories
.idea/
.vscode/
*.swp
# Ignore log files
*.log
logs/
# Ignore temporary files
*.tmp
*~
.DS_Store
# Ignore specific paths
/cache/
/tmp/
環境変数
# Set default options for all fd commands
export FD_OPTS="--hidden --follow --exclude .git"
# Control colorization (fd respects LS_COLORS)
export LS_COLORS="di=34:ln=35:ex=31:*.txt=33"
便利なエイリアス
# Add to ~/.bashrc or ~/.zshrc
# Search including hidden files
alias fdh='fd -H'
# Search ignoring .gitignore
alias fda='fd -I'
# Find large files
alias fdl='fd --size +100m'
# Find recent files (last 24 hours)
alias fdr='fd --changed-within 1d'
# Find and preview with fzf and bat
alias fdf='fd -t f | fzf --preview "bat --color=always {}"'
# Find and edit with vim
alias fdv='fd -t f | fzf | xargs -r vim'
一般的なユースケース
ユースケース 1: ビルド成果物のクリーニング
# Remove all Python compiled files
fd -e pyc -e pyo -x rm {}
# Remove all node_modules directories (up to 2 levels deep)
fd -t d -d 2 '^node_modules$' -x rm -rf {}
# Clean old log files (older than 30 days)
fd -e log --changed-before 30d -x rm {}
# Remove all temporary files
fd -g '*.tmp' -g '*~' -g '*.swp' -x rm {}
ユースケース 2: コード検索と分析
# Find all JavaScript/TypeScript files modified in last week
fd -e js -e ts -e jsx -e tsx --changed-within 7d
# Count lines of code in Rust project
fd -e rs -x wc -l {} | awk '{sum+=$1} END {print sum}'
# Find all TODO comments in Python files
fd -e py -x grep -Hn "TODO:" {}
# Find largest source files
fd -e java -e kt -x du -h {} | sort -hr | head -20
# Find files containing specific function name
fd -e cpp -e hpp -x grep -l "myFunction" {}
ユースケース 3: ログファイル管理
# Find and compress logs older than 7 days
fd -e log --changed-before 7d -x gzip {}
# Search for errors in recent logs
fd -e log --changed-within 1d -x grep -i "error" {} \;
# Find large log files (over 100MB)
fd -e log --size +100m -l
# Archive old logs to separate directory
fd -e log --changed-before 30d -x mv {} /archive/logs/
ユースケース 4: DockerとコンテナManagement
# Find all Dockerfiles in project
fd -g '*Dockerfile*' -g 'Dockerfile.*'
# Find all docker-compose files
fd -g 'docker-compose*.yml' -g 'docker-compose*.yaml'
# Find and validate Dockerfiles
fd '^Dockerfile$' -x docker build --dry-run -f {} .
# Find container-related configs
fd -e yml -e yaml -x grep -l "kind:" {} \;
ユースケース 5: セキュリティと権限の監査
# Find world-writable files (security risk)
fd -t f -x sh -c 'test -w {} && ls -l {}'
# Find SUID/SGID binaries
fd -t x -x sh -c 'test -u {} -o -g {} && ls -l {}'
# Find files with passwords in name
fd -i password -i passwd -i secret
# Find configuration files with potential secrets
fd -g '*.conf' -g '*.cfg' -g '*.ini' -x grep -i "password\|secret\|key" {}
# Find SSH keys
fd -g 'id_rsa*' -g 'id_ed25519*' -H
ベストプラクティス
- 使用
.fdignoreファイル: プロジェクト固有の無視パターンを作成して、検索を高速化し、ノイズを削減 - スマートなデフォルトを活用:
fd自動的に尊重
Note: Some sections were left blank as no specific text was provided to translate. I’ve maintained the structure and added Japanese translations where text was present..gitignore- 無視するファイルが必要な場合にのみ使用-I- 他のツールと組み合わせる: パイプfd出力をxargs、grep、またはfzfに渡して強力なワークフローを作成
- で
-0と一緒に使用: スペースや特殊文字を含むファイル名には常にxargsを使用 - を
fd -0 | xargs -0よりも優先**: パイプする代わりに-xを使用fd -x command {}するとパフォーマンスが向上 - 深さの制限を設定: 過度な再帰を防ぐために大きなディレクトリツリーで
xargsフラグを使用 - 特定の型フィルターを使用: 検索範囲を減らすために早期に型(
-d、-t f)でフィルタリング - エイリアスを作成: 頻繁に使用する
-t dパターンのシェルエイリアスを設定してタイピングを省略 - 時間とサイズのフィルターを組み合わせる: ファイルを正確にターゲットにするために
fdと--changed-withinを一緒に使用 - 正規表現パターンをテスト: 複雑な
--size検索の前にオンライン正規表現テスターでパターンを確認
トラブルシューティング
fd| 期待されるファイルが見つからない | ファイルがフィルター(拡張子、型、サイズ、時間)に一致するか確認。
| 問題 | ソリューション |
|---|---|
Command not found: fd | On Debian/Ubuntu, binary is fdfind. Create symlink: ln -s $(which fdfind) ~/.local/bin/fd |
| Too many results | Use depth limit -d 3, exclude patterns -E node_modules, or filter by type -t f |
| Not finding hidden files | Add -H flag to include hidden files, or -HI to also ignore .gitignore rules |
| Search is too slow | Reduce depth with -d, exclude large directories with -E, or use --threads to control parallelism |
| Regex not matching | Remember fd uses case-insensitive search by default. Use -s for case-sensitive or -F for literal strings |
| Files in .gitignore showing up | This shouldn’t happen by default. Check for -I or --no-ignore flags in aliases or FD_OPTS |
| Special characters in filenames breaking commands | Use -0 with xargs -0 or use -x flag instead: fd pattern -x command {} |
| Cannot execute command on results | Check if using correct syntax: -x for individual execution, -X for batch execution |
| Color output in logs/files | Disable with --color never or redirect stderr: fd pattern 2>/dev/null |
| Permission denied errors | Redirect stderr to ignore: fd pattern 2>/dev/null or run with appropriate permissions |
| Not respecting .fdignore file | Ensure .fdignore is in project root or ~/.config/fd/ignore. Check file syntax matches .gitignore format |