Daily Linux Command: Find

<span>find</span> is a very powerful command in Linux used to search for files and directories within a directory tree. It can search based on various conditions such as file name, type, size, permissions, timestamps, and can perform operations on the found files (such as delete, move, print, etc.).

🧩 Basic Syntax

find [starting_directory] [expression]

📌 Common Examples

1. Find all <span>.log</span> files in a specified directory

find /var/log -name "*.log"
  • <span>/var/log</span>: starting directory.
  • <span>-name "*.log"</span>: find files matching the name <span>.log</span>.

2. Find all files larger than 1MB in the current directory and its subdirectories

find . -type f -size +1M
  • <span>.</span>: current directory.
  • <span>-type f</span>: only search for regular files.
  • <span>-size +1M</span>: files larger than 1MB.

3. Find files modified in the last 7 days

find /home/user -type f -mtime -7
  • <span>-mtime -7</span>: files modified in the last 7 days.

4. Find and delete all empty files

find /path/to/dir -type f -empty -delete
  • <span>-empty</span>: find empty files or directories.
  • <span>-delete</span>: delete the found files (use with caution).

5. Find and execute a command (e.g., view file contents)

find . -name "*.txt" -exec cat {} \;
  • <span>-exec</span>: execute a command on each found file.
  • <span>{}</span>: represents the path of the currently found file.
  • <span>\;</span>: indicates the end of the command.

🔍 Common Options Explained

Option Description
<span>-name</span> match by file name
<span>-iname</span> case-insensitive file name match
<span>-type</span> match by file type (f=file, d=directory, etc.)
<span>-size</span> match by file size
<span>-mtime</span> match by modification time (in days)
<span>-atime</span> match by access time
<span>-ctime</span> match by status change time
<span>-user</span> match by file owner
<span>-group</span> match by group ownership
<span>-perm</span> match by permissions
<span>-empty</span> find empty files or directories
<span>-exec</span> execute a command on matched files
<span>-delete</span> delete matched files
<span>-ls</span> list detailed information
<span>-print</span> default action, output matched file paths

🧠 Tips

  • Use <span>-o</span> (or), <span>-a</span> (and), <span>!</span> (not) for logical combinations:

    find /tmp -name "*.tmp" -o -name "*.bak"
    

    Find <span>.tmp</span> or <span>.bak</span> files.

  • When using parentheses, they need to be escaped:

    find / -type f \( -name "core" -o -name "*.tmp" \) -exec rm -f {} \;
    

❗ Cautions

  • Permission issues: use <span>sudo</span> to find files in system directories.
  • Performance considerations: searching large directories may be slow, it is recommended to limit the search scope.
  • Safe operations: be particularly careful when using <span>-exec</span> or <span>-delete</span>, it is best to preview first with <span>-print</span>.

Leave a Comment