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