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