Mastering Linux Find Command Options | Generated by AI

Home 2026.02

The find command is one of the most powerful tools in Unix/Linux for searching files and directories. Here’s a thorough guide to its options and capabilities.

Basic Syntax

find [path...] [expression]

Starting Points and Paths

You can specify one or more starting directories:

find /home /var /tmp -name "*.log"    # Search multiple directories
find . -name "file.txt"                # Current directory
find / -name "config"                  # Entire filesystem

Tests and Filters

Name-based searches:

find . -name "*.txt"
find . -iname "README*"
find . -regex ".*\.(jpg|png|gif)"

Type-based searches:

find /var -type d -name "log*"
find . -type f -name "*.sh"

Size-based searches:

find . -size +100M              # Larger than 100MB
find . -size -1k                # Smaller than 1KB
find /var/log -size +50M -size -100M  # Between 50-100MB

Time-based searches:

find . -mtime -7                # Modified in last 7 days
find . -mtime +30               # Modified more than 30 days ago
find . -mmin -60                # Modified in last hour
find . -newer reference.txt     # Newer than reference.txt

Permission-based searches:

find . -perm 644                # Exactly 644
find . -perm -644               # At least 644
find /bin -perm /u+s,g+s        # SUID or SGID set

Ownership searches:

find /home -user john
find . -nouser                  # Find orphaned files

Depth control:

find . -maxdepth 2 -name "*.txt"
find . -mindepth 3 -type f

Other useful tests:

find . -empty -type d           # Empty directories
find /usr/bin -executable -type f

Logical Operators

Combine tests with logical operators:

find . -name "*.txt" -o -name "*.md"
find . -type f -not -name "*.log"
find . \\( -name "*.c" -o -name "*.h" \\) -a -mtime -7

Actions

Display actions:

find . -name "*.txt" -print0
find . -printf "%p\t%s bytes\n"

Execution actions:

find . -name "*.tmp" -exec rm {} \;
find . -type f -exec chmod 644 {} +
find . -name "*.jpg" -execdir convert {} {}.png \;

Deletion:

find . -name "*.bak" -delete
find . -type f -empty -delete

Other actions:

find . -name ".git" -prune -o -type f -print

Practical Examples

# Find large files in home directory
find ~ -type f -size +500M -exec ls -lh {} \;

# Find and archive old log files
find /var/log -name "*.log" -mtime +90 -exec gzip {} \;

# Find broken symbolic links
find . -type l ! -exec test -e {} \; -print

# Find files modified today
find . -type f -mtime 0

# Find world-writable files (security check)
find / -perm -002 -type f 2>/dev/null

# Find setuid files (security audit)
find / -perm -4000 -type f 2>/dev/null

# Count files by extension
find . -type f | sed 's/.*\.//' | sort | uniq -c

# Find duplicate filenames
find . -type f -printf "%f\n" | sort | uniq -d

# Clean up temporary files
find /tmp -type f -atime +7 -user $(whoami) -delete

Performance Tips

The find command is incredibly flexible and can be combined with other Unix tools through pipes and command substitution to create powerful file management workflows.


Back Donate