Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

find Command Guide for Searching the Filesystem

This guide helps you understand and use the find command, which searches for files and directories that match criteria such as name, type, size, or modification time.


What is find, and Why Use It?

The find command walks through a directory tree and prints the paths of everything that matches the tests you give it. It is the most powerful search tool on any Unix-like system because you can combine name patterns with type, size, date, permission, and action filters.

Why Learn find?

  • Locate files by rule, not name alone: “every .conf file modified in the last 7 days” is impossible with ls.
  • Safe deletion: find paired with -delete or -exec rm removes only what matches, which is far safer than guessing.
  • Scripting backbone: Almost every shell script that cleans up, backs up, or audits a system uses find.
  • No database required: Unlike locate, find reads the filesystem directly, so results are always current.

find is part of the base system on Linux and macOS. There is nothing to install.


1. Find Files by Name

find . -name "notes.txt"
  • -name matches the file name exactly; it is case-sensitive.
find . -iname "notes.txt"
  • -iname is the case-insensitive variant.
find /etc -name "*.conf"
  • The pattern must be quoted, or the shell expands the * before find ever sees it.

Example:

find . -name "*.md"
./introduction.md
./SUMMARY.md
./cli-navigation/ls.md

2. Find Files and Directories by Type

find . -type f -name "*.sh"
  • -type f restricts results to regular files.
find . -type d
  • -type d lists directories only.
find /var/log -type l
  • -type l finds symbolic links.

3. Limit How Deep find Looks

find . -maxdepth 2 -type f
  • -maxdepth 2 stops descending after two levels — the single most useful control for large trees.
find . -mindepth 1 -maxdepth 1
  • -mindepth 1 skips the starting directory itself, giving you a clean “list this folder” result.

4. Find by Size

find . -size +100M
  • +100M means larger than 100 megabytes.
find . -size -1k -type f
  • -1k means smaller than 1 kilobyte.
find /var/log -size +10M -exec ls -lh {} +
  • The suffixes c, k, M, and G select bytes, kilobytes, megabytes, and gigabytes.

5. Find by Modification Time

find . -mtime -7
  • -mtime -7 matches files modified less than 7 days ago.
find . -mtime +30
  • +30 matches files modified more than 30 days ago.
find . -newer reference.txt
  • -newer compares against another file’s timestamp instead of a fixed number of days.

6. Act on the Results

find . -name "*.tmp" -delete
  • -delete removes every match. There is no confirmation — test with a plain find first.
find . -name "*.log" -exec gzip {} +
  • -exec ... {} + runs a command on the matches. {} is replaced by each file path.
find . -name "*.bak" -exec rm -i {} \;
  • -exec ... \; runs the command once per file; -i prompts before each deletion.

Always run the search without -delete or -exec first, read the list, and only then add the action.


7. Combine Conditions

find . -type f -name "*.sh" -o -name "*.bash"
  • -o means OR: the command prints files matching either pattern.
find . -type f -size +1M ! -name "*.zip"
  • ! negates a test: files larger than 1 MB that are not zip archives.
find . \( -name "*.tmp" -o -name "*.bak" \) -delete
  • Parentheses must be escaped so the shell passes them to find; they group the OR conditions.

Common Pitfalls

  • Forgetting to quote patterns. find . -name *.md lets the shell expand *.md first and find errors or misbehaves. Always write -name "*.md".
  • Using -delete without testing. Run the search alone first; -delete cannot be undone.
  • find can be slow on huge trees. Add -maxdepth or search a narrower path when scanning / or /home.
  • -name vs -path. -name matches only the final component; use -path when the directory part matters, e.g. -path "*/src/*".
  • Permissions errors. On system directories, prefix with sudo or accept that some subdirectories are skipped.

CommandDescription
locateFast filename search via a prebuilt index
lsList directory contents
grepSearch file contents, not filenames
treeDraw a directory tree
duReport disk usage, useful with -size results

find Cheatsheet

CommandDescription
find . -name "*.txt"Find files by name pattern
find . -iname "*.txt"Case-insensitive name match
find . -type fRegular files only
find . -type dDirectories only
find . -type lSymbolic links only
find . -maxdepth 2Limit recursion depth
find . -size +100MFiles larger than 100 MB
find . -mtime -7Modified in the last 7 days
find . -mtime +30Modified more than 30 days ago
find . -newer fileModified after file
find . -name "*.tmp" -deleteDelete matches (test first!)
find . -exec CMD {} +Run CMD on all matches
find . -exec CMD {} \;Run CMD once per match
find . -name a -o -name bOR two conditions
find . ! -name "*.zip"Negate a condition