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