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 -flags Oo Cm - Ns | Ns Cm + Oc Ns Ar flags , Ns Ar notflags 247The flags are specified using symbolic names (see 248.Xr chflags 1 ) . 249Those with the 250.Qq Li no 251prefix (except 252.Qq Li nodump ) 253are said to be 254.Ar notflags . 255Flags in 256.Ar flags 257are checked to be set, and flags in 258.Ar notflags 259are checked to be not set. 260Note that this is different from 261.Ic -perm , 262which only allows the user to specify mode bits that are set. 263.Pp 264If flags are preceded by a dash 265.Pq Dq Li - , 266this primary evaluates to true 267if at least all of the bits in 268.Ar flags 269and none of the bits in 270.Ar notflags 271are set in the file's flags bits. 272If flags are preceded by a plus 273.Pq Dq Li + , 274this primary evaluates to true 275if any of the bits in 276.Ar flags 277is set in the file's flags bits, 278or any of the bits in 279.Ar notflags 280is not set in the file's flags bits. 281Otherwise, 282this primary evaluates to true 283if the bits in 284.Ar flags 285exactly match the file's flags bits, 286and none of the 287.Ar flags 288bits match those of 289.Ar notflags . 290.It Ic -fstype Ar type 291True if the file is contained in a file system of type 292.Ar type . 293The 294.Xr sysctl 8 295command can be used to find out the types of filesystems 296that are available on the system: 297.Pp 298.Dl "sysctl vfs" 299.Pp 300In addition, there are two pseudo-types, 301.Dq Li local 302and 303.Dq Li rdonly . 304The former matches any file system physically mounted on the system where 305the 306.Nm 307is being executed and the latter matches any file system which is 308mounted read-only. 309.It Ic -group Ar gname 310True if the file belongs to the group 311.Ar gname . 312If 313.Ar gname 314is numeric and there is no such group name, then 315.Ar gname 316is treated as a group ID. 317.It Ic -iname Ar pattern 318Like 319.Ic -name , 320but the match is case insensitive. 321.It Ic -inum Ar n 322True if the file has inode number 323.Ar n . 324.It Ic -ipath Ar pattern 325Like 326.Ic -path , 327but the match is case insensitive. 328.It Ic -iregex Ar pattern 329Like 330.Ic -regex , 331but the match is case insensitive. 332.It Ic -links Ar n 333True if the file has 334.Ar n 335links. 336.It Ic -ls 337This primary always evaluates to true. 338The following information for the current file is written to standard output: 339its inode number, size in 512\-byte blocks, file permissions, number of hard 340links, owner, group, size in bytes, last modification time, and pathname. 341If the file is a block or character special file, the major and minor numbers 342will be displayed instead of the size in bytes. 343If the file is a symbolic link, the pathname of the linked\-to file will be 344displayed preceded by 345.Dq Li -> . 346The format is identical to that produced by 347.Bk -words 348.Nm ls Fl dgils . 349.Ek 350.It Ic -maxdepth Ar n 351True if the depth of the current file into the tree is less than or equal to 352.Ar n . 353.It Ic -mindepth Ar n 354True if the depth of the current file into the tree is greater than or equal to 355.Ar n . 356.It Ic -mmin Ar n 357True if the difference between the file last modification time and the time 358.Nm 359was started, rounded up to the next full minute, is 360.Ar n 361minutes. 362.It Ic -mnewer Ar file 363Same as 364.Ic -newer . 365.It Ic -mtime Ar n 366True if the difference between the file last modification time and the time 367.Nm 368was started, rounded up to the next full 24\-hour period, is 369.Ar n 37024\-hour periods. 371.It Ic -name Ar pattern 372True if the last component of the pathname being examined matches 373.Ar pattern . 374Special shell pattern matching characters 375.Dq ( Li \&[ , 376.Dq Li \&] , 377.Dq Li * , 378and 379.Dq Li \&? ) 380may be used as part of 381.Ar pattern . 382These characters may be matched explicitly by escaping them with a 383backslash 384.Pq Dq Li \e . 385.It Ic -newer Ar file 386True if the current file has a more recent last modification time than 387.Ar file . 388.It Ic -newer Ns Ar X Ns Ar Y Ar file 389True if the current file has a more recent last access time 390.Ar ( X Ns = Ns Cm a ) , 391change time 392.Ar ( X Ns = Ns Cm c ) , 393or modification time 394.Ar ( X Ns = Ns Cm m ) 395than the last access time 396.Ar ( Y Ns = Ns Cm a ) , 397change time 398.Ar ( Y Ns = Ns Cm c ) , 399or modification time 400.Ar ( Y Ns = Ns Cm m ) 401of 402.Ar file . 403In addition, if 404.Ar Y Ns = Ns Cm t , 405then 406.Ar file 407is instead interpreted as a direct date specification of the form 408understood by 409.Xr cvs 1 . 410Note that 411.Ic -newermm 412is equivalent to 413.Ic -newer . 414.It Ic -nogroup 415True if the file belongs to an unknown group. 416.It Ic -nouser 417True if the file belongs to an unknown user. 418.It Ic -ok Ar utility Op Ar argument ... ; 419The 420.Ic -ok 421primary is identical to the 422.Ic -exec 423primary with the exception that 424.Nm 425requests user affirmation for the execution of the 426.Ar utility 427by printing 428a message to the terminal and reading a response. 429If the response is other than 430.Dq Li y 431the command is not executed and the 432value of the 433.Ic -ok 434expression is false. 435.It Ic -okdir Ar utility Op Ar argument ... ; 436The 437.Ic -okdir 438primary is identical to the 439.Ic -execdir 440primary with the same exception as described for the 441.Ic -ok 442primary. 443.It Ic -path Ar pattern 444True if the pathname being examined matches 445.Ar pattern . 446Special shell pattern matching characters 447.Dq ( Li \&[ , 448.Dq Li \&] , 449.Dq Li * , 450and 451.Dq Li \&? ) 452may be used as part of 453.Ar pattern . 454These characters may be matched explicitly by escaping them with a 455backslash 456.Pq Dq Li \e . 457Slashes 458.Pq Dq Li / 459are treated as normal characters and do not have to be 460matched explicitly. 461.It Ic -perm Oo Cm - Ns | Ns Cm + Oc Ns Ar mode 462The 463.Ar mode 464may be either symbolic (see 465.Xr chmod 1 ) 466or an octal number. 467If the 468.Ar mode 469is symbolic, a starting value of zero is assumed and the 470.Ar mode 471sets or clears permissions without regard to the process' file mode 472creation mask. 473If the 474.Ar mode 475is octal, only bits 07777 476.Pq Dv S_ISUID | S_ISGID | S_ISTXT | S_IRWXU | S_IRWXG | S_IRWXO 477of the file's mode bits participate 478in the comparison. 479If the 480.Ar mode 481is preceded by a dash 482.Pq Dq Li - , 483this primary evaluates to true 484if at least all of the bits in the 485.Ar mode 486are set in the file's mode bits. 487If the 488.Ar mode 489is preceded by a plus 490.Pq Dq Li + , 491this primary evaluates to true 492if any of the bits in the 493.Ar mode 494are set in the file's mode bits. 495Otherwise, this primary evaluates to true if 496the bits in the 497.Ar mode 498exactly match the file's mode bits. 499Note, the first character of a symbolic mode may not be a dash 500.Pq Dq Li - . 501.It Ic -print 502This primary always evaluates to true. 503It prints the pathname of the current file to standard output. 504If none of 505.Ic -exec , -ls , -print0 , 506or 507.Ic -ok 508is specified, the given expression shall be effectively replaced by 509.Cm \&( Ar "given expression" Cm \&) Ic -print . 510.It Ic -print0 511This primary always evaluates to true. 512It prints the pathname of the current file to standard output, followed by an 513.Tn ASCII NUL 514character (character code 0). 515.It Ic -prune 516This primary always evaluates to true. 517It causes 518.Nm 519to not descend into the current file. 520Note, the 521.Ic -prune 522primary has no effect if the 523.Fl d 524option was specified. 525.It Ic -regex Ar pattern 526True if the whole path of the file matches 527.Ar pattern 528using regular expression. 529To match a file named 530.Dq Pa ./foo/xyzzy , 531you can use the regular expression 532.Dq Li ".*/[xyz]*" 533or 534.Dq Li ".*/foo/.*" , 535but not 536.Dq Li xyzzy 537or 538.Dq Li /foo/ . 539.It Ic -size Ar n Ns Op Cm c 540True if the file's size, rounded up, in 512\-byte blocks is 541.Ar n . 542If 543.Ar n 544is followed by a 545.Cm c , 546then the primary is true if the 547file's size is 548.Ar n 549bytes (characters). 550.It Ic -type Ar t 551True if the file is of the specified type. 552Possible file types are as follows: 553.Pp 554.Bl -tag -width indent -compact 555.It Cm b 556block special 557.It Cm c 558character special 559.It Cm d 560directory 561.It Cm f 562regular file 563.It Cm l 564symbolic link 565.It Cm p 566FIFO 567.It Cm s 568socket 569.El 570.It Ic -user Ar uname 571True if the file belongs to the user 572.Ar uname . 573If 574.Ar uname 575is numeric and there is no such user name, then 576.Ar uname 577is treated as a user ID. 578.El 579.Pp 580All primaries which take a numeric argument allow the number to be 581preceded by a plus sign 582.Pq Dq Li + 583or a minus sign 584.Pq Dq Li - . 585A preceding plus sign means 586.Dq more than n , 587a preceding minus sign means 588.Dq less than n 589and neither means 590.Dq exactly n . 591.Sh OPERATORS 592The primaries may be combined using the following operators. 593The operators are listed in order of decreasing precedence. 594.Pp 595.Bl -tag -width "( expression )" -compact 596.It Cm \&( Ar expression Cm \&) 597This evaluates to true if the parenthesized expression evaluates to 598true. 599.Pp 600.It Cm \&! Ar expression 601This is the unary 602.Tn NOT 603operator. 604It evaluates to true if the expression is false. 605.Pp 606.It Ar expression Cm -and Ar expression 607.It Ar expression expression 608The 609.Cm -and 610operator is the logical 611.Tn AND 612operator. 613As it is implied by the juxtaposition of two expressions it does not 614have to be specified. 615The expression evaluates to true if both expressions are true. 616The second expression is not evaluated if the first expression is false. 617.Pp 618.It Ar expression Cm -or Ar expression 619The 620.Cm -or 621operator is the logical 622.Tn OR 623operator. 624The expression evaluates to true if either the first or the second expression 625is true. 626The second expression is not evaluated if the first expression is true. 627.El 628.Pp 629All operands and primaries must be separate arguments to 630.Nm . 631Primaries which themselves take arguments expect each argument 632to be a separate argument to 633.Nm . 634.Sh EXAMPLES 635The following examples are shown as given to the shell: 636.Bl -tag -width indent 637.It Li "find / \e! -name \*q*.c\*q -print" 638Print out a list of all the files whose names do not end in 639.Pa .c . 640.It Li "find / -newer ttt -user wnj -print" 641Print out a list of all the files owned by user 642.Dq wnj 643that are newer 644than the file 645.Pa ttt . 646.It Li "find / \e! \e( -newer ttt -user wnj \e) -print" 647Print out a list of all the files which are not both newer than 648.Pa ttt 649and owned by 650.Dq wnj . 651.It Li "find / \e( -newer ttt -or -user wnj \e) -print" 652Print out a list of all the files that are either owned by 653.Dq wnj 654or that are newer than 655.Pa ttt . 656.It Li "find . -newerct '1 minute ago' -print" 657Print out a list of all the files whose inode change time is more 658recent than the current time minus one minute. 659.El 660.Sh SEE ALSO 661.Xr chflags 1 , 662.Xr chmod 1 , 663.Xr cvs 1 , 664.Xr locate 1 , 665.Xr whereis 1 , 666.Xr which 1 , 667.Xr stat 2 , 668.Xr fts 3 , 669.Xr getgrent 3 , 670.Xr getpwent 3 , 671.Xr strmode 3 , 672.Xr re_format 7 , 673.Xr symlink 7 674.Sh STANDARDS 675The 676.Nm 677utility syntax is a superset of the syntax specified by the 678.St -p1003.2 679standard. 680.Pp 681All the single character options as well as the 682.Ic -iname , -inum , -iregex , -print0 , -delete , -ls , 683and 684.Ic -regex 685primaries are extensions to 686.St -p1003.2 . 687.Pp 688Historically, the 689.Fl d , h 690and 691.Fl x 692options were implemented using the primaries 693.Ic -depth , -follow , 694and 695.Ic -xdev . 696These primaries always evaluated to true. 697As they were really global variables that took effect before the traversal 698began, some legal expressions could have unexpected results. 699An example is the expression 700.Ic -print Cm -o Ic -depth . 701As 702.Ic -print 703always evaluates to true, the standard order of evaluation 704implies that 705.Ic -depth 706would never be evaluated. 707This is not the case. 708.Pp 709The operator 710.Cm -or 711was implemented as 712.Cm -o , 713and the operator 714.Cm -and 715was implemented as 716.Cm -a . 717.Pp 718Historic implementations of the 719.Ic -exec 720and 721.Ic -ok 722primaries did not replace the string 723.Dq Li {} 724in the utility name or the 725utility arguments if it had preceding or following non-whitespace characters. 726This version replaces it no matter where in the utility name or arguments 727it appears. 728.Pp 729The 730.Fl E 731option was implemented on the analogy of 732.Xr grep 1 733and 734.Xr sed 1 . 735.Sh BUGS 736The special characters used by 737.Nm 738are also special characters to many shell programs. 739In particular, the characters 740.Dq Li * , 741.Dq Li \&[ , 742.Dq Li \&] , 743.Dq Li \&? , 744.Dq Li \&( , 745.Dq Li \&) , 746.Dq Li \&! , 747.Dq Li \e 748and 749.Dq Li \&; 750may have to be escaped from the shell. 751.Pp 752As there is no delimiter separating options and file names or file 753names and the 754.Ar expression , 755it is difficult to specify files named 756.Pa -xdev 757or 758.Pa \&! . 759These problems are handled by the 760.Fl f 761option and the 762.Xr getopt 3 763.Dq Fl Fl 764construct. 765.Pp 766The 767.Ic -delete 768primary does not interact well with other options that cause the filesystem 769tree traversal options to be changed. 770.Sh HISTORY 771A 772.Nm 773command appeared in 774.At v1 . 775