1.\" The Regents of the University of California. All rights reserved. 2.\" 3.\" This code is derived from software contributed to Berkeley by 4.\" the Institute of Electrical and Electronics Engineers, Inc. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)find.1 8.7 (Berkeley) 5/9/95 35.\" $FreeBSD$ 36.\" 37.Dd May 9, 1995 38.Dt FIND 1 39.Os 40.Sh NAME 41.Nm find 42.Nd walk a file hierarchy 43.Sh SYNOPSIS 44.Nm 45.Op Fl H | Fl L | Fl P 46.Op Fl EXdsx 47.Op Fl f Ar pathname 48.Op Ar pathname ...\& 49.Ar expression 50.Sh DESCRIPTION 51.Nm Find 52recursively descends the directory tree for each 53.Ar pathname 54listed, evaluating an 55.Ar expression 56(composed of the ``primaries'' and ``operands'' listed below) in terms 57of each file in the tree. 58.Pp 59The options are as follows: 60.Pp 61.Bl -tag -width Ds 62.It Fl E 63Interpret regular expressions followed by 64.Ic -regex 65and 66.Ic -iregex 67options as extended (modern) regular expressions rather than basic 68regular expressions (BRE's). The 69.Xr re_format 7 70manual page fully describes both formats. 71.It Fl H 72The 73.Fl H 74option causes the file information and file type (see 75.Xr stat 2 ) 76returned for each symbolic link specified on the command line to be 77those of the file referenced by the link, not the link itself. 78If the referenced file does not exist, the file information and type will 79be for the link itself. 80File information of all symbolic links not on 81the command line is that of the link itself. 82.It Fl L 83The 84.Fl L 85option causes the file information and file type (see 86.Xr stat 2 ) 87returned for each symbolic link to be those of the file referenced by the 88link, not the link itself. 89If the referenced file does not exist, the file information and type will 90be for the link itself. 91.It Fl P 92The 93.Fl P 94option causes the file information and file type (see 95.Xr stat 2 ) 96returned for each symbolic link to be those of the link itself. 97This is the default. 98.It Fl X 99The 100.Fl X 101option is a modification to permit 102.Nm 103to be safely used in conjunction with 104.Xr xargs 1 . 105If a file name contains any of the delimiting characters used by 106.Xr xargs , 107a diagnostic message is displayed on standard error, and the file 108is skipped. 109The delimiting characters include single (`` ' '') and double (`` " '') 110quotes, backslash (``\e''), space, tab and newline characters. 111.It Fl d 112The 113.Fl d 114option causes 115.Nm 116to perform a depth\-first traversal, i.e. directories 117are visited in post\-order and all entries in a directory will be acted 118on before the directory itself. 119By default, 120.Nm 121visits directories in pre\-order, i.e. before their contents. 122Note, the default is 123.Ar not 124a breadth\-first traversal. 125.It Fl f 126The 127.Fl f 128option specifies a file hierarchy for 129.Nm 130to traverse. 131File hierarchies may also be specified as the operands immediately 132following the options. 133.It Fl s 134The 135.Fl s 136option causes 137.Nm 138to traverse the file hierarchies in lexicographical order, 139i.e., alphabetical order within each directory. 140Note: 141.Sq find -s 142and 143.So 144find | sort 145.Sc 146may give different results. 147.It Fl x 148The 149.Fl x 150option prevents 151.Nm 152from descending into directories that have a device number different 153than that of the file from which the descent began. 154.El 155.Sh PRIMARIES 156.Bl -tag -width Ds 157.It Ic -amin Ar n 158True if the difference between the file last access time and the time 159.Nm 160was started, rounded up to the next full minute, is 161.Ar n 162minutes. 163.It Ic -anewer Ar file 164Same as 165.Ic -neweram . 166.It Ic -atime Ar n 167True if the difference between the file last access time and the time 168.Nm 169was started, rounded up to the next full 24\-hour period, is 170.Ar n 17124\-hour periods. 172.It Ic -cmin Ar n 173True if the difference between the time of last change of file status 174information and the time 175.Nm 176was started, rounded up to the next full minute, is 177.Ar n 178minutes. 179.It Ic -cnewer Ar file 180Same as 181.Ic -newercm . 182.It Ic -ctime Ar n 183True if the difference between the time of last change of file status 184information and the time 185.Nm 186was started, rounded up to the next full 24\-hour period, is 187.Ar n 18824\-hour periods. 189.It Ic -delete 190Delete found files and/or directories. 191Always returns true. 192This executes 193from the current working directory as 194.Nm 195recurses down the tree. 196It will not attempt to delete a filename with a ``/'' 197character in its pathname relative to "." for security reasons. 198Depth\-first traversal processing is implied by this option. 199.It Ic -empty 200True if the current file or directory is empty. 201.It Ic -exec Ar utility Op argument ... ; 202True if the program named 203.Ar utility 204returns a zero value as its exit status. 205Optional arguments may be passed to the utility. 206The expression must be terminated by a semicolon (``;''). 207If the string ``{}'' appears anywhere in the utility name or the 208arguments it is replaced by the pathname of the current file. 209.Ar Utility 210will be executed from the directory from which 211.Nm 212was executed. 213.It Ic -execdir Ar utility Op argument ... ; 214The 215.Ic \&-execdir 216primary is identical to the 217.Ic -exec 218primary with the exception that 219.Ar Utility 220will be executed from the directory that holds 221the current file. 222The filename substituted for 223the string ``{}'' is not qualified. 224.It Ic -fstype Ar type 225True if the file is contained in a file system of type 226.Ar type . 227The 228.Xr sysctl 8 229command can be used to find out the types of filesystems 230that are available on the system: 231.Bd -literal -offset indent 232sysctl vfs 233.Ed 234In addition, there are two pseudo-types, ``local'' and ``rdonly''. 235The former matches any file system physically mounted on the system where 236the 237.Nm 238is being executed and the latter matches any file system which is 239mounted read-only. 240.It Ic -group Ar gname 241True if the file belongs to the group 242.Ar gname . 243If 244.Ar gname 245is numeric and there is no such group name, then 246.Ar gname 247is treated as a group id. 248.It Ic -inum Ar n 249True if the file has inode number 250.Ar n . 251.It Ic -links Ar n 252True if the file has 253.Ar n 254links. 255.It Ic -ls 256This primary always evaluates to true. 257The following information for the current file is written to standard output: 258its inode number, size in 512\-byte blocks, file permissions, number of hard 259links, owner, group, size in bytes, last modification time, and pathname. 260If the file is a block or character special file, the major and minor numbers 261will be displayed instead of the size in bytes. 262If the file is a symbolic link, the pathname of the linked\-to file will be 263displayed preceded by ``\->''. 264The format is identical to that produced by ``ls \-dgils''. 265.It Ic -mnewer Ar file 266Same as 267.Ic -newer . 268.It Ic -maxdepth Ar n 269True if the depth of the current file into the tree is less than or equal to 270.Ar n . 271.It Ic -mindepth Ar n 272True if the depth of the current file into the tree is greater than or equal to 273.Ar n . 274.It Ic -mmin Ar n 275True if the difference between the file last modification time and the time 276.Nm 277was started, rounded up to the next full minute, is 278.Ar n 279minutes. 280.It Ic -mtime Ar n 281True if the difference between the file last modification time and the time 282.Nm 283was started, rounded up to the next full 24\-hour period, is 284.Ar n 28524\-hour periods. 286.It Ic \&-ok Ar utility Op argument ... ; 287The 288.Ic \&-ok 289primary is identical to the 290.Ic -exec 291primary with the exception that 292.Nm 293requests user affirmation for the execution of the utility by printing 294a message to the terminal and reading a response. 295If the response is other than ``y'' the command is not executed and the 296value of the 297.Ar \&ok 298expression is false. 299.It Ic \&-okdir Ar utility Op argument ... ; 300The 301.Ic \&-okdir 302primary is identical to the 303.Ic -execdir 304primary with the same exception as described for the 305.Ic \&-ok 306primary. 307.It Ic -name Ar pattern 308True if the last component of the pathname being examined matches 309.Ar pattern . 310Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') 311may be used as part of 312.Ar pattern . 313These characters may be matched explicitly by escaping them with a 314backslash (``\e''). 315.It Ic -iname Ar pattern 316Like 317.Ic -name , 318but the match is case insensitive. 319.It Ic -regex Ar pattern 320True if the whole path of the file matches 321.Ar pattern 322using regular expression. To match a file named ``./foo/xyzzy'', you 323can use the regular expression ``.*/[xyz]*'' or ``.*/foo/.*'', but not 324``xyzzy'' or ``/foo/''. 325.It Ic -iregex Ar pattern 326Like 327.Ic -regex , 328but the match is case insensitive. 329.It Ic -newer Ar file 330True if the current file has a more recent last modification time than 331.Ar file . 332.It Ic -newerXY Ar file 333True if the current file has a more recent last access time ( 334.Ic X 335= 336.Ic a 337), change time ( 338.Ic X 339= 340.Ic c 341), or modification time ( 342.Ic X 343= 344.Ic m 345) than the last access time ( 346.Ic Y 347= 348.Ic a 349), change time ( 350.Ic Y 351= 352.Ic c 353), or modification time ( 354.Ic Y 355= 356.Ic m 357) of 358.Ar file . 359In addition, if 360.Ic Y 361= 362.Ic t , 363then 364.Ar file 365is instead interpreted as a direct date specification of the form 366understood by 367.Xr cvs 1 . 368Note that 369.Ic -newermm 370is equivalent to 371.Ic -newer . 372.It Ic -nouser 373True if the file belongs to an unknown user. 374.It Ic -nogroup 375True if the file belongs to an unknown group. 376.It Ic -path Ar pattern 377True if the pathname being examined matches 378.Ar pattern . 379Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') 380may be used as part of 381.Ar pattern . 382These characters may be matched explicitly by escaping them with a 383backslash (``\e''). 384Slashes (``/'') are treated as normal characters and do not have to be 385matched explicitly. 386.It Ic -ipath Ar pattern 387Like 388.Ic -path , 389but the match is case insensitive. 390.It Xo 391.Ic -perm 392.Oo Fl Oc Ns Ar mode 393.Xc 394The 395.Ar mode 396may be either symbolic (see 397.Xr chmod 1 ) 398or an octal number. 399If the mode is symbolic, a starting value of zero is assumed and the 400mode sets or clears permissions without regard to the process' file mode 401creation mask. 402If the mode is octal, only bits 07777 403.Pf ( Dv S_ISUID 404| 405.Dv S_ISGID 406| 407.Dv S_ISTXT 408| 409.Dv S_IRWXU 410| 411.Dv S_IRWXG 412| 413.Dv S_IRWXO ) 414of the file's mode bits participate 415in the comparison. 416If the mode is preceded by a dash 417.Pq Dq - , 418this primary evaluates to true 419if at least all of the bits in the mode are set in the file's mode bits. 420If the mode is preceded by a plus 421.Pq Dq + , 422this primary evaluates to true 423if any of the bits in the mode are set in the file's mode bits. 424Otherwise, this primary evaluates to true if 425the bits in the mode exactly match the file's mode bits. 426Note, the first character of a symbolic mode may not be a dash (``\-''). 427.It Ic -flags Op Fl Ns Ar flags 428This primary evaluates to true if exactly those flags of the file are 429set which are also set using the specified 430.Ar flags 431(if these are not preceded by a dash (``\-''), 432or if they match the specified flags (if these are preceded by a dash). 433The 434.Ar flags 435are specified using symbolic names (see 436.Xr chflags 1 ) . 437Note that this is different from 438.Ic -perm , 439which only allows you to specify flags which are set. 440.It Ic -print 441This primary always evaluates to true. 442It prints the pathname of the current file to standard output. 443If none of 444.Ic -exec , 445.Ic -ls , 446.Ic -print0 , 447or 448.Ic \&-ok 449is specified, the given expression shall be effectively replaced by 450.Cm \&( Ns Ar given\& expression Ns Cm \&) 451.Ic -print . 452.It Ic -print0 453This primary always evaluates to true. 454It prints the pathname of the current file to standard output, followed by an 455.Tn ASCII 456.Tn NUL 457character (character code 0). 458.It Ic -prune 459This primary always evaluates to true. 460It causes 461.Nm 462to not descend into the current file. 463Note, the 464.Ic -prune 465primary has no effect if the 466.Fl d 467option was specified. 468.It Ic -size Ar n Ns Op Cm c 469True if the file's size, rounded up, in 512\-byte blocks is 470.Ar n . 471If 472.Ar n 473is followed by a ``c'', then the primary is true if the 474file's size is 475.Ar n 476bytes. 477.It Ic -type Ar t 478True if the file is of the specified type. 479Possible file types are as follows: 480.Pp 481.Bl -tag -width flag -offset indent -compact 482.It Cm b 483block special 484.It Cm c 485character special 486.It Cm d 487directory 488.It Cm f 489regular file 490.It Cm l 491symbolic link 492.It Cm p 493FIFO 494.It Cm s 495socket 496.El 497.Pp 498.It Ic -user Ar uname 499True if the file belongs to the user 500.Ar uname . 501If 502.Ar uname 503is numeric and there is no such user name, then 504.Ar uname 505is treated as a user id. 506.El 507.Pp 508All primaries which take a numeric argument allow the number to be 509preceded by a plus sign 510.Pq Dq + 511or a minus sign 512.Pq Dq - . 513A preceding plus sign means ``more than n'', a preceding minus sign means 514``less than n'' and neither means ``exactly n'' . 515.Sh OPERATORS 516The primaries may be combined using the following operators. 517The operators are listed in order of decreasing precedence. 518.Bl -tag -width (expression) 519.It Cm \&( Ar expression Cm \&) 520This evaluates to true if the parenthesized expression evaluates to 521true. 522.Pp 523.It Cm \&! Ar expression 524This is the unary 525.Tn NOT 526operator. 527It evaluates to true if the expression is false. 528.Pp 529.It Ar expression Cm -and Ar expression 530.It Ar expression expression 531The 532.Cm -and 533operator is the logical 534.Tn AND 535operator. 536As it is implied by the juxtaposition of two expressions it does not 537have to be specified. 538The expression evaluates to true if both expressions are true. 539The second expression is not evaluated if the first expression is false. 540.Pp 541.It Ar expression Cm -or Ar expression 542The 543.Cm -or 544operator is the logical 545.Tn OR 546operator. 547The expression evaluates to true if either the first or the second expression 548is true. 549The second expression is not evaluated if the first expression is true. 550.El 551.Pp 552All operands and primaries must be separate arguments to 553.Nm . 554Primaries which themselves take arguments expect each argument 555to be a separate argument to 556.Nm . 557.Sh EXAMPLES 558.Pp 559The following examples are shown as given to the shell: 560.Bl -tag -width findx 561.It Li "find / \e! -name \*q*.c\*q -print" 562Print out a list of all the files whose names do not end in ``.c''. 563.It Li "find / -newer ttt -user wnj -print" 564Print out a list of all the files owned by user ``wnj'' that are newer 565than the file ``ttt''. 566.It Li "find / \e! \e( -newer ttt -user wnj \e) -print" 567Print out a list of all the files which are not both newer than ``ttt'' 568and owned by ``wnj''. 569.It Li "find / \e( -newer ttt -or -user wnj \e) -print" 570Print out a list of all the files that are either owned by ``wnj'' or 571that are newer than ``ttt''. 572.It Li "find . -newerct '1 minute ago' -print" 573Print out a list of all the files whose inode change time is more 574recent than the current time minus one minute. 575.El 576.Sh SEE ALSO 577.Xr chflags 1 , 578.Xr chmod 1 , 579.Xr cvs 1 , 580.Xr locate 1 , 581.Xr whereis 1 , 582.Xr which 1 , 583.Xr stat 2 , 584.Xr fts 3 , 585.Xr getgrent 3 , 586.Xr getpwent 3 , 587.Xr strmode 3 , 588.Xr re_format 7 , 589.Xr symlink 7 590.Sh STANDARDS 591The 592.Nm 593utility syntax is a superset of the syntax specified by the 594.St -p1003.2 595standard. 596.Pp 597All the single character options as well as the 598.Ic -iname , 599.Ic -inum , 600.Ic -iregex , 601.Ic -print0 , 602.Ic -delete , 603.Ic -ls , 604and 605.Ic -regex 606primaries are extensions to 607.St -p1003.2 . 608.Pp 609Historically, the 610.Fl d , 611.Fl h 612and 613.Fl x 614options were implemented using the primaries ``\-depth'', ``\-follow'', 615and ``\-xdev''. 616These primaries always evaluated to true. 617As they were really global variables that took effect before the traversal 618began, some legal expressions could have unexpected results. 619An example is the expression ``\-print \-o \-depth''. 620As \-print always evaluates to true, the standard order of evaluation 621implies that \-depth would never be evaluated. 622This is not the case. 623.Pp 624The operator ``-or'' was implemented as ``\-o'', and the operator ``-and'' 625was implemented as ``\-a''. 626.Pp 627Historic implementations of the 628.Ic exec 629and 630.Ic ok 631primaries did not replace the string ``{}'' in the utility name or the 632utility arguments if it had preceding or following non-whitespace characters. 633This version replaces it no matter where in the utility name or arguments 634it appears. 635.Pp 636The 637.Fl E 638option was implemented on the analogy of 639.Xr grep 1 640and 641.Xr sed 1 . 642.Sh BUGS 643The special characters used by 644.Nm 645are also special characters to many shell programs. 646In particular, the characters ``*'', ``['', ``]'', ``?'', ``('', ``)'', 647``!'', ``\e'' and ``;'' may have to be escaped from the shell. 648.Pp 649As there is no delimiter separating options and file names or file 650names and the 651.Ar expression , 652it is difficult to specify files named ``-xdev'' or ``!''. 653These problems are handled by the 654.Fl f 655option and the 656.Xr getopt 3 657``--'' construct. 658.Pp 659The 660.Ic -delete 661primary does not interact well with other options that cause the filesystem 662tree traversal options to be changed. 663.Sh HISTORY 664A 665.Nm 666command appeared in 667.At v1 . 668