Skip to main content

find

Quick Summary

find is a real-time filesystem search tool. It walks directories directly, filters by metadata, and can run actions on the matches.

Learning Path

1. Overview

  • What find does and when to use it
  • How it differs from indexed search tools
  • When to prefer it for audits and automation

2. Syntax

  • Basic command structure
  • Path, expression, and action order
  • Default output behavior

3. Core Filters

  • -name and -iname
  • -type
  • -size
  • -mtime, -atime, and -ctime
  • -user and -group
  • -perm

4. Actions

  • -print
  • -ls
  • -delete
  • -exec
  • -ok
  • -printf

5. Practical Search Patterns

  • Find specific files
  • Find by extension
  • Find directories only
  • Find empty files and folders
  • Limit depth with -maxdepth and -mindepth

6. Maintenance Workflows

  • Bulk permission fixes
  • Ownership corrections
  • Safe cleanup with -ok and -delete
  • Exporting search results

7. Safety and Edge Cases

  • Quoting patterns correctly
  • Avoiding destructive actions accidentally
  • Using -prune for exclusions
  • Testing with dry-run style commands first

Core Syntax

find [PATH] [EXPRESSION] [ACTION]
PartMeaning
PATHDirectory to start searching from
EXPRESSIONFilters like -name, -type, -size
ACTIONWhat to do with matches, such as -print or -delete

Core Filters

FilterMeaningExample
-nameMatch filename exactlyfind /var/www -name "wp-config.php"
-inameCase-insensitive filename matchfind . -iname "*.jpg"
-type fMatch files onlyfind . -type f -name "*.php"
-type dMatch directories onlyfind . -type d -name "uploads"
-size +100MMatch files larger than 100 MBfind /backups -size +100M
-mtime -1Modified in the last dayfind . -mtime -1
-ctime -2Metadata changed in the last 2 daysfind . -ctime -2
-user rootMatch ownerfind /var/www -user root
-perm 777Match permission bitsfind . -perm 777

Actions

ActionMeaningExample
-printShow matchesfind . -name "*.log" -print
-lsShow ls -l style outputfind . -type f -ls
-deleteRemove matchesfind /var/log -name "*.log" -delete
-exec CMD {} \;Run a command on each matchfind . -name "*.php" -exec chmod 644 {} \;
-ok CMD {} \;Confirm before runningfind . -name "*.bak" -ok rm {} \;
-printfCustom formatted outputfind . -printf "%f %s\n"

Practical Examples

Find a specific file

find /var/www/html -name "wp-config.php"

Find all PHP files

find /var/www/html -name "*.php"

Find directories only

find /var/www/html -type d -name "uploads"

Find large files

find /var/www/html -type f -size +100M

Find empty files

find /var/www/html -type f -empty

Find and delete logs

find /var/log -name "*.log" -delete

Fix permissions

find /var/www/html -name "*.php" -exec chmod 644 {} \;

Best Practices

  • Start with a narrow path.
  • Test the search before using -delete or -exec.
  • Quote patterns like *.php so the shell does not expand them first.
  • Use -maxdepth when you only want the top level.
  • Use -prune to skip cache or vendor directories.

What's Next

  1. Learn the syntax and expression rules in the examples above.
  2. Practice a few searches against /var/www, /home, or /var/log.
  3. Add find to shell scripts for cleanup, audits, and bulk maintenance.