find [path …] [expression …] This is the man page for the GNU version 4.5.11 BSD version ⌘
Default path is the current directory
Default expression is -print

expression may be composed of:

  • options: affect overall operation (rather than the processing of a specific file) . Example:(-xdev)
  • tests: -iname, -mmin, -size,…
  • actions:   separated by operators return true or false

    example: ( -ls -exec rm {} \;)

    options

    at the beginning of the expression.

    -depth Process files in directory before the directory itself.
    -prune do not descend the current directory; true. With -depth, false; no effect.
    -maxdepth lev  Descend at most lev of directories below the path … .
    0 only apply the tests and actions to path ….
    -mindepth lev Do not apply any tests or actions at levels less than lev
    1 i.e. process all files except the path ….
    -noleaf Needed when searching non-Unix filesystems, such as CD-ROM or MS-DOS or AFS.
    Each directory on a Unix filesystem has at least 2 hard links: its name and its . entry.
    Its subdirectories each have a .. entry linked to that directory.
    -follow DEreference symbolic links. Implies -noleaf.
    ‑xdev , ‑mount Don't descend directories on other filesystems.
    -daystart Measure times from the beginning of today rather than from 24 hours ago.
    (see -amin, -atime, -cmin, -ctime, -mmin, and -mtime)
    -version
    -help

    tests

    Numeric arguments can be specified as +n for greater than, -n for less than.

    -name pattern Filename matches pattern.
    The metacharacters (*, ?, and []) do not match a . at the start of the base name.
    (i.e. find ~ -name "*rc" does not find .bashrc).
    To ignore a directory and the files under it, use -prune:

    find . -path ./src/emacs -prune -o -print

    /usr/bin/find . -name 2011-08-\*

    -iname pattern case insensitive. ex: "fo*" or "F??" matches Foo, FOO, foo, fOo, etc.
    -regex pattern File name matches regular expression pattern.
    This is a match on the whole path, not a search.
    For example, to match a file named fubar3 use the regular expression bar. or b.*3 but not b.*r3
    -iregex pattern case insensitive.

    Time related options

      Numeric arguments of time are specified as
    • +n older than
    • -n for newer than
    • omitting the sign indicates equal to ( and must match exactly )

    minutes since
    hours is minutes.

    Find files modified a long time ago and format the output of find's ls to look more like ls -l

    /usr/bin/find . -mmin -523709 -and -mmin +479000 -ls |\
       sed "s/^[[:digit:]]\{1,9\} \{1,9\}[[:digit:]]\{1,6\} .......... \{1,5\}[[:digit:]]\{1,4\} [[:alnum:]]\{1,8\} \{1,10\}[[:alnum:]]\{1,8\} \{1,10\}//"|
       sort -n|tail

    -daystart Measure times from the beginning of today rather than from 24 hours ago.
    Data Modified
    -mmin ±mmm data was last modified mmm minutes ago.
    Use -mmin -10 for less than 10 minutes ago (i.e. after 10 minutes ago, -30 shows more files.
    -mtime ±ddd ddd*24 hours ago (days ago).
    Remove files (and direcrories) not modified in the last 2 years
    find . -not -mtime -730 -exec rm -r {} \;
    -newer file more recently than file.
    affected by -follow only if -follow comes before -newer on the command line.

    Example: Find, beginning in the parent/parent directory ,
    only files, newer than DBtables.cgi and
    do an ls supressing the group and cutting off the mode-flags and the owner.
    find ../.. -type f -newer DBtables.cgi -exec /bin/ls -lg {} \; |cut -c21-

    Accessed (Not all systems update the time files are accessed see mount -o noatime )
    -amin ±mmm accessed exactly mmm minutes ago.
    -amin -5 for files accessed in last 5 mintes.
    +4464 lists files not accesses in last 31 days.
    -anewer file more recently than file was modified.
    affected by -follow only if -follow comes before -anewer on the command line.
    -atime ±ddd ddd*24 hours ago (days ago ) .
    Status changed
    -used ±ddd ddd days after its status was last changed.
    -cmin ±ddd status was last changed ddd minutes ago.

    Example, starting at the current directory, find files, larger than 6MB created recently (i.e. within last 8 hours) ( if -type f is omitted ls will follow directories)
    an cut off the mode flags and owner.

    /usr/bin/find . -type f -size +12000 -cmin -480 -exec /bin/ls -lg {} \; |cut -c20-

    -cnewer file more recently than file was modified.
    affected by -follow only if -follow comes before -cnewer on the command line.
    -ctime ±ddd ddd*24 hours ago (days ago) .
    Numeric arguments are specified as +n for greater than, -n for less than, ommitting the sign indicates equal to.
    -size ±nc + (more) or - (less) than nb blocks(default), nkilobytes, ncharacters , or 2-byte words nw.
    -empty Well, if the file is empty
    -path pattern name matches shell pattern pattern.
      The metacharacters do not treat / or . specially; so, for example,
      find . -path ./sr*sc
      will output an entry for a directory called ./src/misc .
      To ignore a whole directory tree, use -prune rather than checking every file in the tree.
      For example, to skip the directory src/emacs and all files and directories under it, and output the names of the other files found:
      find . -path ./src/emacs -prune -o -print
    -ipath pattern match is case insensitive.

    -perm mode File's permission bits are exactly mode (octal or symbolic).
    Symbolic modes use mode 0 as a point of departure.
    -perm -mode All of the permission bits mode are set for the file.
    -perm +mode Any ...

    -false
    -true

    -fstype type filesystem of type type, including ufs, 4.2, 4.3, nfs, tmp, mfs, S51K, S52K.
    Use -printf with the %F directive to see the types of your filesystems.

    -type t block (buffered) special, character (unbuffered) special, directory, named pipe (FIFO), regular file, symbolic link, socket , door (Solaris)
    -xtype t For symbolic links:
    • if -follow has not been given, true if the file is a link to a file of type t;
    • if -follow has been given, true if t is l.
    In other words, for symbolic links, -xtype checks the type of the file that -type does not check.
    -uid n owned by user n.
    -user uname owned by user uname (numeric user ID allowed).
    -nouser owner no longer exists, i.e. No /etc/passwd record
    -gid n numeric group ID is n.
    -group gname belongs to group gname (numeric group ID allowed).
    -nogroup group no longer exists.
    -inum n has inode number n.
    -links n has n links.
    -lname pattern symbolic link whose contents match pattern .
    The metacharacters do not treat / or . specially.
    -ilname pattern case insensitive.

    actions

    -print is the default action on all files for which expression is true.

    -exec command [ args ] \;

    -ok command ; Like -exec but asks first. If the response does not start with y or Y, the command is not executed, and returns false.

      /usr/bin/find . -iname temp.\* -ok rm {} \;

    -ls list filenames in ls -ilds format with on STDOUT. size in K.
    Returns true

    -fls file true; like -ls but write to file like -fprint.

    ( all prints return true)

    -print
    output the full file name followed by a newline.
    -print0
    filename followed by a null .
    Useful if file name might contain \n or spaces or other special characters .
    Useful with xargs
    Specifiy Field widths and precisions.
    -fprint ofile output the full file name to ofile.
    Does not add a newline at the end of the string (so use \n at the end of the specs).
    Interprets \ escapes and % as directives.
    -fprint0 ofile
    -printf "specs" output specs -fprintf ofile "specs"

    escapes:

    \a alert bell \b Backspace (repositions cursor over previous char)
    \c Stop and flush the output
    \f Form feed \n Newline \r Carriage return
    \t Horizontal tab \v Vertical tab
    \\ backslash %% percent sign
    \nnn The character whose ASCII code is nnn (octal)
    A \ followed by any other character is treated as an ordinary character, both are output

    directives:

    %a access time
    Sun Jun 12 13:36:18 2005
    %Ak format specified by k
    either fc or a directive for the C strftime function. "%AT"
    13:36:18


    %c last status change time.%Ck formated
    %t modification time %Tk formated

    formatting codes fc

    Time fields:

    r 12-hour hh:mm:ss [AP]M      T 24-hour hh:mm:ss
    X H:M:S Z time zone
    H hour 00..23 h 01..12 k 0..23 l 1..12
    M minute 00..59 p AM or PM S seconds 00..61
    @ seconds since Jan. 1, 1970, 00:00 GMT.

    Date fields:

    D mm/dd/yy x mm/dd/yy
    c Sat Nov 04 12:02:33 EST 1989
    a Sun..Sat A Sunday..Saturday w day of week 0..6
    b Jan..Dec h B January..December(variable length
    m month 01..12 d day of month 01..31
    y 00..99 Y 1970...
    j day of year 001..366
    U week number of year with Sunday
         as first day of week 00..53
    W week number of year with Monday

    Directives

    %k size in 1K , %b in 512-byte blocks , %s in bytes.
    %d depth in the directory tree; 0 i.e. the file is a command line argument.
    %h Leading directories of file's name (all but the last element).
    %f name with any leading directories removed (only the last element).
    %p name.
    %P File's name with the name of the command line argument under which it was found removed.
    %H Command line argument under which file was found.

    %F Type of the filesystem the file is on; this value can be used for -fstype.
    %g group name, or numeric group ID if the group has no name.
    %G numeric group ID.
    %i inode number
    %l Object of symbolic link (empty string if not a symbolic link).
    %m permission bits (in octal).
    %n Number of hard links to file.
    %u owner, or numeric user ID if the user has no name. %U numeric user ID.
    A % character followed by any other character is discarded (the other character is output).

    OPERATORS

    All options return true
    tests and actions return true or false. Listed in order of decreasing precedence:
    Default operator is -and .

    \( expr \)

    \! expr false if expr is true.
    -not

    expr1 -and expr2 expr2 is not evaluated if expr1 is false.
    expr1 -a expr2
    expr1 expr2 And (implied)
    expr1 , expr2 List; both expr1 and expr2 are always evaluated.

    expr1 -o expr2    Or; expr2 is not evaluated if expr1 is true.
    expr1 -or expr2    The value of expr1 is discarded.    The value of the list is the value of expr2.

    SEE ALSO locate(1L), locatedb(5L), updatedb(1L), xargs(1L)

    bash -d, -f

    if [ -d $DirOrFile ] ; then echo "$DirOrFile is a directory"; fi
    find.sed
    s/^[[:digit:]]\{1,9\} \{1,9\}[[:digit:]]\{1,6\} .......... \{1,5\}[[:digit:]]\{1,4\} [[:alnum:]]\{1,8\} \{1,10\}[[:alnum:]]\{1,8\} \{1,10\}//

    on Mac OS X , search the spotlight database instead of the directory (Much faster)

    use locate to search the database of files that existed when updatedb was last run.

    Current GNU version is 2014-07-19 html man Release findutils-4.5.14. maintained by James Youngman jay@gnu.org