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