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