Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. find

find

Since: All Linux
macOS(2001 Cheetah)
Bash 1.0(1989)

find is a command that recursively searches a directory tree to locate files. You can filter results by name, type, modification time, size, and more. It also has a powerful feature that lets you run a command on each file found.

Syntax

find starting-path [options] [conditions] [actions]

Options and Conditions

Option / ConditionDescription
-name "pattern"Searches for files whose names match the pattern (wildcards are supported).
-iname "pattern"Searches for files by name, case-insensitively.
-type fMatches regular files only.
-type dMatches directories only.
-type lMatches symbolic links only.
-mtime nMatches files modified n days ago. Use +n for "more than n days ago" and -n for "within the last n days".
-newer fileMatches files newer than the specified file.
-size n[cwbkMG]Filters by file size (e.g., -size +1M matches files larger than 1 MB).
-maxdepth nLimits the search to n levels deep.
-mindepth nSkips entries shallower than n levels deep.
-emptyMatches empty files or directories.
-exec command {} \;Runs the specified command on each matched file individually.
-exec command {} +Passes all matched files to the command at once (more efficient).
-deleteDeletes each matched file.
! conditionNegates the condition.

Sample Code

The following examples use this directory structure.

📁 ~/project/ 📁 docs/ 📄 readme.txt 📁 src/ 📄 app.php 📄 helper.php 📁 tmp/ 📄 cache.tmp 📄 debug.tmp 📁 notes/ 📄 todo.txt 📄 demo.mp4 (5MB) 📄 archive.tar.gz (2MB)

Search for all .txt files under the current directory.

find . -name "*.txt"
./docs/readme.txt
./notes/todo.txt

List directories only.

find . -type d
.
./.git
./docs
./src
./tmp
./notes

Search for files larger than 1 MB.

find . -type f -size +1M
./demo.mp4
./archive.tar.gz

Search for files modified within the last 7 days.

find . -type f -mtime -7

Show detailed information for each found file using $ ls -lh.

find . -name "*.php" -exec ls -lh {} \;
-rw-r--r-- 1 user staff 4.2K Mar  5 10:00 ./src/app.php
-rw-r--r-- 1 user staff 1.8K Mar  5 09:30 ./src/helper.php

Delete all .tmp files.

find . -name "*.tmp" -delete
ls tmp/

Search for .php files while excluding the .git directory.

find . -name ".git" -prune -o -name "*.php" -print
./src/app.php
./src/helper.php

List empty files.

find . -empty -type f

Limit the search depth. Search only the immediate contents of the current directory (1 level deep).

find . -maxdepth 1 -name "*.txt"

Common Mistakes

Common Mistake 1: Looping over find results fails with filenames that contain spaces

Using for f in $(find ...) splits results on spaces, so filenames with spaces are broken into separate items.

for f in $(find . -name "*.txt"); do echo "$f"; done
./my
file.txt
("my file.txt" was split into two items)

Use while IFS= read -r with -print0 and -0 to handle any filename safely.

find . -name "*.txt" -print0 | while IFS= read -r -d '' f; do echo "$f"; done
./my file.txt

Common Mistake 2: Omitting -type also matches directories

Without -type f, find returns both files and directories matching the name pattern.

find . -name "config"
./config
./src/config
(./config may be a directory)

Add -type f to match only regular files.

find . -type f -name "config"
./src/config

Notes

While '$ find' is extremely powerful, when conditions become complex, piping the results through '|' to xargs is often simpler than using '-exec'.

To search file contents for text, use '$ grep -r' instead of '$ find'. For listing files, also see ls.

If you find any errors or copyright issues, please .