find

BSD Apple OS X (Darwin)

see GNU / Linux version

find [-P | -L | -H] [-EXdsx] [-f pathname] [pathname …] expression

Traverse a file hierarchy beginning at pathnames and select files matching a criteria, outputting or executing a command with the matching file names as an argument.
The criteria is an expression, composed of the primaries and operands .

Primaries which take a numeric argument, match when equal unless
preceeded with a plus (+) meaning more than n or
                       minus (-) less than n

symbolic link handling:
-P information is of the link ( Pointer ). default.
-L file Linked to information is used
-H symbolic links        on the command is information for those of the files referenced by the link.
symbolic links not on the command is that of the link .
If the referenced file does not exist, information will be for the link .
-E -regex and -iregex are extended regular expressions
-X names containing apostrophe ( ' ), quotes ( " ), backslash (\), space, tab and newline (which are delimiters used by xargs) are skippend and a diagnostic message is displayed on standard error.
-d
-depth
depth-first traversal, i.e., directories are visited in post-order and all entries in a directory will be acted on before the directory itself.
Useful when used with cpio to process files that are contained in directories with various permissions.
Ensures write permission while placing files in a directory, then sets the directory's permissions .
Always true
dir1
  file11
  dir2
   file21
   file22  
  file2
order is file11, file21,file22,file2, dir2, dir1
default: visit directories before their contents not a breadth-first traversal.
-f pathnames. May also be specified as the operands following options.
-s traverse the file hierarchies in lexicographical order, i.e., alphabetical order within each directory.
find -s and find | sort may give different results.
-x prevents descending into directories that have a device number different than that of the file from which the descent began.
eXclude volume crossing .

PRIMARIES

file time-stamp oriented

Relative times are base on time find was started.
+ more than xxx (before), - less than xxx (more recently than, since)
w 7 Days, d 24 hours, h 60 minutes, m 60 seconds and s second. Units may be combined, for example: 1h30m.
i.e. 1 (day is default) means before yesterday at this time, not anytime yesterday.
For example running find . -mtime -1 this afternoon will NOT find files modified yesterday morning!

To find files modified anytime since yesterday use: find . -mtime -`date +1d%Hh%Mm%Ss`

current file was:
-Bmin [±]m Born (created) … m minutes ago
-Btime [±]n[wdhms] Born n wdhms ago. Default d days! (i.e. n*24 hours
-Bnewer file Born before file was modified.
-amin [±]m accessed            access times may not be enabled see
-atime [±]n[wdhms]
-anewer file
-cmin [±]mstatus information
changed
-ctime [±]n[wdhms]
-cnewer file
-mmin ±mmodified
-mtime [±]n[wdhms]
-mnewer file
 -newer file
-newerct ccyy-mm-dd

current file has a more recent

c
access
change
modification

time than ccyy-mm-dd
date of the form understood by cvs

-newercf file True if the
current file has a more recent access (a.)
change(c.)
modification(m.)
time than file's
access (.a)
change (.c)
modification (.m)

Example: find . -newermm a.out # find files modified before a.out

-empty True if the current file or directory is empty.
execution options
-exec util [args …] {} \; {} is replaced by the pathname of the current file.
True if util returns zero.

The expression must be terminated by a semicolon (;) escaped with a \.
util will be executed from the directory from which find was executed.
util and args are not subject to the further expansion of shell patterns and constructs.

-execdir util [args …] {} \; util will be executed from the directory of the current file
-exec util [args …] {} + {} is replaced with as many pathnames as possible for each invocation of util like xargs
-execdir util [args…] {} +
-ok util [args … ] {} \; Requests user affirmation for the execution of util.
y executes the command, anything else skips the command and returns false.
/usr/bin/find . -iname "*~" -ok rm {} \;
"rm ./Applications/Album/CHRIS.HTM~"? n
"rm ./Documents/JackTheRiperCracker.html~"? n
"rm ./Documents/main.java~"? y
"rm ./Documents/MSU/585-01-security/Project/1-intro.txt~"? 
-okdir util {} [args …];
-perm [±]modeTests file permissions.
mode may be symbolic (see chmod) or octal .

-mmm true if   all bits in mmm are set in the file's mode bits.
+mmm true if any bits in mmm are set in the file's .
 mmm true if the bits in mmm match the file's .
If symbolic, a starting value of 000 and the mode sets or clears permissions (ignoring the process' file mode creation mask.
If octal, only bits 07777 (SUID | SGID | STXT | U | G | O) of the file's mode bits participate in the comparison.
i.e. q, zzz is ignored
-flags [±]flags,notflags flags are specified using symbolic names (see chflags)
archived , opaque , nodump , sappend , uappend , schange, simmutable , uchange, uimmutable , hidden .
Those with the "no" prefix are notflags (except nodump).
   flags are checked to be set
notflags are checked to be reset.

True if the bits in flags exactly match the file's flags bits, and none of the flags bits match those of notflags.

- flags: true if at least all of the bits in flags and none of the bits in notflags are set in the file's flags bits.

+ flags: true if any of the bits in flags is set in the file's flags bits, or
                     any of the bits in notflags is not set in the file's flags bits.

Different from -perm, which only allows specifing permission bits(mode) that are set.

-fstype type True if the file is type.
> lsvfs 
Filesystem  Refs Flags
---------------- ----
nfs           0 
devfs         1 
nullfs        0 
hfs           3 local
apfs          3 local
autofs        2 
smbfs         1 

pseudo-types:
local matches any file system physically mounted on the system where the find is being executed
rdonly matches any file system which is mounted read-only.

-group group True if the file belongs to group name or ID.
-links n True if the file has n links.
-inum n True if the file has inode number n.

-maxdepth n True if the depth of the current file into the tree is less than or equal to n.
-mindepth n pattern.
Special shell pattern matching characters ([, ], *, and ?) may be used as part of pattern.
These characters may be matched explicitly by escaping them with a backslash (\).
-iname patternthe match is case insensitive.
-nogroup True if the file belongs to an unknown group.
-nouser True if the file belongs to an unknown user.

-path pattern True if the pathname matches pattern.
shell pattern matching characters ([, ], *, and ?) may be used as part of pattern, in which case the entire pattern should be enclosed in "
To match these characters in the path escape them with a backslash (\).
Slashes (/) are treated as normal characters and do not have to be matched explicitly.

-ipath pattern Like -path, case insensitive.
-regex pattern True if the whole path of the file matches pattern using regular expression.
To match ./foo/xyzzy, use: .*/[xyz]* or .*/foo/.*,
not xyzzy or /foo/.
-iregex pattern Like -regex, but is case insensitive.
-size n [ckMGTP] True if the file's size, rounded up, in 512-byte blocks is n, If n is followed by a
c, size is characters, kilobytes, Mega, Giga, Tera, Peta.
-type t True if the file is of the type.
d directory c character special
f regular file b block special
l symbolic link
p FIFO s socket
-user uname True if the file belongs to the user uname. If uname is numeric and there is no such user name, then uname is treated as a user ID.
primaries which are always true affect output
-print Outputs the pathname of the current file to standard output. If none of -exec, -ls, -print0, or -ok is specified, the given expression shall be replaced by ( expression ) -print.
-print0 Outputs the pathname of the current file followed NULL .
Useful when filenames contain or spaces or other special characters espically with xargs
-ls To STDOUT, The format is as per ls -dgils.
inode  blks permissions hard owner group bytes modification  pathname
                        link
                       count
1151651  32 -rwx------  1 userme mygrp   1638 May  9 2007 ./ds/archive/asts.xls
3147130 360 -rwx------  1 userme mygrp  18278 Jan 27 2008 ./ds/asts-0801.xls
3440935 552 -rwx------  1 userme mygrp  28006 Jul 13 2008 ./ds/asts-0807.xls
2876708 368 -rwx------  1 userme mygrp  18585 Jan 21 2009 ./ds/asts-0810.xls
4666431 360 -rwx------  1 userme mygrp  18380 Dec 20 2008 ./ds/asts-0812.xls
9067172 528 -rwx------  1 userme mygrp  26828 Dec  9 2007 ./ds/asts-3yrs.xls
9933738 664 -rwx------  1 userme mygrp  33689 Dec  9 2007 ./ds/asts-allyrs.xls 
To removed everything before the bytes use:
sed "s/^[[:digit:]]\{1,18\} \{1,9\}[[:digit:]]\{0,6\} .......... \{0,5\}[[:digit:]]\{0,4\} [[:alpha:]]\{1,7\}  [[:alpha:]]\{1,7\} //"
For block or character special file, the major and minor numbers will be displayed .
For symbolic link, the pathname of the linked-to file will be displayed preceded by ->.
-delete Delete found files and/or directories.
This executes from the current working directory as find recurses down the tree.
It will not delete a filename with a / in its pathname relative to . for security reasons.
Depth-first traversal processing is implied by this option.
-prune Don't descend into the current file.
Ignored with -d (Depth first)

OPERATORS

Primaries may be combined using these operators, listed in order of decreasing precedence.

\( expression \) evaluates to true if the parenthesized expression evaluates to true.

\! expression
-false expression
-not expression

expression -and expression
expression expression

default opeator. The second expression is not evaluated if the first expression is false.

expression -or expression The second expression is not evalated if the first expression is true.

Operands and primaries must be separate arguments to find.
Primaries which take arguments expect each argument to be a separate argument .

Examples

Names do not end in .c. find mysrclib \! -name "*.c" -print

Newer than ttt and owned by user wnj find mysrclib -newer ttt -user wnj -print

Are not both newer than ttt and owned by wnj. find mysrclib \! \( -newer ttt -user wnj \) -print

Either owned by wnj or newer than ttt. find mysrclib \( -newer ttt -or -user wnj \) -print

Recent than the current time minus one minute. /usr/bin/find . -newerct '1 minute ago' -print whose inode change time is more

See Also

chflags, chmod, cvs, locate, whereis, which, stat, fts, getgrent, getpwent, strmode, re_format, psymlink .

Standards

Single character options as well as -iname, -inum, -iregex, -print0, -delete, -ls, and -regex are extensions to IEEE Std 1003.2 (POSIX.2).

Historically -d, -h and -x were implemented using -depth, -follow, and -xdev.
These are evaluated to true.
As they were global variables that took effect before the traversal began, some legal expressions could have unexpected results.
An example is the expression -print -o -depth.
As -print always evaluates to true, the standard order of evaluation implies that -depth would never be evaluated. This is not the case.

The -or was implemented as -o, and -and was implemented as -a.

of the -exec and -ok did not replace {} in the utility name or the utility arguments if it had preceding or following non-whitespace characters. This version replaces it no matter where in the utility name or arguments it appears.

The -E was implemented on the analogy of grep(1) and sed(1).

BUGS The special characters used by find are also special characters to many shell programs. In particular, the characters *, [, ], ?, (, ), !, \ and ; may have to be escaped from the shell.

As there is no delimiter separating options and file names or file names and the expression, it is difficult to specify files named -xdev or !. These problems are handled by the -f option and the getopt(3) -- construct.

STANDARDS a superset of the syntax specified by the IEEE Std 1003.2 (POSIX.2) standard.
single character options and -iname, -inum, -iregex, -print0, -delete, -ls, and -regex primaries are extensions to IEEE Std 1003.2 (POSIX.2).

-d, -h and -x were implemented using the primaries -depth, -follow, and -xdev and evaluated true. As global variables that took effect before the traversal began, some expressions could have unexpected results. An example -print -o -depth. As -print evaluates to true, depth would not be evaluated. This is not the case.

-or was implemented as -o, and the operator -and was implemented as -a.

In some implementations -exec and -ok did not replace {} if it had preceding or following non-whitespace .
This version replaces it whereever utility name or arguments .

-E was implemented on the analogy of grep(1) and sed(1).

Reminder: *, [, ], ?, (, ), !, \ and ; may have to be escaped from the shell.

There is no delimiter separating options, names and expression, making it difficult to specify files named -xdev or !.
Use -f and the getopt -- construct.

-delete does not interact well with other options that cause the filesystem tree traversal options to be changed.

-mindepth and -maxdepth are actually global options (as documented above). They should probably be replaced by options which look like options.

BSD May 3, 2001 +2006