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