Vai al contenuto

fd - Alternativa Veloce e Facile da Usare a Find Cheatsheet

Installazione

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

Comandi Base

Comando Descrizione
fd filename Cerca file/directory che corrispondono a "filename"
fd pattern /path Cerca un pattern nella directory specifica
fd -e txt Trova tutti i file con estensione .txt
fd -e js -e ts Trova file con estensioni multiple (.js o .ts)
fd -t f pattern Trova solo file (non directory)
fd -t d dirname Trova solo directory
fd -t x Trova solo file eseguibili
fd -t l Trova solo link simbolici
fd -s Pattern Ricerca sensibile alle maiuscole/minuscole (predefinito è case-insensitive)
fd -d 3 pattern Limita la profondità di ricerca a 3 livelli
fd -H pattern Includi file nascosti nella ricerca
fd -I pattern Non rispettare le regole di .gitignore
fd -a pattern Mostra percorsi assoluti invece di relativi
fd -l pattern Show detailed listing (like ls -l)
fd -0 pattern Use null separator (for piping to xargs -0)

Utilizzo Avanzato

Comando Descrizione
fd '^test.*\.js$' Utilizzare il pattern regex (file che iniziano con "test", che terminano con .js)
fd -g '*.{js,ts}' Utilizzare glob pattern invece di regex
fd -F 'exact.string' Ricerca di stringhe fisse (nessuna interpretazione regex)
fd -E node_modules -E '*.tmp' pattern Escludere specifici pattern dalla ricerca
fd --no-ignore pattern Non utilizzare file .gitignore, .fdignore o .ignore
fd -L pattern Segui i link simbolici durante la ricerca
fd --changed-within 2d File modificati negli ultimi 2 giorni
fd --changed-before 30d File non modificati negli ultimi 30 giorni
fd --size +100m File più grandi di 100MB
fd --size +10m --size -100m File tra 10MB e 100MB
fd --owner username File posseduti da un utente specifico
fd --max-results 100 Limita l'output a 100 risultati
fd -x rm {} Esegui comando su ogni risultato (elimina file)
fd -X convert {} {.}.png Esegui comando con più argomenti (conversione batch)
fd --color never pattern Disabilita output colorato
fd --strip-cwd-prefix pattern Remove ./ prefix from results
fd -H -t d "^\." Trova directory nascoste (che iniziano con un punto)
fd -t f -x du -h {} Mostra le dimensioni dei file per tutte le corrispondenze
fd pattern --threads 1 Utilizzare single thread (per debugging)
fd --base-directory /path pattern Cambia directory base per la ricerca

Configurazione

File di Configurazione

Posizioni del file di ignore globale: - Linux/macOS:~/.config/fd/ignoreo~/.fdignore - Windows:%APPDATA%\fd\ignore

Specifico per il progetto:.fdignorenella root del progetto

Esempio di File .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/

Variabili di Ambiente

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

Alias Utili

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

Casi d'Uso Comuni

Caso d'Uso 1: Pulizia Artefatti di Build

# 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 {}

Caso d'Uso 2: Ricerca e Analisi Codice

# 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" {}

Caso d'Uso 3: Gestione File di Log

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

Caso d'Uso 4: Gestione Docker e Container

# 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:" {} \;

Caso d'Uso 5: Audit di Sicurezza e Permessi

# 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

Migliori Pratiche

  • Utilizzare file .fdignore: Creare pattern di ignore specifici per il progetto per velocizzare le ricerche e ridurre il rumore
  • Sfruttare i valori predefiniti intelligenti:fdrispetta automaticamente.gitignore- usa-Isolo quando hai bisogno di file ignorati
  • Combina con altri strumenti: Inviafdl'output axargs,grep,fzfper workflow potenti
  • Usa-0conxargs: Usa semprefd -0 | xargs -0per nomi di file con spazi o caratteri speciali
  • Preferisci-xinvece di pipe: Usafd -x command {}invece di inviare axargsper migliori prestazioni
  • Imposta limiti di profondità: Usa-dflag su grandi alberi di directory per prevenire ricorsioni eccessive
  • Usa filtri specifici per tipo: Filtra per tipo (-t f,-t d) presto per ridurre lo spazio di ricerca
  • Crea alias: Imposta alias di shell per modellifdfrequentemente usati per risparmiare digitazione
  • Combina filtri di tempo e dimensione: Usa--changed-withine--sizeinsieme per un targeting preciso dei file
  • Testa pattern regex: Usa tester regex online prima difdricerche complesse per verificare i pattern

Risoluzione dei problemi

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