xref: /openbsd/usr.bin/find/find.1 (revision 09467b48)
1.\"	$OpenBSD: find.1,v 1.98 2019/09/02 21:18:41 deraadt Exp $
2.\" Copyright (c) 1990, 1993
3.\"	The Regents of the University of California.  All rights reserved.
4.\"
5.\" This code is derived from software contributed to Berkeley by
6.\" the Institute of Electrical and Electronics Engineers, Inc.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	from: @(#)find.1	8.1 (Berkeley) 6/6/93
33.\"
34.Dd $Mdocdate: September 2 2019 $
35.Dt FIND 1
36.Os
37.Sh NAME
38.Nm find
39.Nd walk a file hierarchy
40.Sh SYNOPSIS
41.Nm find
42.Op Fl dHhLXx
43.Op Fl f Ar path
44.Ar path ...
45.Op Ar expression
46.Sh DESCRIPTION
47.Nm
48recursively descends the directory tree for each
49.Ar path
50listed, evaluating an
51.Ar expression
52(composed of the
53.Dq primaries
54and
55.Dq operators
56listed below) in terms
57of each file in the tree.
58In the absence of an expression,
59.Ic -print
60is assumed.
61If an expression is given,
62but none of the primaries
63.Ic -delete ,
64.Ic -exec ,
65.Ic -execdir ,
66.Ic -ls ,
67.Ic -ok ,
68.Ic -print ,
69or
70.Ic -print0
71are specified, the given expression is effectively replaced by
72.Cm \&( Ar given expression Cm \&)
73.Ic -print .
74.Pp
75The options are as follows:
76.Bl -tag -width Ds
77.It Fl d
78Causes
79.Nm
80to visit directories in post-order
81i.e. all entries in a directory will be acted on before the directory itself.
82By default,
83.Nm
84visits directories in pre-order i.e. before their contents.
85.It Fl f Ar path
86Specifies a file hierarchy for
87.Nm
88to traverse.
89File hierarchies may be specified without the
90.Fl f
91option if they are given immediately after any other options.
92.It Fl H
93Causes the file information and file type (see
94.Xr stat 2 )
95returned for each symbolic link encountered on the command line to be
96those of the file referenced by the link, not the link itself.
97If the referenced file does not exist, the file information and type will
98be for the link itself.
99File information of all symbolic links not on
100the command line is that of the link itself.
101.It Fl h
102An alias for the
103.Fl L
104option.
105This option exists for backwards compatibility.
106.It Fl L
107Causes the file information and file type (see
108.Xr stat 2 )
109returned for each symbolic link to be those of the file referenced by the
110link, not the link itself.
111If the referenced file does not exist, the file information and type will
112be for the link itself.
113.It Fl X
114Permit
115.Nm
116to be safely used in conjunction with
117.Xr xargs 1 .
118If a file name contains any of the delimiting characters used by xargs,
119a diagnostic message is displayed on standard error, and the file
120is skipped.
121The delimiting characters include single
122.Pq Ql '
123and double
124.Pq Ql \&"
125quotes, backslash
126.Pq Ql \e ,
127space, tab, and newline
128.Pq Sq \en
129characters.
130Consider using
131.Fl print0
132instead.
133.It Fl x
134Prevents
135.Nm
136from descending into directories that have a device number different
137than that of the file from which the descent began.
138.El
139.Pp
140It is not an error to specify more than one of
141the mutually exclusive options
142.Fl H
143and
144.Fl L .
145Where more than one of these options is specified,
146the last option given overrides the others.
147.Sh PRIMARIES
148.Bl -tag -width Ds -compact
149.It Ic -amin Ar n
150True if the difference between the file last access time and the time
151.Nm
152was started, rounded up to the next full minute, is
153.Ar n
154minutes.
155.Pp
156.It Ic -anewer Ar file
157True if the current file has a more recent last access time than
158.Ar file .
159.Pp
160.It Ic -atime Ar n
161True if the difference between the file last access time and the time
162.Nm
163was started, rounded up to the next full 24-hour period, is
164.Ar n
16524-hour periods.
166.Pp
167.It Ic -cmin Ar n
168True if the difference between the time of last change of file status
169information and the time
170.Nm
171was started, rounded up to the next full minute, is
172.Ar n
173minutes.
174.Pp
175.It Ic -cnewer Ar file
176True if the current file has a more recent last change time than
177.Ar file .
178.Pp
179.It Ic -ctime Ar n
180True if the difference between the time of last change of file status
181information and the time
182.Nm
183was started, rounded up to the next full 24-hour period, is
184.Ar n
18524-hour periods.
186.Pp
187.It Ic -delete
188Delete found files and directories.
189Always returns true.
190This executes
191from the current working directory as
192.Nm
193recurses down the tree.
194It will not attempt to delete a filename with a
195.Sq Pa /
196character in its pathname relative to
197.Sq Pa \&.
198for security reasons.
199Depth-first traversal processing is implied by this option.
200The
201.Ic -delete
202primary will fail to delete a directory if it is not empty.
203Following symlinks is incompatible with this option.
204.Pp
205.It Ic -depth
206This primary always evaluates to true.
207The same as specifying the
208.Fl d
209option.
210.Pp
211.It Ic -empty
212True if the current file or directory is empty.
213.Pp
214.It Ic -exec Ar utility Oo Ar argument ... Oc \&;
215.It Ic -exec Ar utility Oo Ar argument ... Oc {} +
216Execute the specified
217.Ar utility .
218Optional arguments may be passed to the utility.
219The expression must be terminated by a semicolon
220.Pq Ql \&;
221or a plus sign
222.Pq Ql \&+ .
223.Pp
224If terminated by a semicolon, the
225.Ar utility
226is executed once per path.
227This form of the primary evaluates to true if
228.Ar utility
229exits with a zero exit status.
230If the string
231.Qq {}
232appears anywhere in the utility name or the
233arguments it is replaced by the pathname of the current file.
234.Pp
235If terminated by a plus sign,
236the pathnames for which the
237primary is evaluated are aggregated into sets, and
238.Ar utility
239will be invoked once per set, similar to
240.Xr xargs 1 .
241This form of the primary always evaluates to true.
242If any invocation exits with a non-zero exit status, then
243.Nm
244will eventually do so as well, but this does not cause
245.Nm
246to exit early.
247The string
248.Qq {}
249must appear, and must appear last.
250Each set is limited to no more than 5,000 pathnames,
251and is also limited such that the invocation of
252.Ar utility
253does not exceed
254.Dv ARG_MAX .
255.Pp
256.It Ic -execdir Ar utility Oo Ar argument ... Oc \&;
257Identical to the first form of the
258.Ic -exec
259primary with the exception that
260.Ar utility
261will be executed from the directory that holds
262the current file.
263The filename substituted for the string
264.Qq {}
265is not qualified.
266.Pp
267.It Xo
268.Ic -flags
269.Oo - Oc Ns Ar flags
270.Xc
271The
272.Ar flags
273are comma-separated symbolic file flags (see
274.Xr chflags 1
275for a list of valid flag names).
276If the flags are preceded by a dash
277.Pq Sq - ,
278this primary evaluates to true if at least all specified
279.Ar flags
280are set in the file's flags.
281If the flags are not preceded by a dash, this primary evaluates to true if the
282flags specified exactly match those of the file.
283.Pp
284.It Ic -follow
285This primary always evaluates to true.
286The same as specifying the
287.Fl L
288option.
289.Pp
290.It Ic -fstype Ar type
291True if the file is contained in a file system of type
292.Ar type .
293Two special file system types are recognized:
294.Dq local
295and
296.Dq rdonly .
297These do not describe actual file system types;
298the former matches any file system physically mounted on the system where
299.Nm
300is being executed whereas the latter matches any file system which is
301mounted read-only.
302.Pp
303.It Ic -group Ar gname
304True if the file belongs to the group
305.Ar gname .
306If
307.Ar gname
308is numeric and there is no such group name, then
309.Ar gname
310is treated as a group ID.
311.Pp
312.It Ic -iname Ar pattern
313Identical to the
314.Ic -name
315primary except that the matching is done in a case insensitive manner.
316.Pp
317.It Ic -inum Ar n
318True if the file has inode number
319.Ar n .
320.Pp
321.It Ic -links Ar n
322True if the file has
323.Ar n
324links.
325.Pp
326.It Ic -ls
327This primary always evaluates to true.
328The following information for the current file is written to standard output:
329its inode number, size in 512-byte blocks, file permissions, number of hard
330links, owner, group, size in bytes, last modification time, and pathname.
331If the file is a block or character special file, the major and minor numbers
332will be displayed instead of the size in bytes.
333If the file is a symbolic link, the pathname of the linked-to file will be
334displayed preceded by
335.Dq -> .
336The format is identical to that produced by
337.Dq ls -dils .
338.Pp
339.It Ic -maxdepth Ar n
340True if the current search depth is less than or equal to what is specified in
341.Ar n .
342.Pp
343.It Ic -mindepth Ar n
344True if the current search depth is at least what is specified in
345.Ar n .
346.Pp
347.It Ic -mmin Ar n
348True if the difference between the file last modification time and the time
349.Nm
350was started, rounded up to the next full minute, is
351.Ar n
352minutes.
353.Pp
354.It Ic -mtime Ar n
355True if the difference between the file last modification time and the time
356.Nm
357was started, rounded up to the next full 24-hour period, is
358.Ar n
35924-hour periods.
360.Pp
361.It Ic -name Ar pattern
362True if the last component of the pathname being examined matches
363.Ar pattern ,
364which may use any of the special characters documented in
365.Xr glob 7 .
366.Pp
367.It Ic -newer Ar file
368True if the current file has a more recent last modification time than
369.Ar file .
370.Pp
371.It Ic -nogroup
372True if the file belongs to an unknown group.
373.Pp
374.It Ic -nouser
375True if the file belongs to an unknown user.
376.Pp
377.It Ic -ok Ar utility Oo Ar argument ... Oc \&;
378Identical to the
379.Ic -exec
380primary with the exception that
381.Nm
382requests user affirmation for the execution of
383.Ar utility
384by printing
385a message to the terminal and reading a response.
386If the response is other than
387.Sq y
388the command is not executed and the
389value of the
390.Ic ok
391expression is false.
392.Pp
393.It Ic -path Ar pattern
394True if the pathname being examined matches
395.Ar pattern ,
396which may use any of the special characters documented in
397.Xr glob 7 .
398Slashes
399.Pq Ql /
400are treated as normal characters and do not have to be
401matched explicitly.
402.Pp
403.It Xo
404.Ic -perm
405.Oo - Oc Ns Ar mode
406.Xc
407The
408.Ar mode
409may be either symbolic (see
410.Xr chmod 1 )
411or an octal number.
412If the mode is symbolic, a starting value of zero is assumed and the
413mode sets or clears permissions without regard to the process's file mode
414creation mask.
415If the mode is octal, only bits 07777
416.Pq Dv S_ISUID | S_ISGID | S_ISTXT | S_IRWXU | S_IRWXG | S_IRWXO
417of the file's mode bits participate
418in the comparison.
419If the mode is preceded by a dash
420.Pq Sq - ,
421this primary evaluates to true
422if at least all of the bits in the mode are set in the file's mode bits.
423If the mode is not preceded by a dash, this primary evaluates to
424true if the bits in the mode exactly match the file's mode bits.
425Note, the first character of a symbolic mode may not be a dash.
426.Pp
427.It Ic -print
428This primary always evaluates to true.
429It prints the pathname of the current file to standard output, followed
430by a newline
431.Pq Ql \en
432character.
433.Pp
434.It Ic -print0
435This primary always evaluates to true.
436It prints the pathname of the current file to standard output, followed
437by a null character, suitable for use with the
438.Fl 0
439option to
440.Xr xargs 1 .
441.Pp
442.It Ic -prune
443This primary always evaluates to true.
444It causes
445.Nm
446to not descend into the current file.
447Note, the
448.Ic -prune
449primary has no effect if the
450.Fl d
451option was specified.
452.Pp
453.It Ic -size Ar n Ns Op Cm c
454True if the file's size, rounded up, in 512-byte blocks is
455.Ar n .
456If
457.Ar n
458is followed by a
459.Sq c ,
460then the primary is true if the
461file's size is
462.Ar n
463bytes.
464.Pp
465.It Ic -type Ar t
466True if the file is of the specified type.
467Possible file types are as follows:
468.Pp
469.Bl -tag -width flag -offset indent -compact
470.It Cm b
471block special
472.It Cm c
473character special
474.It Cm d
475directory
476.It Cm f
477regular file
478.It Cm l
479symbolic link
480.It Cm p
481FIFO
482.It Cm s
483socket
484.El
485.Pp
486.It Ic -user Ar uname
487True if the file belongs to the user
488.Ar uname .
489If
490.Ar uname
491is numeric and there is no such user name, then
492.Ar uname
493is treated as a user ID.
494.Pp
495.It Ic -xdev
496This primary always evaluates to true.
497The same as specifying the
498.Fl x
499option.
500.El
501.Pp
502All primaries which take a numeric argument allow the number to be
503preceded by a plus sign
504.Pq Ql +
505or a minus sign
506.Pq Ql - .
507A preceding plus sign means
508.Dq more than n ,
509a preceding minus sign means
510.Dq less than n ,
511and neither means
512.Dq exactly n .
513Exceptions are the primaries
514.Ic mindepth
515and
516.Ic maxdepth .
517.Sh OPERATORS
518The primaries may be combined using the following operators.
519The operators are listed in order of decreasing precedence.
520.Pp
521.Bl -tag -width "(expression)" -compact
522.It Cm \&( Ar expression Cm \&)
523This evaluates to true if the parenthesized expression evaluates to
524true.
525.Pp
526.It Cm \&! Ar expression
527.It Cm -not Ar expression
528This is the unary NOT operator.
529It evaluates to true if the expression is false.
530.Pp
531.It Ar expression Cm -a Ar expression
532.It Ar expression Cm -and Ar expression
533.It Ar expression expression
534The logical AND operator.
535As it is implied by the juxtaposition of two expressions it does not
536have to be specified.
537The expression evaluates to true if both expressions are true.
538The second expression is not evaluated if the first expression is false.
539.Pp
540.It Ar expression Cm -o Ar expression
541.It Ar expression Cm -or Ar expression
542The logical OR operator.
543The expression evaluates to true if either the first or the second expression
544is true.
545The second expression is not evaluated if the first expression is true.
546.El
547.Pp
548Operators, primaries, and arguments to primaries must be separate
549arguments to
550.Nm find ,
551i.e. they should be separated by whitespace.
552.Sh EXIT STATUS
553The
554.Nm
555utility exits with a value of 0 on successful traversal of all path operands
556or with a value >0 if an error occurred.
557.Sh EXAMPLES
558Print out a list of all the files whose names end in
559.Dq \&.c :
560.Pp
561.Dl "$ find / -name '*.c'"
562.Pp
563Print out a list of all the files which are not both newer than
564.Dq ttt
565and owned by
566.Dq wnj :
567.Pp
568.Dl "$ find / \e! \e( -newer ttt -user wnj \e)"
569.Pp
570Print out a list of all core files on local file systems:
571.Pp
572.Dl "$ find / \e! -fstype local -prune -o -name '*.core'"
573.Pp
574Find all files in
575.Pa /usr/src
576ending in a dot and single digit, but skip directory
577.Pa /usr/src/gnu :
578.Pp
579.Dl "$ find /usr/src -path /usr/src/gnu -prune -o -name \e*.[0-9]"
580.Pp
581Find and remove all *.jpg and *.gif files under the current working
582directory:
583.Pp
584.Dl "$ find . \e( -name \e*.jpg -o -name \e*.gif \e) -exec rm {} +"
585or
586.Dl "$ find . \e( -name \e*.jpg -o -name \e*.gif \e) -delete"
587.Sh SEE ALSO
588.Xr chflags 1 ,
589.Xr chmod 1 ,
590.Xr locate 1 ,
591.Xr ls 1 ,
592.Xr whereis 1 ,
593.Xr which 1 ,
594.Xr xargs 1 ,
595.Xr stat 2 ,
596.Xr fts_open 3 ,
597.Xr glob 7 ,
598.Xr symlink 7
599.Sh STANDARDS
600The
601.Nm
602utility is compliant with the
603.St -p1003.1-2008
604specification.
605.Pp
606The options
607.Op Fl dfhXx ,
608primaries
609.Ic -amin ,
610.Ic -anewer ,
611.Ic -cmin ,
612.Ic -cnewer ,
613.Ic -delete ,
614.Ic -empty ,
615.Ic -execdir ,
616.Ic -flags ,
617.Ic -follow ,
618.Ic -fstype ,
619.Ic -iname ,
620.Ic -inum ,
621.Ic -ls ,
622.Ic -maxdepth ,
623.Ic -mindepth ,
624.Ic -mmin ,
625and
626.Ic -print0 ,
627and operators
628.Fl and ,
629.Fl not ,
630and
631.Fl or ,
632are extensions to that specification.
633.Pp
634Historically, the
635.Fl d ,
636.Fl L ,
637and
638.Fl x
639options were implemented using the primaries
640.Ic -depth ,
641.Ic -follow ,
642and
643.Ic -xdev .
644These primaries always evaluated to true.
645As they were really global variables that took effect before the traversal
646began, some legal expressions could have unexpected results.
647An example is the expression
648.Dq -print -o -depth .
649As
650.Cm -print
651always evaluates to true, the standard order of evaluation
652implies that
653.Cm -depth
654would never be evaluated.
655This is not the case.
656.Pp
657Historic implementations of the
658.Ic -exec
659and
660.Ic -ok
661primaries did not replace the string
662.Qq {}
663in the utility name or the
664utility arguments if it had preceding or following non-whitespace characters.
665This version replaces it no matter where in the utility name or arguments
666it appears.
667.Sh HISTORY
668A
669.Nm
670command appeared in
671.At v1 .
672.Sh CAVEATS
673The special characters used by
674.Nm
675are also special characters to many shell programs.
676In particular, the characters
677.Ql * ,
678.Ql \&[ ,
679.Ql \&] ,
680.Ql \&? ,
681.Ql \&( ,
682.Ql \&) ,
683.Ql \&! ,
684.Ql \e ,
685and
686.Ql \&;
687may have to be escaped from the shell.
688.Pp
689As file names may contain whitespace and shell metacharacters,
690passing the output of
691.Nm
692to other programs requires some care:
693.Pp
694.Dl "$ find . -name \e*.jpg | xargs rm"
695or
696.Dl "$ rm `find . -name \e*.jpg`"
697.Pp
698would, given files
699.Dq important\ .jpg
700and
701.Dq important ,
702remove
703.Dq important .
704Use the
705.Ic -print0
706or
707.Ic -exec
708primaries instead.
709.Pp
710As there is no delimiter separating options and file names or file
711names and the
712.Ar expression ,
713it is difficult to specify files named
714.Dq -xdev
715or
716.Sq \&! .
717These problems are handled by the
718.Fl f
719option and the
720.Xr getopt 3
721.Sq --
722construct.
723