xref: /netbsd-src/usr.bin/find/find.1 (revision 6a1508dad3515842aa76bf5ec8fc2daab5f5af02)
1.\"	$NetBSD: find.1,v 1.67 2009/01/04 12:13:56 wiz Exp $
2.\"
3.\" Copyright (c) 1990, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" the Institute of Electrical and Electronics Engineers, Inc.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. Neither the name of the University nor the names of its contributors
18.\"    may be used to endorse or promote products derived from this software
19.\"    without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.\"	from: @(#)find.1	8.7 (Berkeley) 5/9/95
34.\"
35.Dd July 19, 2007
36.Dt FIND 1
37.Os
38.Sh NAME
39.Nm find
40.Nd walk a file hierarchy
41.Sh SYNOPSIS
42.Nm
43.Op Fl H | Fl L | Fl P
44.Op Fl dEhsXx
45.Ar file
46.Op Ar file ...
47.Op Ar expression
48.Nm
49.Op Fl H | Fl L | Fl P
50.Op Fl dEhsXx
51.Fl f Ar file
52.Op Ar file ...
53.Op Ar expression
54.Sh DESCRIPTION
55.Nm
56recursively descends the directory tree for each
57.Ar file
58listed, evaluating an
59.Ar expression
60(composed of the
61.Dq primaries
62and
63.Dq operands
64listed below) in terms
65of each file in the tree.
66.Pp
67The options are as follows:
68.Pp
69.Bl -tag -width Ds
70.It Fl H
71The
72.Fl H
73option causes the file information and file type (see
74.Xr stat 2 ) ,
75returned for each symbolic link encountered on the command line to be
76those of the file referenced by the link, not the link itself.
77If the referenced file does not exist, the file information and type will
78be for the link itself.
79File information of all symbolic links not on the command line is that
80of the link itself.
81.It Fl L
82The
83.Fl L
84option causes the file information and file type (see
85.Xr stat 2 )
86returned for each symbolic link to be those of the file referenced by the
87link, not the link itself.
88If the referenced file does not exist, the file information and type will
89be for the link itself.
90.It Fl P
91The
92.Fl P
93option causes the file information and file type (see
94.Xr stat 2 )
95returned for each symbolic link to be those of the link itself.
96.It Fl d
97The
98.Fl d
99option causes
100.Nm
101to perform a depth-first traversal, i.e., directories
102are visited in post-order and all entries in a directory will be acted
103on before the directory itself.
104By default,
105.Nm
106visits directories in pre-order, i.e., before their contents.
107Note, the default is
108.Ar not
109a breadth-first traversal.
110.It Fl E
111The
112.Fl E
113option causes
114.Ar regexp
115arguments to primaries to be interpreted as extended regular
116expressions (see
117.Xr re_format 7 ) .
118.It Fl f
119The
120.Fl f
121option specifies a file hierarchy for
122.Nm
123to traverse.
124File hierarchies may also be specified as the operands immediately
125following the options.
126.It Fl h
127The
128.Fl h
129option causes the file information and file type (see
130.Xr stat  2  ) ,
131returned for each symbolic link to be those of the file referenced by the
132link, not the link itself.
133If the referenced file does not exist, the file information and type will
134be for the link itself.
135.It Fl s
136The
137.Fl s
138option causes the entries of each directory to be sorted in
139lexicographical order.
140Note that the sorting is done only inside of each directory;
141files in different directories are not sorted.
142Therefore,
143.Sq Li a/b
144appears before
145.Sq Li a.b ,
146which is different from
147.Dq Li "find ... \&| sort"
148order.
149.It Fl X
150The
151.Fl X
152option is a modification to permit
153.Nm
154to be safely used in conjunction with
155.Xr xargs 1 .
156If a file name contains any of the delimiting characters used by
157.Nm xargs ,
158a diagnostic message is displayed on standard error, and the file
159is skipped.
160The delimiting characters include single
161.Pq Dq \&'
162and double
163.Pq Dq \&"
164quotes, backslash
165.Pq Dq \e ,
166space, tab and newline characters.
167Alternatively, the
168.Ic -print0
169or
170.Ic -printx
171primaries can be used to format the output in a way that
172.Nm xargs
173can accept.
174.It Fl x
175The
176.Fl x
177option restricts the search to the file system containing the
178directory specified.
179Does not list mount points to other file systems.
180.El
181.Sh PRIMARIES
182.Bl -tag -width Ds
183.It Ic -amin Ar n
184True if the difference between the file last access time and the time
185.Nm
186was started, rounded up to the next full minute, is
187.Ar n
188minutes.
189.It Ic -anewer Ar file
190True if the current file has a more recent last access time than
191.Ar file  .
192.It Ic -atime Ar n
193True if the difference between the file last access time and the time
194.Nm
195was started, rounded up to the next full 24-hour period, is
196.Ar n
19724-hour periods.
198.It Ic -cmin Ar n
199True if the difference between the time of last change of file status
200information and the time
201.Nm
202was started, rounded up to the next full minute, is
203.Ar n
204minutes.
205.It Ic -cnewer Ar file
206True if the current file has a more recent last change time than
207.Ar file  .
208.It Ic -ctime Ar n
209True if the difference between the time of last change of file status
210information and the time
211.Nm
212was started, rounded up to the next full 24-hour period, is
213.Ar n
21424-hour periods.
215.It Ic -delete
216Delete found files and/or directories.
217Always returns True.
218This executes from the current working directory as
219.Nm
220recurses down the tree.
221It will not attempt to delete a filename with a
222.Dq /
223character in its pathname relative to
224.Dq \&.
225for security reasons.
226Depth-first traversal processing is implied by this option.
227This can also be invoked as
228.Ic -rm .
229.It Ic -empty
230True if the current file or directory is empty.
231.\" The ".sp" below is probably not the right way to get the desired effect.
232.It Ic -exec Ar utility Oo argument ... Oc No ;
233.sp -1l
234.It Ic -exec Ar utility Oo argument ... Oc No {} +
235Execute the specified
236.Ar utility
237with the specified arguments.
238The list of arguments is terminated by
239.Dq Li \&;
240or
241.Dq Li \&+ .
242.Ar utility
243will be executed from the directory from which
244.Nm
245was executed.
246.Pp
247If terminated by a semicolon
248.Pq Dq \&; ,
249the
250.Ar utility
251is invoked once per path.
252If the string
253.Dq {}
254appears anywhere in the utility name or the arguments,
255it is replaced by the pathname of the current file.
256.Pp
257If terminated by a plus sign
258.Pq Dq \&+ ,
259the pathnames for which the
260primary is evaluated are aggregated into sets, and
261.Ar utility
262will be invoked once per set, similar to
263.Xr xargs 1 .
264If any invocation exits with non-zero exit status, then
265.Nm
266will eventually do so as well, but this does not cause
267.Nm
268to exit early.
269The string
270.Dq {}
271must appear, and must appear last.
272Each set is limitted to no more than 5,000 pathnames,
273and is also limitted such that the invokation of
274.Ar utility
275does not exceed
276.Dv ARG_MAX .
277.It Ic -execdir Ar utility Oo argument ... Oc No ;
278The
279.Ic -execdir
280primary is similar to the semicolon-terminated
281.Pq Dq \&;
282variant of the
283.Ic -exec
284primary, with the exception that
285.Ar utility
286will be executed from the directory that holds
287the current file.
288The filename substituted for the string
289.Dq {}
290is not qualified.
291Set aggregation
292.Pq Do \&+ Dc termination
293is not supported.
294.It Ic -exit Op Ar n
295This primary causes
296.Nm
297to stop traversing the filesystem and exit immediately if a
298previous condition was met.
299If no value is specified, the exit value will be 0, else
300.Ar n .
301Note that other primaries will be evaluated and acted upon before exiting.
302.It Ic -false
303This primary always evaluates to false.
304This can be used following a primary that caused the
305expression to be true to make the expression to be false.
306This can be useful after using a
307.Ic -fprint
308primary so it can continue to the next expression (using an
309.Cm -or
310operator, for example).
311.It Ic -flags Oo Fl Oc Ns Ar flags
312If
313.Ar flags
314are preceded by a dash
315.Pq Dq - ,
316this primary evaluates to true
317if at least all of the bits in
318.Ar flags
319are set in the file's flags bits.
320If
321.Ar flags
322are not preceded by a dash, this primary evaluates to true if
323the bits in
324.Ar flags
325exactly match the file's flags bits.
326If
327.Ar flags
328is
329.Dq none ,
330files with no flags bits set are matched.
331(See
332.Xr chflags 1
333for more information about file flags.)
334.It Ic -follow
335Follow symbolic links.
336.It Ic -fprint Ar filename
337This primary always evaluates to true.
338This creates
339.Ar filename
340or overwrites the file if it already exists.
341The file is created at startup.
342It writes the pathname of the current file to this file, followed
343by a newline character.
344The file will be empty if no files are matched.
345.It Ic -fstype Ar type
346True if the file is contained in a file system of type
347.Ar type .
348The
349.Xr sysctl 8
350command can be used to find out the types of filesystems
351that are available on the system:
352.Bd -literal -offset indent
353sysctl vfs.generic.fstypes
354.Ed
355.Pp
356In addition, there are two pseudo-types,
357.Dq local
358and
359.Dq rdonly .
360The former matches any file system physically mounted on the system where
361the
362.Nm
363is being executed, and the latter matches any file system which is
364mounted read-only.
365.It Ic -group Ar gname
366True if the file belongs to the group
367.Ar gname  .
368If
369.Ar gname
370is numeric and there is no such group name, then
371.Ar gname
372is treated as a group id.
373.It Ic -iname Ar pattern
374True if the last component of the pathname being examined
375matches
376.Ar pattern .
377Case insensitive.
378.It Ic -inum Ar n
379True if the file has inode number
380.Ar n  .
381.It Ic -iregex Ar regexp
382True if the path name of the current file matches the case-insensitive
383basic regular expression
384.Pq see Xr re_format 7
385.Ar regexp .
386This is a match on the whole path, not a search for the regular expression
387within the path.
388.It Ic -links Ar n
389True if the file has
390.Ar n
391links.
392.It Ic -rm
393This is an alias for
394.Ic -delete .
395.It Ic -ls
396This primary always evaluates to true.
397The following information for the current file is written to standard output:
398its inode number, size in 512-byte blocks, file permissions, number of hard
399links, owner, group, size in bytes, last modification time, and pathname.
400If the file is a block or character special file, the major and minor numbers
401will be displayed instead of the size in bytes.
402If the file is a symbolic link, the pathname of the linked-to file will be
403displayed preceded by
404.Dq -\*[Gt] .
405The format is identical to that produced by
406.Dq ls -dgils .
407.It Ic -maxdepth Ar n
408True if the current search depth is less than or equal to what is specified in
409.Ar n .
410.It Ic -mindepth Ar n
411True if the current search depth is at least what is specified in
412.Ar n .
413.It Ic -mmin Ar n
414True if the difference between the file last modification time and the time
415.Nm
416was started, rounded up to the next full minute, is
417.Ar n
418minutes.
419.It Ic -mtime 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 24-hour period, is
423.Ar n
42424-hour periods.
425.It Ic -ok Ar utility Oo argument ... Oc No ;
426The
427.Ic -ok
428primary is similar to the semicolon-terminated
429.Pq Dq \&;
430variant of the
431.Ic -exec
432primary, with the exception that
433.Nm
434requests user affirmation for the execution of the utility by printing
435a message to the terminal and reading a response.
436If the response is other than
437.Dq y ,
438the command is not executed and the
439.Ar -ok
440primary evaluates to false.
441Set aggregation
442.Pq Do \&+ Dc termination
443is not supported.
444.It Ic -name Ar pattern
445True if the last component of the pathname being examined matches
446.Ar pattern  .
447Special shell pattern matching characters
448.Po
449.Dq \&[ ,
450.Dq \&] ,
451.Dq \&* ,
452.Dq \&?
453.Pc
454may be used as part of
455.Ar pattern  .
456These characters may be matched explicitly by escaping them with a
457backslash
458.Pq Dq \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 -nouser
463True if the file belongs to an unknown user.
464.It Ic -nogroup
465True if the file belongs to an unknown group.
466.It Ic -path Ar pattern
467True if the pathname being examined matches
468.Ar pattern  .
469Special shell pattern matching characters
470.Po
471.Dq \&[ ,
472.Dq \&] ,
473.Dq \&* ,
474and
475.Dq \&?
476.Pc
477may be used as part of
478.Ar pattern  .
479These characters may be matched explicitly by escaping them with a
480backslash
481.Pq Dq \e .
482Slashes
483.Pq Dq /
484are treated as normal characters and do not have to be
485matched explicitly.
486.It Ic -perm Oo Fl Oc Ns Ar mode
487The
488.Ar mode
489may be either symbolic (see
490.Xr chmod  1  )
491or an octal number.
492If the mode is symbolic, a starting value of zero is assumed and the
493mode sets or clears permissions without regard to the process' file mode
494creation mask.
495If the mode is octal, only bits 07777
496.Pf ( Dv S_ISUID
497|
498.Dv S_ISGID
499|
500.Dv S_ISTXT
501|
502.Dv S_IRWXU
503|
504.Dv S_IRWXG
505|
506.Dv S_IRWXO )
507of the file's mode bits participate
508in the comparison.
509If the mode is preceded by a dash
510.Pq Dq - ,
511this primary evaluates to true
512if at least all of the bits in the mode are set in the file's mode bits.
513If the mode is not preceded by a dash, this primary evaluates to true if
514the bits in the mode exactly match the file's mode bits.
515Note, the first character of a symbolic mode may not be a dash
516.Pq Dq - .
517.It Ic -print
518This primary always evaluates to true.
519It prints the pathname of the current file to standard output, followed
520by a newline character.
521If none of
522.Ic -exec ,
523.Ic -exit ,
524.Ic -fprint ,
525.Ic -ls ,
526.Ic -ok ,
527.Ic -print0 ,
528nor
529.Ic -printx
530is specified, the given expression shall be effectively replaced by
531.Cm \&( Ns Ar given\& expression Ns Cm \&)
532.Ic -print .
533.It Ic -print0
534This primary always evaluates to true.
535It prints the pathname of the current file to standard output, followed
536by a null character.
537.It Ic -printx
538This primary always evaluates to true.
539It prints the pathname of the current file to standard output,
540with each space, tab, newline, backslash, dollar sign, and single,
541double, or back quotation mark prefixed by a backslash, so the output of
542.Nm find
543can safely be used as input to
544.Nm xargs .
545.It Ic -prune
546This primary always evaluates to true.
547It causes
548.Nm
549to not descend into the current file.
550Note, the
551.Ic -prune
552primary has no effect if the
553.Fl d
554option was specified.
555.It Ic -regex Ar regexp
556True if the path name of the current file matches the case-sensitive
557basic regular expression
558.Pq see Xr re_format 7
559.Ar regexp .
560This is a match on the whole path, not a search for the regular expression
561within the path.
562.It Ic -size Ar n Ns Op Cm c
563True if the file's size, rounded up, in 512-byte blocks is
564.Ar n  .
565If
566.Ar n
567is followed by a
568.Dq c ,
569then the primary is true if the file's size is
570.Ar n
571bytes.
572.It Ic -type Ar t
573True if the file is of the specified type.
574Possible file types are as follows:
575.Pp
576.Bl -tag -width flag -offset indent -compact
577.It Cm b
578block special
579.It Cm c
580character special
581.It Cm d
582directory
583.It Cm f
584regular file
585.It Cm l
586symbolic link
587.It Cm p
588FIFO
589.It Cm s
590socket
591.It Cm W
592whiteout
593.It Cm w
594whiteout
595.El
596.Pp
597.It Ic -user Ar uname
598True if the file belongs to the user
599.Ar uname  .
600If
601.Ar uname
602is numeric and there is no such user name, then
603.Ar uname
604is treated as a user id (and considered a numeric argument).
605.It Ic -xdev
606This primary always evaluates to true.
607It causes find not to descend past directories that have a different
608device ID (st_dev, see
609.Xr stat 2
610S5.6.2 [POSIX.1]).
611.El
612.Pp
613All primaries which take a numeric argument allow the number to be
614preceded by a plus sign
615.Pq Dq +
616or a minus sign
617.Pq Dq \- .
618A preceding plus sign means
619.Dq more than n ,
620a preceding minus sign means
621.Dq less than n ,
622and neither means
623.Dq exactly n .
624.Sh OPERATORS
625The primaries may be combined using the following operators.
626The operators are listed in order of decreasing precedence.
627.Bl -tag -width (expression)
628.It Cm \&( Ar expression Cm \&)
629This evaluates to true if the parenthesized expression evaluates to
630true.
631.Pp
632.It Cm \&! Ar expression
633This is the unary
634.Tn NOT
635operator.
636It evaluates to true if the expression is false.
637.Pp
638.It Ar expression Cm -and Ar expression
639.It Ar expression expression
640The
641.Cm -and
642operator is the logical
643.Tn AND
644operator.
645As it is implied by the juxtaposition of two expressions it does not
646have to be specified.
647The expression evaluates to true if both expressions are true.
648The second expression is not evaluated if the first expression is false.
649.Pp
650.It Ar expression Cm -or Ar expression
651The
652.Cm -or
653operator is the logical
654.Tn OR
655operator.
656The expression evaluates to true if either the first or the second expression
657is true.
658The second expression is not evaluated if the first expression is true.
659.El
660.Pp
661All operands and primaries must be separate arguments to
662.Nm  .
663Primaries which themselves take arguments expect each argument
664to be a separate argument to
665.Nm  .
666.Sh EXIT STATUS
667The
668.Nm
669utility normally exits 0 on success, and exits with 1 under certain
670internal error conditions.
671If any invokations of
672.Dq Ic -exec Ar ... No +
673primaries return non-zero exit-status, then
674.Nm
675will do so as well.
676.Sh EXAMPLES
677The following examples are shown as given to the shell:
678.Bl -tag -width findx
679.It Li "find  /  \e!  -name  \*q*.c\*q  -print"
680Print out a list of all the files whose names do not end in
681.Dq \&.c .
682.It Li "find  /  -newer  ttt  -user  wnj  -print"
683Print out a list of all the files owned by user
684.Dq wnj
685that are newer than the file
686.Dq ttt .
687.It Li "find  /  \e!  \e(  -newer  ttt  -user  wnj  \e)  -print"
688Print out a list of all the files which are not both newer than
689.Dq ttt
690and owned by
691.Dq wnj .
692.It Li "find  /  \e(  -newer  ttt  -or  -user wnj  \e)  -print"
693Print out a list of all the files that are either owned by
694.Dq wnj
695or that are newer than
696.Dq ttt .
697.It Li "find  /  \e(  -newer  ttt  -or  -user wnj  \e)  -exit 1"
698Return immediately with a value of 1 if any files are found that are either
699owned by
700.Dq wnj
701or that are newer than
702.Dq ttt ,
703but do not print them.
704.It Li "find  /  \e(  -newer  ttt  -or  -user wnj  \e)  -ls -exit 1"
705Same as above, but list the first file matching the criteria before exiting
706with a value of 1.
707.El
708.Sh SEE ALSO
709.Xr chflags 1 ,
710.Xr chmod 1 ,
711.Xr locate 1 ,
712.Xr xargs 1 ,
713.Xr stat 2 ,
714.Xr fts 3 ,
715.Xr getgrent 3 ,
716.Xr getpwent 3 ,
717.Xr strmode 3 ,
718.Xr symlink 7 ,
719.Xr sysctl 8
720.Sh STANDARDS
721The
722.Nm
723utility syntax is a superset of the syntax specified by the
724.St -p1003.2
725standard.
726.Pp
727The options and the
728.Ic -amin ,
729.Ic -anewer ,
730.Ic -cmin ,
731.Ic -cnewer ,
732.Ic -delete ,
733.Ic -empty ,
734.Ic -execdir ,
735.Ic -follow ,
736.Ic -fstype ,
737.Ic -iname ,
738.Ic -inum ,
739.Ic -iregex ,
740.Ic -links ,
741.Ic -ls ,
742.Ic -maxdepth ,
743.Ic -mindepth ,
744.Ic -mmin ,
745.Ic -path ,
746.Ic -print0 ,
747.Ic -printx ,
748.Ic -regex ,
749and
750.Ic -rm
751primaries are extensions to
752.St -p1003.2 .
753.Pp
754Historically, the
755.Fl d ,
756.Fl h ,
757and
758.Fl x
759options were implemented using the primaries
760.Dq -depth ,
761.Dq -follow ,
762and
763.Dq -xdev .
764These primaries always evaluated to true.
765As they were really global variables that took effect before the traversal
766began, some legal expressions could have unexpected results.
767An example is the expression
768.Dq -print -o -depth .
769As -print always evaluates to true, the standard order of evaluation
770implies that -depth would never be evaluated.
771This is not the case.
772.Pp
773The operator
774.Dq -or
775was implemented as
776.Dq -o ,
777and the operator
778.Dq -and
779was implemented as
780.Dq -a .
781.Pp
782Historic implementations of the
783.Ic -exec
784and
785.Ic -ok
786primaries did not replace the string
787.Dq {}
788in the utility name or the
789utility arguments if it had preceding or following non-whitespace characters.
790This version replaces it no matter where in the utility name or arguments
791it appears.
792.Pp
793Support for
794.Dq Ic -exec Ar ... No +
795is consistent with
796.Em IEEE PASC Interpretation 1003.2 #210 ,
797though the feature originated in
798.Tn SVR4 .
799.Pp
800The
801.Ic -delete
802primary does not interact well with other options that cause the filesystem
803tree traversal options to be changed.
804.Sh HISTORY
805A much simpler
806.Nm find
807command appeared in First Edition AT\*[Am]T Unix.
808The syntax had become similar to the present version by
809the time of the Fifth Edition.
810.Sh BUGS
811The special characters used by
812.Nm
813are also special characters to many shell programs.
814In particular, the characters
815.Dq \&* ,
816.Dq \&[ ,
817.Dq \&] ,
818.Dq \&? ,
819.Dq \&( ,
820.Dq \&) ,
821.Dq \&! ,
822.Dq \e ,
823and
824.Dq \&;
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.Dq -xdev
832or
833.Dq \&! .
834These problems are handled by the
835.Fl f
836option and the
837.Xr getopt 3
838.Dq --
839construct.
840