Skip to content

Find Command Cheatsheet

Table of Contents

Command Structure

The basic syntax of the find command is:

Terminal window
find [path] [options] [expression]
  • [path]: Starting point of the search (defaults to current directory if not specified)
  • [options]: Modify search behavior
  • [expression]: Define search criteria (name, type, size, etc.)

Basic Search Patterns

  1. Simple Search

    Terminal window
    find . -name "file.txt" # Find specific file
    find /home -type d # Find all directories
  2. Multiple Criteria

    Terminal window
    find . -type f -name "*.txt" # Find all .txt files
    find . -type f -empty # Find empty files
  3. Negation

    Terminal window
    find . ! -name "*.txt" # Find files NOT ending in .txt
    find . ! -type d # Find everything that's not a directory

Search by Name

Terminal window
-name "pattern" # Case-sensitive name search
-iname "pattern" # Case-insensitive name search
-path "pattern" # Search in full path
-regex "pattern" # Use regex pattern

Search by Type

The -type option accepts these file type indicators:

Terminal window
find . -type f # Regular files
find . -type d # Directories
find . -type l # Symbolic links
find . -type b # Block devices
find . -type c # Character devices
find . -type p # Named pipes
find . -type s # Sockets

Search by Size

  • c: bytes
  • k: kilobytes
  • M: megabytes
  • G: gigabytes

Search by Time

  • -mtime: Content modification time
  • -atime: Access time
  • -ctime: Status change time
  • -mmin, -amin, -cmin: Time in minutes

Search by Permissions

Terminal window
-perm mode # Exact permissions
-perm -mode # At least these permissions
-perm /mode # Any of these permissions

Search by Owner

Terminal window
# User ownership
find . -user username # Files owned by username
find . -uid N # Files owned by UID N
# Group ownership
find . -group groupname # Files owned by groupname
find . -gid N # Files owned by GID N

Actions and Execution

Terminal window
-delete # Delete matching files
-print # Print matches (default)
-ls # Detailed listing
-quit # Exit after first match

Complex Operations

  1. Combining Conditions

    Terminal window
    # AND operation (implicit)
    find . -type f -name "*.txt" -size +1M
    # OR operation
    find . -name "*.jpg" -o -name "*.png"
    # NOT operation
    find . ! -name "*.txt" -type f
  2. Directory Management

    Terminal window
    # Find and remove empty directories
    find . -type d -empty -delete
    # Skip specific directories
    find . -path "./node_modules" -prune -o -print
  3. Advanced Time Operations

    Terminal window
    # Files modified in last hour, larger than 1MB
    find . -mmin -60 -size +1M -type f
    # Recently changed executables
    find . -type f -executable -ctime -1


Real-World Scenarios

Terminal window
# Find and remove files older than 30 days in /tmp
find /tmp -type f -mtime +30 -delete
# Find all .log files larger than 100MB
find /var/log -type f -name "*.log" -size +100M -exec ls -lh {} \;
# Find and compress old log files while maintaining structure
find . -name "*.log" -mtime +7 -exec gzip {} \;
# Find duplicate files (by name) and create a report
find . -type f -name "*.jpg" -exec md5sum {} \; | sort | uniq -d

Advanced Search Techniques

Using Multiple Time Conditions

Terminal window
# Files modified in last hour but not accessed in last week
find . -type f -mmin -60 -not -atime -7
# Files changed in last 24 hours but not modified
find . -type f -ctime -1 -not -mtime -1
# Files accessed recently but created long ago
find . -type f -atime -1 -ctime +30

Complex Permission Searches

Terminal window
# Files with exact permissions
find . -type f -perm 644
# Files with at least these permissions
find . -type f -perm -644
# Files with any of these permissions
find . -type f -perm /u+w,g+w
# Files without specific permissions
find . -type f ! -perm -o+r

Advanced Execution Techniques

Using -exec with Multiple Commands

Terminal window
# Multiple commands with -exec
find . -type f -name "*.txt" -exec chmod 644 {} \; -exec echo "Processing: {}" \;
# Using shell features with -exec
find . -type f -name "*.log" -exec sh -c '
for file do
echo "Processing $file"
gzip "$file"
done
' sh {} +

Optimizing -exec Operations

  1. Using -exec with +

    Terminal window
    # Less efficient (runs command for each file)
    find . -name "*.txt" -exec chmod 644 {} \;
    # More efficient (runs command with multiple files)
    find . -name "*.txt" -exec chmod 644 {} +
  2. Using xargs

    Terminal window
    # Parallel processing with xargs
    find . -name "*.jpg" -print0 | xargs -0 -P4 convert -resize 800x600
    # Safe handling of filenames with spaces
    find . -name "*.txt" -print0 | xargs -0 grep "pattern"

Error Handling and Debugging

Terminal window
# Suppress permission denied messages
find / -name "*.conf" 2>/dev/null
# Log errors to file
find / -name "*.conf" 2>>/var/log/find_errors.log
# Continue on error
find . -name "*.txt" -exec rm {} \; || true

Integration with Other Tools

Find with Git

Terminal window
# Find files not in git
find . -type f -not -path "./.git/*" -print0 |
git check-ignore --stdin -z
# Find and add new files to git
find . -type f -not -path "./.git/*" -exec git add {} +
# Find files modified after last commit
find . -type f -newer .git/index

Find with Grep

Terminal window
# Search for pattern in specific files
find . -type f -name "*.py" -exec grep -l "pattern" {} +
# Search with context
find . -type f -name "*.log" -exec grep -C 2 "error" {} \;
# Search case-insensitive
find . -type f -name "*.txt" -exec grep -i "pattern" {} +

Performance Optimization

  1. Limiting Search Depth

    Terminal window
    # Limit directory depth
    find . -maxdepth 2 -type f
    # Skip specific directories
    find . -path "./node_modules" -prune -o -type f -print
  2. Filesystem Optimization

    Terminal window
    # Stay on same filesystem
    find / -xdev -type f -size +100M
    # Use locate for faster searches
    locate "*.conf" | find -f
  3. Memory Usage

    Terminal window
    # Limit process memory
    find . -type f -size +1G -exec ulimit -v 1048576 \; \
    -exec process_large_file {} \;

Backup and Archive Operations

Terminal window
# Create backup of files modified today
find . -type f -mtime -1 -exec cp --parents {} /backup/ \;
# Create tar archive of old files
find . -type f -mtime +30 -print0 |
tar czf old_files.tar.gz --null -T -
# Sync files modified in last hour
find . -type f -mmin -60 -exec rsync -av {} backup/ \;

Security Considerations

Terminal window
# Find files with insecure permissions
find / -type f -perm -o+w ! -path "/proc/*" ! -path "/sys/*"
# Find unauthorized SUID/SGID files
find / -type f \( -perm -4000 -o -perm -2000 \) -ls
# Find recently modified system files
find /etc -type f -mtime -1 -ls