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