find – search for files in directory hierarchy

find command
find command

find searches for files and directories based on various criteria like name, size, time, and permissions.

Synopsis

find [PATH...] [EXPRESSION]

Common Options

OptionDescription
-name PATTERNMatch filename (case-sensitive)
-iname PATTERNMatch filename (case-insensitive)
-type fFiles only
-type dDirectories only
-size +100MLarger than 100MB
-mtime -7Modified in last 7 days
-user USERNAMEOwned by user
-perm 755With specific permissions
-exec CMD {} \;Execute command on results
-deleteDelete matching files

Examples

Find by name

$ find /home -name "*.txt"
$ find . -iname "readme*"    # Case-insensitive

Find by type

$ find /var/log -type f      # Files only
$ find . -type d -name "test"  # Directories named "test"

Find by size

$ find /home -size +100M     # Larger than 100MB
$ find . -size -1k           # Smaller than 1KB
$ find /tmp -size 0          # Empty files

Find by time

$ find . -mtime -7           # Modified in last 7 days
$ find . -mtime +30          # Modified more than 30 days ago
$ find . -mmin -60           # Modified in last 60 minutes

Find and execute command

$ find . -name "*.log" -exec rm {} \;
$ find . -type f -exec chmod 644 {} \;
$ find . -name "*.txt" -exec grep "error" {} +

Find and delete

$ find /tmp -type f -mtime +7 -delete
$ find . -name "*.tmp" -delete

Find by permissions

$ find /var/www -perm 777    # Exactly 777
$ find . -perm -u+x          # User executable

Find by owner

$ find /home -user greys
$ find /var -group www-data

Combine conditions

# AND (default)
$ find . -name "*.log" -size +1M

# OR
$ find . -name "*.jpg" -o -name "*.png"

# NOT
$ find . -type f ! -name "*.txt"

Limit depth

$ find . -maxdepth 1 -type f    # Current dir only
$ find . -maxdepth 2 -name "*.md"

Find empty files/directories

$ find . -type f -empty
$ find . -type d -empty

Common Patterns

Find and count

$ find . -name "*.py" | wc -l

Find recently modified

$ find . -mmin -30 -type f    # Last 30 minutes

Find large files

$ find / -type f -size +500M 2>/dev/null

Find and archive

$ find . -name "*.log" -exec tar -rvf logs.tar {} \;

Find duplicate names

$ find . -type f -name "*.txt" | xargs -I{} basename {} | sort | uniq -d

Tips

  • Use -print0 with xargs -0 for filenames with spaces
  • Redirect errors: 2>/dev/null to hide permission denied
  • Use locate for speed: It uses a database (but may be outdated)
  • Modern alternative: fd is faster and simpler for common cases
  • Test before -delete: Run without -delete first to verify matches

See Also

  • locate — Fast file search using database
  • grep — Search file contents
  • xargs — Build commands from input

Tutorials