How to Find Files in Linux
Finding files and directories in Linux is a fundamental skill for system administrators and users alike. This comprehensive guide covers everything you need to know about locating files using various tools and search criteria.
Overview: File Search Tools in Linux
Linux provides several powerful tools for finding files, each with its own strengths and use cases.
1. The find Command
The most versatile and powerful file search tool in Linux. It searches the filesystem in real-time and supports complex search criteria.
Best for: Complex searches, combining multiple criteria, performing actions on results
# Basic find syntax
find [path] [options] [expression]
# Example: Find all .txt files
find /home -name "*.txt"
2. The locate Command
A fast file search tool that uses a pre-built database. Much faster than find but requires periodic database updates.
Best for: Quick searches by filename across the entire system
# Search for files containing "config" in the name
locate config
# Case-insensitive search
locate -i CONFIG
3. The updatedb Command
Updates the database used by locate. Run with sudo to update system-wide.
# Update the locate database
sudo updatedb
4. The which Command
Locates executable programs in PATH directories.
Best for: Finding where commands are installed
# Find the location of python
which python
# Find all instances
which -a python
5. The whereis Command
Locates binary, source, and manual page files for a command.
Best for: Finding program binaries and documentation
# Find program files
whereis python
# Only binaries
whereis -b python
# Only manuals
whereis -m python
6. Using grep to Search File Contents
While not primarily for finding files, grep combined with find is powerful for content searches.
# Find files containing specific text
grep -r "error" /var/log/
# Find and show filenames only
grep -rl "TODO" /home/user/projects/
Find Files by Name and Pattern
One of the most common search needs is finding files by their name or pattern.
Find Exact Filename
# Find exact filename (case-sensitive)
find /home -name "document.txt"
# Find in current directory
find . -name "config.yaml"
Case-Insensitive Name Search
# Find files regardless of case
find /var -iname "readme.txt"
# Will match: README.txt, ReadMe.TXT, readme.txt
Find by Wildcard Pattern
# Find all log files
find /var/log -name "*.log"
# Find all files starting with "test"
find . -name "test*"
# Find files with specific pattern
find . -name "*backup*"
Find by Extension
# Find all Python files
find ~/projects -name "*.py"
# Find multiple extensions using -o (OR)
find . -name "*.jpg" -o -name "*.png"
# Find all image files
find . \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" \)
Using locate for Quick Name Searches
# Fast search across entire system
locate filename.txt
# Limit results
locate -n 10 config
# Case-insensitive
locate -i readme
# Update database before searching
sudo updatedb && locate myfile
Find Files by Size
Finding files based on their size is crucial for disk space management and cleanup.
Find Files Larger Than Specific Size
# Files larger than 100MB
find /home -type f -size +100M
# Files larger than 1GB
find . -size +1G
# Files larger than 500KB
find /var -size +500k
Find Files Smaller Than Specific Size
# Files smaller than 1MB
find . -type f -size -1M
# Files smaller than 100KB
find /tmp -size -100k
# Very small files (less than 1KB)
find . -size -1k
Find Files with Exact Size
# Files exactly 512 bytes
find . -size 512c
# Files exactly 10MB
find . -size 10M
Find Files in Size Range
# Files between 10MB and 100MB
find . -type f -size +10M -size -100M
# Files between 1KB and 1MB
find /var/log -size +1k -size -1M
Size Units Reference
c- bytesk- kilobytes (1024 bytes)M- megabytes (1024 KB)G- gigabytes (1024 MB)
Find and Sort by Size
# Find large files and sort by size
find /home -type f -size +50M -exec ls -lh {} \; | sort -k5 -hr
# Top 10 largest files
find . -type f -exec du -h {} \; | sort -rh | head -n 10
Find Files by Permissions
Searching by file permissions helps identify security issues and configuration problems.
Find by Exact Permissions
# Find files with exact permissions 644
find /var/www -type f -perm 644
# Find directories with permissions 755
find /var/www -type d -perm 755
# Find files with permission 777 (potential security risk)
find /var/www -perm 777
Find by Minimum Permissions
# Files where at least owner has read permission
find . -perm -u=r
# Files with at least rw for owner
find . -perm -u=rw
# Files with at least 755
find . -perm -755
Find Files with Specific Permission Bits
# Find SUID files (security audit)
find / -perm -4000 -type f 2>/dev/null
# Find SGID files
find / -perm -2000 -type f 2>/dev/null
# Find sticky bit files
find / -perm -1000 2>/dev/null
Find Writable Files
# World-writable files (security concern)
find / -perm -002 -type f 2>/dev/null
# Group-writable files
find . -perm -020 -type f
# User-writable files
find . -perm -200 -type f
Find Executable Files
# Find executable files
find /usr/local/bin -type f -executable
# Find files executable by owner
find . -perm -u=x -type f
# All executable files in current directory
find . -type f -perm /u+x,g+x,o+x
Find and Fix Permissions
# Find and fix directory permissions
find /var/www -type d -exec chmod 755 {} \;
# Find and fix file permissions
find /var/www -type f -exec chmod 644 {} \;
# Remove world-writable permissions
find . -perm -002 -exec chmod o-w {} \;
Find Files by Time and Date
Time-based searches help locate recently modified files or old files that can be archived.
Find by Modification Time (mtime)
# Files modified in the last 7 days
find /home -mtime -7
# Files modified exactly 7 days ago
find /home -mtime 7
# Files modified more than 30 days ago
find /var/log -mtime +30
# Files modified in last 24 hours
find . -mtime 0
Find by Modification Time in Minutes (mmin)
# Files modified in last 30 minutes
find /tmp -mmin -30
# Files modified in last hour
find . -mmin -60
# Files modified more than 2 hours ago
find /var -mmin +120
Find by Access Time (atime)
# Files accessed in last 7 days
find /home -atime -7
# Files not accessed in 90 days (candidates for archival)
find /data -atime +90
# Files accessed in last hour
find . -amin -60
Find by Status Change Time (ctime)
# Files with metadata changed in last 7 days
find /etc -ctime -7
# Files with status changed more than 30 days ago
find . -ctime +30
Find Files Newer Than a Reference File
# Files newer than reference.txt
find . -newer reference.txt
# Files modified more recently than a specific file
find /var/log -newer /var/log/syslog
Find Files by Date Range
# Create reference files
touch -t 202301010000 /tmp/start
touch -t 202312312359 /tmp/end
# Find files between dates
find /var/log -newer /tmp/start ! -newer /tmp/end
Find and Delete Old Files
# Delete log files older than 30 days
find /var/log -name "*.log" -mtime +30 -delete
# Delete temp files older than 7 days
find /tmp -type f -mtime +7 -delete
# Find old backup files (dry run first)
find /backups -name "*.bak" -mtime +90 -print
Time Units Reference
-mtime n- Modified n days ago-mmin n- Modified n minutes ago-atime n- Accessed n days ago-amin n- Accessed n minutes ago-ctime n- Status changed n days ago-cmin n- Status changed n minutes ago
Note: -n means less than n, +n means more than n, n means exactly n
Using find with -exec and Actions
The -exec option allows you to perform actions on found files, making find extremely powerful.
Basic -exec Syntax
# Basic syntax
find [path] [criteria] -exec command {} \;
# {} is replaced with each found file
# \; marks the end of the -exec command
Delete Found Files
# Delete all .tmp files
find . -name "*.tmp" -exec rm {} \;
# Better: use -delete (safer)
find . -name "*.tmp" -delete
# Delete with confirmation
find . -name "*.bak" -ok rm {} \;
Copy Found Files
# Copy all .pdf files to a directory
find . -name "*.pdf" -exec cp {} /backup/pdfs/ \;
# Copy with directory structure preserved
find . -name "*.conf" -exec cp --parents {} /backup/ \;
Move Found Files
# Move old log files to archive
find /var/log -name "*.log" -mtime +30 -exec mv {} /archive/logs/ \;
# Move with rename
find . -name "*.txt" -exec mv {} {}.backup \;
Change Permissions or Ownership
# Set permissions on found files
find /var/www -type f -exec chmod 644 {} \;
# Change ownership
find /home/user -type f -exec chown user:group {} \;
# Fix permissions on scripts
find . -name "*.sh" -exec chmod +x {} \;
Execute Multiple Commands
# Execute multiple commands on same file
find . -name "*.log" -exec gzip {} \; -exec mv {}.gz /archive/ \;
# Using sh -c for complex commands
find . -type f -exec sh -c 'echo "Processing: $1"; process "$1"' _ {} \;
Using -exec with Placeholders
# Process with basename
find /path -type f -exec basename {} \;
# Complex processing
find . -name "*.jpg" -exec convert {} -resize 800x600 thumbs/{} \;
-exec vs -exec {} +
# Executes command once per file (slower)
find . -name "*.txt" -exec echo {} \;
# Executes command once with all files (faster, like xargs)
find . -name "*.txt" -exec echo {} +
# Example: more efficient deletion
find /tmp -name "core.*" -exec rm {} +
Interactive Execution with -ok
# Prompt before each action
find . -name "*.old" -ok rm {} \;
# Prompt before changing permissions
find /var/www -type f -ok chmod 644 {} \;
Using with xargs (Alternative to -exec)
# Better performance for many files
find . -name "*.log" -print0 | xargs -0 rm
# Parallel execution
find . -name "*.jpg" -print0 | xargs -0 -P 4 optimize-image
# With custom delimiter
find . -name "*.txt" | xargs -I {} cp {} /backup/
Common -exec Patterns
# Find and list with details
find . -type f -size +100M -exec ls -lh {} \;
# Find and display file contents
find . -name "README*" -exec cat {} \;
# Find and compress
find /var/log -name "*.log" -mtime +7 -exec gzip {} \;
# Find and search content
find . -name "*.php" -exec grep -H "password" {} \;
# Find and count
find . -type f -exec basename {} \; | sort | uniq -c
# Archive found files
find . -name "*.doc" -exec tar -rvf archive.tar {} \;
Find Files by Type
Linux supports various file types beyond just regular files and directories.
Find Regular Files
# All regular files
find /home -type f
# Regular files with specific name
find . -type f -name "*.txt"
Find Directories
# All directories
find /var -type d
# Directories with specific name
find . -type d -name "backup*"
# Empty directories
find . -type d -empty
Find Symbolic Links
# All symbolic links
find /usr -type l
# Broken symbolic links
find . -type l ! -exec test -e {} \; -print
# Symbolic links pointing to specific target
find . -type l -lname "*/target/*"
Find Special Files
# Block devices
find /dev -type b
# Character devices
find /dev -type c
# Named pipes (FIFOs)
find /tmp -type p
# Sockets
find /var -type s
Combine Type with Other Criteria
# Large regular files only
find /var -type f -size +100M
# Recently modified directories
find /home -type d -mtime -7
# Executable regular files
find /usr/bin -type f -executable
Find Files by Owner and Group
Finding files by ownership is essential for system administration and security audits.
Find by Owner
# Files owned by specific user
find /home -user john
# Files owned by current user
find /var/www -user $(whoami)
# Files owned by UID
find / -uid 1000
Find by Group
# Files owned by specific group
find /var/www -group www-data
# Files owned by specific GID
find / -gid 33
Find Files with No Owner or Group
# Files with no valid user (orphaned)
find / -nouser
# Files with no valid group
find / -nogroup
# Both (potential security concern)
find / -nouser -o -nogroup 2>/dev/null
Combine Owner and Group
# Files owned by user AND group
find /var -user www-data -group www-data
# Files owned by user OR group
find /var -user root -o -group admin
Find and Change Ownership
# Find and change owner
find /var/www -user old-user -exec chown new-user {} \;
# Find and change group
find /var/www -group old-group -exec chgrp new-group {} \;
# Change both owner and group
find /home/user -exec chown user:user {} \;
Advanced Find Techniques
Take your file finding skills to the next level with these advanced techniques.
Combining Multiple Criteria
# AND conditions (implicit)
find . -name "*.log" -size +1M -mtime -7
# OR conditions
find . -name "*.log" -o -name "*.txt"
# Complex conditions with parentheses
find . \( -name "*.log" -o -name "*.txt" \) -size +1M
# NOT conditions
find . ! -name "*.log"
# Multiple conditions
find /var/log -type f -size +10M -mtime +30 -name "*.log"
Depth Control
# Search only in current directory (no subdirectories)
find . -maxdepth 1 -name "*.txt"
# Search up to 2 levels deep
find /var -maxdepth 2 -type d
# Start search from 2 levels deep
find /home -mindepth 2 -name "config"
# Exact depth level
find . -mindepth 2 -maxdepth 2 -type f
Excluding Directories
# Exclude .git directories
find . -path "./.git" -prune -o -type f -print
# Exclude multiple directories
find . \( -path "./node_modules" -o -path "./.git" \) -prune -o -type f -print
# Exclude pattern
find / -path "*/proc/*" -prune -o -name "*.conf" -print
Empty Files and Directories
# All empty files and directories
find /tmp -empty
# Empty files only
find . -type f -empty
# Empty directories only
find . -type d -empty
# Delete empty directories
find . -type d -empty -delete
Find Duplicate Files
# Find files with same name
find . -type f -exec basename {} \; | sort | uniq -d
# Find duplicate files by content (using md5sum)
find . -type f -exec md5sum {} \; | sort | uniq -w32 -D
# Find duplicate files in a directory
find /path -type f -exec md5sum {} + | sort | uniq -w32 -D --all-repeated=separate
Case Studies: Real-World Examples
Clean Up Old Log Files
# Find, compress, and archive old logs
find /var/log -name "*.log" -mtime +30 -exec gzip {} \; -exec mv {}.gz /archive/logs/ \;
Find Large Files Consuming Disk Space
# Top 20 largest files
find /home -type f -exec du -h {} + | sort -rh | head -n 20
Security Audit: Find SUID/SGID Files
# Find all SUID files
find / -type f -perm -4000 -ls 2>/dev/null
# Find all SGID files
find / -type f -perm -2000 -ls 2>/dev/null
Find Recently Modified Configuration Files
# Config files changed in last 24 hours
find /etc -name "*.conf" -mtime 0
Batch Rename Files
# Add .backup extension to all .conf files
find /etc -name "*.conf" -exec mv {} {}.backup \;
Performance Tips and Best Practices
Optimize Find Performance
# Limit search depth
find /var -maxdepth 3 -name "*.log"
# Use -type early in expression
find . -type f -name "*.txt" # Faster
# Avoid searching entire filesystem
find /specific/path -name "file" # Better than find / -name "file"
# Use -prune to skip directories
find . -path "./large_dir" -prune -o -name "*.txt" -print
Use locate Instead of find When Possible
# Much faster for simple name searches
locate filename # Faster than: find / -name filename
# But remember to update database
sudo updatedb
Redirect Errors for Cleaner Output
# Hide permission denied errors
find / -name "config" 2>/dev/null
# Redirect errors to a file
find / -name "*.conf" 2>errors.log
# Separate output and errors
find / -name "*.log" > found.txt 2> errors.txt
Parallel Processing
# Use xargs with parallel execution
find . -name "*.jpg" -print0 | xargs -0 -P 4 -I {} convert {} {}.optimized
# GNU Parallel (if installed)
find . -name "*.txt" | parallel process {}
Best Practices
- Always test with -print first before using -delete or -exec
- Use -maxdepth to limit search scope
- Quote patterns to prevent shell expansion
- Use -print0 with xargs -0 for filenames with spaces
- Redirect stderr when searching system directories
- Update locate database regularly:
sudo updatedb - Use specific paths instead of searching from root
- Combine related criteria efficiently
Common Use Cases and Examples
Web Development
# Find all JavaScript files modified today
find ./src -name "*.js" -mtime 0
# Find large image files that need optimization
find ./public/images -type f \( -name "*.jpg" -o -name "*.png" \) -size +500k
# Find configuration files
find ./config -name "*.conf" -o -name "*.yaml" -o -name "*.json"
# Clean up node_modules directories
find . -name "node_modules" -type d -prune -exec rm -rf {} \;
System Administration
# Find core dump files
find /var -name "core.*" -o -name "*.core"
# Find files modified in last hour (troubleshooting)
find /etc -mmin -60
# Find world-writable files (security)
find / -type f -perm -002 ! -path "/proc/*" 2>/dev/null
# Find files larger than 1GB
find /var -type f -size +1G -exec ls -lah {} \;
Backup and Archiving
# Find files for incremental backup
find /data -mtime -1 -print0 | tar -czvf backup-$(date +%Y%m%d).tar.gz --null -T -
# Archive old files
find /archive -type f -mtime +365 -exec tar -rvf old-archive.tar {} \;
# Find and copy recent documents
find ~/Documents -name "*.pdf" -mtime -30 -exec cp {} ~/backup/recent/ \;
Development
# Find TODO comments in code
find ./src -name "*.py" -exec grep -H "TODO" {} \;
# Find files without copyright header
find ./src -name "*.java" ! -exec grep -q "Copyright" {} \; -print
# Find large git repositories
find ~/projects -name ".git" -type d -exec du -sh {} \;
# Remove compiled files
find . -name "*.pyc" -o -name "*.class" -o -name "*.o" -delete
Media Management
# Find duplicate images (by size)
find ~/Pictures -type f -name "*.jpg" -exec ls -l {} \; | awk '{print $5, $9}' | sort -n
# Find high-resolution images
find ./photos -name "*.jpg" -size +5M
# Batch convert images
find . -name "*.png" -exec convert {} {}.jpg \;
# Find videos by duration (requires mediainfo)
find . -name "*.mp4" -exec mediainfo --Inform="General;%Duration%" {} \;
Troubleshooting Common Issues
Permission Denied Errors
# Use sudo for system-wide searches
sudo find /var -name "*.log"
# Or redirect errors
find /var -name "*.log" 2>/dev/null
Too Many Results
# Limit depth
find . -maxdepth 2 -name "*.txt"
# Pipe to head
find / -name "config" 2>/dev/null | head -n 20
# Use more specific criteria
find /home/user/documents -name "report-2024*.pdf"
Slow Searches
# Use locate instead
locate filename
# Limit search path
find /home/user -name "file" # Instead of: find /home -name "file"
# Use -prune to exclude directories
find . -path "./large_folder" -prune -o -name "*.txt" -print
Filenames with Spaces
# Use -print0 with xargs -0
find . -name "*.mp3" -print0 | xargs -0 ls -l
# Or quote the exec command properly
find . -name "*.mp3" -exec ls -l "{}" \;
locate Not Finding Recent Files
# Update the database
sudo updatedb
# Or use find instead
find / -name "recently-created-file"
Find Options Reference
| Option | Description | Example |
|---|---|---|
-name |
Case-sensitive name | find . -name "file.txt" |
-iname |
Case-insensitive name | find . -iname "FILE.txt" |
-type |
File type (f/d/l) | find . -type f |
-size |
File size | find . -size +100M |
-mtime |
Modified days ago | find . -mtime -7 |
-mmin |
Modified minutes ago | find . -mmin -30 |
-user |
Owner username | find . -user john |
-group |
Owner group | find . -group staff |
-perm |
Permissions | find . -perm 644 |
-empty |
Empty files/dirs | find . -empty |
-exec |
Execute command | find . -name "*.log" -exec rm {} \; |
-delete |
Delete found files | find . -name "*.tmp" -delete |
-maxdepth |
Max directory depth | find . -maxdepth 2 -name "*.txt" |
-mindepth |
Min directory depth | find . -mindepth 2 -name "*.txt" |
-newer |
Newer than file | find . -newer reference.txt |
-prune |
Exclude directory | find . -path "./dir" -prune |
Logical Operators
| Operator | Description | Example |
|---|---|---|
| (none) | AND (implicit) | find . -name "*.txt" -size +1M |
-o |
OR | find . -name "*.txt" -o -name "*.log" |
! or -not |
NOT | find . ! -name "*.txt" |
\( \) |
Grouping | find . \( -name "*.txt" -o -name "*.log" \) -size +1M |