find Command Guide
The find command is one of the most versatile tools in Linux for locating files and directories. Learn how to search your filesystem efficiently with practical examples.
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
find . -name "*.txt"Find by name in current dirfind /path -type fFind files in pathfind /path -type dFind directories in pathBy Name
-name "pattern"Case-sensitive name match-iname "pattern"Case-insensitive match-name "*.js"Wildcard patternBy Size
-size +100MLarger than 100MB-size -1kSmaller than 1KB-emptyEmpty files/directoriesBy Time
-mtime -7Modified in last 7 days-mtime +30Modified over 30 days ago-mmin -60Modified in last 60 minBy Type
-type fRegular files-type dDirectories-type lSymbolic linksActions
-exec cmd {} \;Execute command on each-deleteDelete matched files-printPrint pathname (default)Downloadable Image Preview
Basic Usage
The find command searches for files in a directory hierarchy. The basic syntax is straightforward: specify where to search and what to look for.
find /path/to/search -name "filename"If you omit the path, find will search in the current directory.
# Search in current directory
find . -name "*.txt"Searching by Name
The most common use case is searching for files by name. You can use wildcards and case-insensitive searches.
Exact name match
find /home -name "config.json"Case-insensitive search (-iname)
find . -iname "readme.md"Wildcard patterns
# Find all JavaScript files
find ./src -name "*.js"
# Find files starting with "test"
find . -name "test*"
# Find files with specific pattern
find . -name "*config*.json""*.txt".Searching by Type
You can filter results to show only files, directories, or other types.
# Find only files
find . -type f -name "*.log"
# Find only directories
find . -type d -name "node_modules"
# Find symbolic links
find . -type lCommon Type Options
| -type f | Regular file |
| -type d | Directory |
| -type l | Symbolic link |
Searching by Size
Find files based on their size using the -size option.
# Find files larger than 100MB
find . -size +100M
# Find files smaller than 1KB
find . -size -1k
# Find files exactly 50 bytes
find . -size 50c
# Find empty files
find . -type f -emptySize Units
| c | Bytes |
| k | Kilobytes |
| M | Megabytes |
| G | Gigabytes |
Searching by Time
Find files based on when they were modified, accessed, or created.
# Modified in the last 7 days
find . -mtime -7
# Modified more than 30 days ago
find . -mtime +30
# Modified in the last 60 minutes
find . -mmin -60
# Accessed in the last 24 hours
find . -atime -1-mtime uses days, while -mmin uses minutes. The same applies to -atime/-amin (access time) and -ctime/-cmin (change time).Executing Commands
One of find's most powerful features is the ability to execute commands on the files it finds.
Using -exec
# Delete all .tmp files
find . -name "*.tmp" -exec rm {} \;
# Change permissions on all shell scripts
find . -name "*.sh" -exec chmod +x {} \;
# View contents of all found files
find . -name "*.log" -exec cat {} \;Using -exec with confirmation
# Ask before deleting each file
find . -name "*.bak" -exec rm -i {} \;
# Or use -ok for built-in confirmation
find . -name "*.bak" -ok rm {} \;-exec rm. Always test your find command without the -exec part first to see what files would be affected.Combining Conditions
Use logical operators to combine multiple search conditions.
# AND (implicit) - both conditions must match
find . -name "*.js" -size +10k
# OR - either condition matches
find . -name "*.js" -o -name "*.ts"
# NOT - exclude matches
find . -type f ! -name "*.log"
# Complex combinations with parentheses
find . \( -name "*.js" -o -name "*.ts" \) -mtime -7Practical Examples
Clean up old log files
find /var/log -name "*.log" -mtime +30 -deleteFind large files in home directory
find ~ -type f -size +100M -exec ls -lh {} \;Find and list all Git repositories
find ~ -type d -name ".git" 2>/dev/nullFind files with specific permissions
# Find world-writable files
find /var -perm -002 -type f
# Find setuid files
find / -perm -4000 -type f 2>/dev/nullSearch and replace in multiple files
find . -name "*.txt" -exec sed -i 's/old/new/g' {} \;Performance Tips
Here are some tips to make your find commands faster and more efficient.
# Limit search depth
find . -maxdepth 2 -name "*.js"
# Skip certain directories
find . -name "node_modules" -prune -o -name "*.js" -print
# Use -quit to stop after first match
find . -name "config.json" -quitlocate command which uses a pre-built database and is much faster. Install it with apt install mlocate on Debian/Ubuntu.Summary
The find command is incredibly powerful for locating files based on various criteria. Key takeaways:
- Use
-namefor name patterns,-inamefor case-insensitive - Filter by type with
-type f(files) or-type d(directories) - Search by size with
-sizeand time with-mtime - Execute commands on results with
-exec - Combine conditions with
-a(AND),-o(OR), and!(NOT)
Official Documentation
For authoritative information, refer to the official documentation: