Unix 'find' Command

Explore the power of the 'find' command in Unix. Our comprehensive guide covers file searches, criteria, and practical examples for efficient file location and management
E
Edtoks4:24 min read

The find command in Unix is a powerful tool for searching and locating files and directories within a specified directory hierarchy. It allows you to search for files based on various criteria such as file name, size, type, modification time, and more. Here's a detailed explanation of the find command with examples:

Basic Syntax:

find [directory] [options] [expression]
  • directory: The directory to start the search from. If not specified, find starts from the current directory.
  • options: Optional flags to control the behavior of find.
  • expression: Specifies the search criteria using various predicates and actions.

Common Options:

  1. -name pattern: Searches for files with a specific name pattern (case-sensitive).

    • Example: find /path/to/search -name "file.txt"
  2. -iname pattern: Searches for files with a specific name pattern (case-insensitive).

    • Example: find /path/to/search -iname "file.txt"
  3. -type type: Searches for files of a specific type (f for regular files, d for directories, l for symbolic links, etc.).

    • Example: find /path/to/search -type f
  4. -size size: Searches for files based on size. You can use suffixes like c (bytes), k (kilobytes), M (megabytes), and G (gigabytes).

    • Example: find /path/to/search -size +1M
  5. -mtime days: Searches for files modified within the specified number of days. Use + for older than, - for newer than, and no prefix for exactly that many days.

    • Example: find /path/to/search -mtime +7
  6. -exec command {} \;: Executes a command on each found file. Replace {} with the file name.

    • Example: find /path/to/search -type f -exec chmod 644 {} \;
  7. -print: Prints the paths of found files to the standard output.

  8. -maxdepth n: Limits the search to a maximum depth of n levels in the directory hierarchy.

    • Example: find /path/to/search -maxdepth 2

Examples of find Usage:

  1. Find Files by Name:

    • Search for all files named "file.txt" within the current directory and its subdirectories.
    find /path/to/search -name "file.txt"
    
  2. Find Directories:

    • Search for all directories within a specific path.
    find /path/to/search -type d
    
  3. Find Large Files:

    • Find all files larger than 10 megabytes.
    find /path/to/search -type f -size +10M
    
  4. Find Files Modified in the Last 7 Days:

    • Search for files modified in the last week.
    find /path/to/search -type f -mtime -7
    
  5. Find and Delete Files:

    • Find and delete all files with a .bak extension.
    find /path/to/search -type f -name "*.bak" -exec rm {} \;
    
  6. Find and Copy Files:

    • Find and copy all .log files to a backup directory.
    find /path/to/search -type f -name "*.log" -exec cp {} /backup/ \;
    
  7. Find Empty Files and Directories:

    • Search for empty files and directories.
    find /path/to/search -empty
    
  8. Find Setuid and Setgid Files:

    • Find all setuid and setgid files on the system.
    find / -type f \( -perm -4000 -o -perm -2000 \)
    
  9. Find Files Modified Within a Specific Date Range:

    • Search for files modified between two dates.
    find /path/to/search -type f -newermt "2022-01-01" ! -newermt "2022-12-31"
    
  10. Find Files with a Specific Extension and List Sizes:

    • Search for .jpg files and list their sizes.
    find /path/to/search -type f -name "*.jpg" -exec ls -lh {} \;
    

The find command is a versatile tool for file and directory searches in Unix. By combining different options and expressions, you can create complex search queries to locate and work with files and directories based on various criteria. It is especially useful for tasks like backups, cleanup, and data analysis.

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.