Zsh Tutorial: find & fd — Search Your Filesystem Fast #21
Video: Zsh Tutorial: find & fd — Search Your Filesystem Fast #21 by Taught by Celeste AI - AI Coding Coach
Watch full page →Zsh Tutorial: find & fd — Search Your Filesystem Fast
Mastering file search in Zsh can greatly speed up your workflow. This guide covers the classic find command with powerful options like name patterns, file types, sizes, modification times, and executing commands on results. It also introduces fd, a modern, faster, and user-friendly alternative with regex support and .gitignore awareness.
Code
# Using find to search by filename (case-sensitive)
find . -name "*.txt"
# Case-insensitive search by filename
find . -iname "*.jpg"
# Find only directories
find . -type d
# Find files larger than 10 kilobytes
find . -type f -size +10k
# Find files modified in the last 7 days
find . -mtime -7
# Combine criteria: find .txt files modified in last 3 days
find . -name "*.txt" -mtime -3
# Execute a command on each found file (print file details)
find . -name "*.log" -exec ls -lh {} \;
# Batch mode execution (faster)
find . -name "*.log" -exec ls -lh {} +
# Safe handling of filenames with spaces using find and xargs
find . -name "*.md" -print0 | xargs -0 cat
# Preview files to be deleted, then delete
find . -name "*.tmp" -print
find . -name "*.tmp" -delete
# Using fd: simpler syntax and faster search
fd "report.*\.pdf$"
# Search only files with .sh extension
fd -e sh
# Search only directories named 'config'
fd -t d config
# Execute a command on each match with fd
fd -e log -x gzip {}
# Exclude node_modules directory
fd --exclude node_modules
# Include files normally ignored by .gitignore
fd --no-ignore
Key Points
findis a versatile tool for searching files by name, type, size, and modification time with complex criteria.- Use
-execwithfindto run commands on each file, choosing between per-file or batch execution. - Combine
findwithxargs -0and-print0for safe handling of filenames containing spaces. fdis a modern alternative tofindthat uses regex by default, respects .gitignore, and offers simpler syntax and faster performance.- Both tools support filtering by extension, type, and excluding directories or files, making file searches efficient and customizable.