find

Overview

The find command is used to search for files and directories in a directory hierarchy based on various criteria such as name, size, type, and permissions.

Syntax

find [path...] [expression]

Common Options

Option Description
-name pattern Search for files by name
-type f/d Search by type (f=file, d=directory)
-size n[cwbkMG] Search by size
-mtime n Search by modification time
-user name Search by owner
-perm mode Search by permissions
-exec command {} \; Execute command on found files
-maxdepth levels Limit search depth

Key Use Cases

  1. Locate files by name or pattern
  2. Find and delete old files
  3. Search for files by size or date
  4. Execute commands on found files
  5. Find files with specific permissions

Examples with Explanations

Example 1: Find files by name

find /home -name "*.txt"

Finds all .txt files in /home directory and subdirectories

Example 2: Find and delete old files

find /tmp -type f -mtime +30 -delete

Finds and deletes files older than 30 days in /tmp

Example 3: Find large files

find / -type f -size +100M

Finds files larger than 100 megabytes

Understanding Output

  • Default output shows full path of found files
  • Can be modified with -printf for custom formats
  • Error messages for inaccessible directories
  • Results can be sorted or filtered with other commands

Common Usage Patterns

  1. Find and execute:

    find . -name "*.log" -exec grep "error" {} \;
  2. Find recent files:

    find . -type f -mtime -7
  3. Find empty files/directories:

    find . -type f -empty

Performance Analysis

  • Use -maxdepth to limit directory traversal
  • Combine with -prune to skip directories
  • Use -xdev to stay on one filesystem
  • Consider using locate for faster name-based searches

Additional Resources