Skip to main content

find

Explanation

The find command is a powerful tool for searching files and directories within a directory hierarchy based on user-specified criteria like name, size, type, permissions, or timestamps. It can also execute actions (like deleting or moving) on the files it finds.

Basic Syntax

find [starting-point] [expression]

Common Flags and Expressions

ExpressionDescription
-name PATTERNBase of file name matches shell pattern PATTERN.
-iname PATTERNLike -name, but case-insensitive.
-type cFile is of type c: f (regular file), d (directory), l (symlink).
-size nFile uses n units of space (e.g., +100M for greater than 100 Megabytes).
-mtime nData was last modified n*24 hours ago.
-exec COMMAND ;Execute COMMAND; true if 0 status is returned. {} is replaced by the file name.

Real-world Examples

  1. Find files by exact name:

    find /home -name "document.txt"
  2. Find all directories in the current path:

    find . -type d
  3. Find files larger than 50MB and delete them:

    find /var/log -type f -size +50M -exec rm -f {} +
  4. Find files modified in the last 7 days:

    find /etc -mtime -7