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