find is a general purpose file/directory search tool. It combines
various filters (such as comparing by modified time, executable files,
types, and more) along with command execution. Create powerful
pipelines by combining
find with other commands. Here are some
# Recursively find all directories in the current directory $ find . -type d # Recursively find all files in the current directory $ find . -type f # Pipe find to xargs $ find . -type -f -print0 | xargs -0
find is a powerful command with many options and features via
expressions. Expressions include tests, actions, global options,
positional arguments, and operators. Tests return true/false. Actions
have side effects. Global options apply to everything in an
expression. Positional options only affect tests or actions which
follow them. Operators join other items in expressions.
Let’s unpack the examples.
find . -type d uses the
-type returns true if the
file matches the given type. Common types are
d for directory,
for file, or
l for symbolic links.
find assume the
-type f to print
all files. The third example uses the
-print0 action. This prints
null terminated output for use combinations with
xargs -0. These
xargs place nice when files names include
delimiting characters (such as whitespace).
Naturally there are more tests than just
-type. Let’s cover the use
cases before diving deeper into expressions and examples.
find is a general purpose file and directory search. It is useful in
shell programs with conditional logic (e.g. match this or that) or
to recursively find everything in a set of directories. Generally
is used for simple directory listings, while
find is for searching
and some form of batch processing.
BSD vs GNU
Here’s a summary of important differences between the GNU and BSD versions. This is not exhaustive, instead it is optimized for things you may encounter in day-to-day use. Refer to the BSD find manual and GNU find manual for complete information.
-depth Noption. Both versions support
-depthwithout a value. This triggers depth first traversal in both cases. BSD
-depth Ntest if the file/directory is depth
Nfrom the initial traversal point. GNU
finddoes not support
-permfor similar functionality. GNU
findeffectively has shorthands for these common permissions.
-flags, with both BSD and GNU find support
-permtest permissions (read/write/execute).
-flagsmaps to BSD specific file system flags. Refer to the (Free)BSD chflags manual for more information.
-Xoption to skip files names with delimiting characters. This option intends to make
findwork correctly with
xargs(which would break normally). Both BSD and GNU find support
-print0which makes all files play nicely with
- Both versions support
-Efor extended regex support. However the exact regex syntax varies between BSD and GNU.
-O LEVELfor query optimizations. BSD
findhas no such option. Refer to the GNU find manual for more information.
find -scause find to traverse the file hierarchies in lexicographical order, i.e., alphabetical order within each directory. Note that
find | sortmay give different results. GNU
findhas no such option.
find -D OPTSmanages debug options. Refer the GNU find manual for more information. BSD
findhas no such option.
- Both BSD and GNU
findincludes compatible options to skip files that are on different file systems than the original traversal point. Both versions implement
-mount, while BSD
find, uses a global
-xoption in favor of a depcreated
finddoes not support the
-xoption, but does support
-printfaction for formatting output. BSD
findincludes no similar feature.
- BSD “primaries” refer to GNU tests, actions, global options, and positional options.