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