Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

6 Commits

Repository files navigation

find (macOS / bash)

On macOS, find is the BSD implementation (not GNU). Most examples here work the same way; common differences: no -printf (GNU); -size accepts a c suffix for bytes. See man find for full details.

Demo scripts

The scripts/ directory contains runnable examples that create a temporary tree, run find, and print the results:

cd scripts
./run-all.sh

Files: 01-empty-and-types.sh, 02-name-and-quotes.sh, 03-depth-prune.sh, 04-time-size-perm.sh, 05-exec-print0.sh.


Empty files

find <PATH> -type f -empty

Empty directories (no entries):

find <PATH> -type d -empty

Find by name (file or directory)

Pattern matches the file name (not the full path). Metacharacters *, ?, and [] should be quoted so the shell passes them to find instead of expanding them first.

find . -type f -name '*<NAME>*.ppt'

Find directories whose names match a pattern (recommended: put -type before -name):

find . -type d -name '*<DIR_NAME>*'

Wrong (without quotes the shell expands * before find runs; this can error or give unexpected results):

find . -type d -name *<DIR_NAME>*

All files under the current directory (no need for -name "*"):

find . -type f

Case-insensitive name match:

find . -type f -iname '*.pdf'

Match the full path (useful for directory segments):

find . -path '*/node_modules/*' -prune -o -type f -print

(The -prune pattern skips entire branches; -o ... -print is the usual BSD find idiom.)


Quick predicate reference

OptionShort meaning
.Starting directory for the walk (here, the current one).
-type f / -d / -lRegular file, directory, symbolic link.
-name / -inamePattern on the base name (fnmatch).
-emptyEmpty file or directory.
-maxdepth n / -mindepth nDepth limits (supported on macOS).
-mtime -1Modified less than 24 hours ago (whole days).
-mmin -30Modified less than 30 minutes ago.
-size +1MLarger than 1 megabyte (k, M, G; c = bytes).
-perm -100Owner execute bit set.

Time and size

find . -type f -mmin -60
find ~/Downloads -type f -size +100M

Depth and skipping directories

Only the starting directory (no descendants):

find . -maxdepth 1 -type f

Skip node_modules while listing .js files:

find .\( -path ./node_modules -o -path './node_modules/*'\) -prune -o -type f -name '*.js' -print

(Adjust ./node_modules if your search root is not ..)


Actions: list, run commands, delete with care

One command invocation per match ({} is the path; \; ends -exec):

find . -type f -name '*.txt' -exec wc -l {} \;

Batch arguments into fewer command runs (trailing +):

find . -type f -name '*.txt' -exec grep -l 'TODO' {} +

Paths with spaces: NUL-terminated output and safe handling in bash:

while IFS= read -r -d '' f;do ls -la "$f";done<<(find . -type f -print0)

You will also often see:

find . -type f -print0 | xargs -0 ls -la

Danger: deletes bypass the Trash. Verify with -print first:

find /some/tmp -type f -name '*.log' -print
# find /some/tmp -type f -name '*.log' -delete

Logical operators

find . -type f \( -name '*.png' -o -name '*.jpg'\)

Summary

find combines tree walking, predicates (-type, -name, -mtime, -size, …), and actions (default -print, or -exec, -delete, etc.). It is the standard macOS tool for locating files under complex rules without proprietary extensions.

About

Snippets for find command. Bash programming

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages