xref: /netbsd-src/usr.bin/find/find.1 (revision e4d7c2e329d54c97e0c0bd3016bbe74f550c3d5e)
1.\"	$NetBSD: find.1,v 1.23 1999/07/20 09:02:24 kleink 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. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"	This product includes software developed by the University of
20.\"	California, Berkeley and its contributors.
21.\" 4. Neither the name of the University nor the names of its contributors
22.\"    may be used to endorse or promote products derived from this software
23.\"    without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35.\" SUCH DAMAGE.
36.\"
37.\"	from: @(#)find.1	8.7 (Berkeley) 5/9/95
38.\"
39.Dd January 4, 1999
40.Dt FIND 1
41.Os
42.Sh NAME
43.Nm find
44.Nd walk a file hierarchy
45.Sh SYNOPSIS
46.Nm find
47.Op Fl H | Fl L | Fl P
48.Op Fl Xdhx
49.Op Fl f Ar file
50.Op Ar file ...
51.Ar expression
52.Sh DESCRIPTION
53.Nm
54recursively descends the directory tree for each
55.Ar file
56listed, evaluating an
57.Ar expression
58(composed of the ``primaries'' and ``operands'' listed below) in terms
59of each file in the tree.
60.Pp
61The options are as follows:
62.Pp
63.Bl -tag -width Ds
64.It Fl H
65The
66.Fl H
67option causes the file information and file type (see
68.Xr stat 2 ) ,
69returned for each symbolic link encountered on the command line to be
70those of the file referenced by the link, not the link itself.
71If the referenced file does not exist, the file information and type will
72be for the link itself.  File information of all symbolic links not on
73the command line is that of the link itself.
74.It Fl L
75The
76.Fl L
77option causes the file information and file type (see
78.Xr stat 2)
79returned for each symbolic link to be those of the file referenced by the
80link, not the link itself.
81If the referenced file does not exist, the file information and type will
82be for the link itself.
83.It Fl P
84The
85.Fl P
86option causes the file information and file type (see
87.Xr stat 2)
88returned for each symbolic link to be those of the link itself.
89.It Fl X
90The
91.Fl X
92option is a modification to permit
93.Nm
94to be safely used in conjunction with
95.Xr xargs 1 .
96If a file name contains any of the delimiting characters used by
97.Nm xargs ,
98a diagnostic message is displayed on standard error, and the file
99is skipped.
100The delimiting characters include single (`` ' '') and double (`` " '')
101quotes, backslash (``\e''), space, tab and newline characters.
102Alternatively, the
103.Ic -print0
104or
105.Ic -printx
106primaries can be used to format the output in a way that
107.Nm xargs
108can accept.
109.It Fl d
110The
111.Fl d
112option causes
113.Nm
114to perform a depth\-first traversal, i.e. directories
115are visited in post\-order and all entries in a directory will be acted
116on before the directory itself.
117By default,
118.Nm
119visits directories in pre\-order, i.e. before their contents.
120Note, the default is
121.Ar not
122a breadth\-first traversal.
123.It Fl f
124The
125.Fl f
126option specifies a file hierarchy for
127.Nm
128to traverse.
129File hierarchies may also be specified as the operands immediately
130following the options.
131.It Fl h
132The
133.Fl h
134option causes the file information and file type (see
135.Xr stat  2  ) ,
136returned for each symbolic link to be those of the file referenced by the
137link, not the link itself.
138If the referenced file does not exist, the file information and type will
139be for the link itself.
140.It Fl x
141The
142.Fl x
143option prevents
144.Nm
145from descending into directories that have a device number different
146than that of the file from which the descent began.
147.El
148.Sh PRIMARIES
149.Bl -tag -width Ds
150.It Ic -amin Ar n
151True if the difference between the file last access time and the time
152.Nm
153was started, rounded up to the next full minute, is
154.Ar n
155minutes.
156.It Ic -atime Ar n
157True if the difference between the file last access time and the time
158.Nm
159was started, rounded up to the next full 24\-hour period, is
160.Ar n
16124\-hour periods.
162.It Ic -cmin Ar n
163True if the difference between the time of last change of file status
164information and the time
165.Nm
166was started, rounded up to the next full minute, is
167.Ar n
168minutes.
169.It Ic -ctime Ar n
170True if the difference between the time of last change of file status
171information and the time
172.Nm
173was started, rounded up to the next full 24\-hour period, is
174.Ar n
17524\-hour periods.
176.It Ic -exec Ar utility Op argument ... ;
177True if the program named
178.Ar utility
179returns a zero value as its exit status.
180Optional arguments may be passed to the utility.
181The expression must be terminated by a semicolon (``;'').
182If the string ``{}'' appears anywhere in the utility name or the
183arguments it is replaced by the pathname of the current file.
184.Ar Utility
185will be executed from the directory from which
186.Nm
187was executed.
188.It Xo
189.Ic -flags
190.Op Fl
191.Ns Ar flags
192.Xc
193If
194.Ar flags
195are preceded by a dash (``\-''), this primary evaluates to true
196if at least all of the bits in
197.Ar flags
198are set in the file's flags bits.
199If
200.Ar flags
201are not preceded by a dash, this primary evaluates to true if
202the bits in
203.Ar flags
204exactly match the file's flags bits.
205If
206.Ar flags
207is
208.Dq none ,
209files with no flags bits set are matched.
210(See
211.Xr chflags 1
212for more information about file flags.)
213.It Ic -follow
214Follow symbolic links.
215.It Ic -fstype Ar type
216True if the file is contained in a file system of type
217.Ar type .
218The
219.Xr sysctl 8
220command can be used to find out the types of filesystems
221that are available on the system:
222.Bd -literal -offset indent
223sysctl vfs
224.Ed
225In addition, there are two pseudo-types, ``local'' and ``rdonly''.
226The former matches any file system physically mounted on the system where
227the
228.Nm
229is being executed, and the latter matches any file system which is
230mounted read-only.
231.It Ic -group Ar gname
232True if the file belongs to the group
233.Ar gname  .
234If
235.Ar gname
236is numeric and there is no such group name, then
237.Ar gname
238is treated as a group id.
239.It Ic -inum Ar n
240True if the file has inode number
241.Ar n  .
242.It Ic -iregex Ar regexp
243True if the path name of the current file matches the case-insensitive
244basic regular expression
245.Pq see Xr re_format 7
246.Ar regexp .
247This is a match on the whole path, not a search for the regular expression
248within the path.
249.It Ic -links Ar n
250True if the file has
251.Ar n
252links.
253.It Ic -ls
254This primary always evaluates to true.
255The following information for the current file is written to standard output:
256its inode number, size in 512\-byte blocks, file permissions, number of hard
257links, owner, group, size in bytes, last modification time, and pathname.
258If the file is a block or character special file, the major and minor numbers
259will be displayed instead of the size in bytes.
260If the file is a symbolic link, the pathname of the linked\-to file will be
261displayed preceded by ``\->''.
262The format is identical to that produced by ``ls \-dgils''.
263.It Ic -mmin Ar n
264True if the difference between the file last modification time and the time
265.Nm
266was started, rounded up to the next full minute, is
267.Ar n
268minutes.
269.It Ic -mtime Ar n
270True if the difference between the file last modification time and the time
271.Nm
272was started, rounded up to the next full 24\-hour period, is
273.Ar n
27424\-hour periods.
275.It Ic \&-ok Ar utility Op argument ... ;
276The
277.Ic \&-ok
278primary is identical to the
279.Ic -exec
280primary with the exception that
281.Nm
282requests user affirmation for the execution of the utility by printing
283a message to the terminal and reading a response.
284If the response is other than ``y'' the command is not executed and the
285value of the
286.Ar \&ok
287expression is false.
288.It Ic -name Ar pattern
289True if the last component of the pathname being examined matches
290.Ar pattern  .
291Special shell pattern matching characters
292.Po
293.Dq \&[ ,
294.Dq \&] ,
295.Dq \&* ,
296.Dq \&?
297.Pc
298may be used as part of
299.Ar pattern  .
300These characters may be matched explicitly by escaping them with a
301backslash (``\e'').
302.It Ic -newer Ar file
303True if the current file has a more recent last modification time than
304.Ar file  .
305.It Ic -nouser
306True if the file belongs to an unknown user.
307.It Ic -nogroup
308True if the file belongs to an unknown group.
309.It Ic -path Ar pattern
310True if the pathname being examined matches
311.Ar pattern  .
312Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'')
313may be used as part of
314.Ar pattern  .
315These characters may be matched explicitly by escaping them with a
316backslash (``\e'').
317Slashes (``/'') are treated as normal characters and do not have to be
318matched explicitly.
319.It Xo
320.Ic -perm
321.Op Fl
322.Ns Ar mode
323.Xc
324The
325.Ar mode
326may be either symbolic (see
327.Xr chmod  1  )
328or an octal number.
329If the mode is symbolic, a starting value of zero is assumed and the
330mode sets or clears permissions without regard to the process' file mode
331creation mask.
332If the mode is octal, only bits 07777
333.Pf ( Dv S_ISUID
334|
335.Dv S_ISGID
336|
337.Dv S_ISTXT
338|
339.Dv S_IRWXU
340|
341.Dv S_IRWXG
342|
343.Dv S_IRWXO )
344of the file's mode bits participate
345in the comparison.
346If the mode is preceded by a dash (``\-''), this primary evaluates to true
347if at least all of the bits in the mode are set in the file's mode bits.
348If the mode is not preceded by a dash, this primary evaluates to true if
349the bits in the mode exactly match the file's mode bits.
350Note, the first character of a symbolic mode may not be a dash (``\-'').
351.It Ic -print
352This primary always evaluates to true.
353It prints the pathname of the current file to standard output, followed
354by a newline character.
355If none of
356.Ic -exec ,
357.Ic -ls ,
358.Ic -ok ,
359.Ic -print0 ,
360nor
361.Ic -printx
362is specified, the given expression shall be effectively replaced by
363.Cm \&( Ns Ar given\& expression Ns Cm \&)
364.Ic -print .
365.It Ic -print0
366This primary always evaluates to true.
367It prints the pathname of the current file to standard output, followed
368by a null character.
369.It Ic -printx
370This primary always evaluates to true.
371It prints the pathname of the current file to standard output,
372with each space, tab, newline, backslash, and single or double
373quotation mark prefixed by a backslash, so the output of
374.Nm find
375can safely be used as input to
376.Nm xargs .
377.It Ic -prune
378This primary always evaluates to true.
379It causes
380.Nm
381to not descend into the current file.
382Note, the
383.Ic -prune
384primary has no effect if the
385.Fl d
386option was specified.
387.It Ic -regex Ar regexp
388True if the path name of the current file matches the case-sensitive
389basic regular expression
390.Pq see Xr re_format 7
391.Ar regexp .
392This is a match on the whole path, not a search for the regular expression
393within the path.
394.It Ic -size Ar n Ns Op Cm c
395True if the file's size, rounded up, in 512\-byte blocks is
396.Ar n  .
397If
398.Ar n
399is followed by a ``c'', then the primary is true if the
400file's size is
401.Ar n
402bytes.
403.It Ic -type Ar t
404True if the file is of the specified type.
405Possible file types are as follows:
406.Pp
407.Bl -tag -width flag -offset indent -compact
408.It Cm W
409whiteout
410.It Cm b
411block special
412.It Cm c
413character special
414.It Cm d
415directory
416.It Cm f
417regular file
418.It Cm l
419symbolic link
420.It Cm p
421FIFO
422.It Cm s
423socket
424.El
425.Pp
426.It Ic -user Ar uname
427True if the file belongs to the user
428.Ar uname  .
429If
430.Ar uname
431is numeric and there is no such user name, then
432.Ar uname
433is treated as a user id.
434.El
435.Pp
436All primaries which take a numeric argument allow the number to be
437preceded by a plus sign (``+'') or a minus sign (``\-'').
438A preceding plus sign means ``more than n'', a preceding minus sign means
439``less than n'' and neither means ``exactly n'' .
440.Sh OPERATORS
441The primaries may be combined using the following operators.
442The operators are listed in order of decreasing precedence.
443.Bl -tag -width (expression)
444.It Cm \&( Ns Ar expression Ns Cm \&)
445This evaluates to true if the parenthesized expression evaluates to
446true.
447.Pp
448.It Cm \&! Ns Ar expression
449This is the unary
450.Tn NOT
451operator.
452It evaluates to true if the expression is false.
453.Pp
454.It Ar expression Cm -and Ar expression
455.It Ar expression expression
456The
457.Cm -and
458operator is the logical
459.Tn AND
460operator.
461As it is implied by the juxtaposition of two expressions it does not
462have to be specified.
463The expression evaluates to true if both expressions are true.
464The second expression is not evaluated if the first expression is false.
465.Pp
466.It Ar expression Cm -or Ar expression
467The
468.Cm -or
469operator is the logical
470.Tn OR
471operator.
472The expression evaluates to true if either the first or the second expression
473is true.
474The second expression is not evaluated if the first expression is true.
475.El
476.Pp
477All operands and primaries must be separate arguments to
478.Nm ""  .
479Primaries which themselves take arguments expect each argument
480to be a separate argument to
481.Nm ""  .
482.Sh EXAMPLES
483.Pp
484The following examples are shown as given to the shell:
485.Bl -tag -width findx
486.It Li "find  /  \e!  -name  \*q*.c\*q  -print"
487Print out a list of all the files whose names do not end in ``.c''.
488.It Li "find  /  -newer  ttt  -user  wnj  -print"
489Print out a list of all the files owned by user ``wnj'' that are newer
490than the file ``ttt''.
491.It Li "find  /  \e!  \e(  -newer  ttt  -user  wnj  \e)  -print"
492Print out a list of all the files which are not both newer than ``ttt''
493and owned by ``wnj''.
494.It Li "find  /  \e(  -newer  ttt  -or  -user wnj  \e)  -print"
495Print out a list of all the files that are either owned by ``wnj'' or
496that are newer than ``ttt''.
497.El
498.Sh SEE ALSO
499.Xr chflags 1 ,
500.Xr chmod 1 ,
501.Xr locate 1 ,
502.Xr stat 2 ,
503.Xr fts 3 ,
504.Xr getpwent 3 ,
505.Xr getgrent 3 ,
506.Xr strmode 3 ,
507.Xr symlink 7
508.Sh STANDARDS
509The
510.Nm
511utility syntax is a superset of the syntax specified by the
512.St -p1003.2
513standard.
514.Pp
515The options and the
516.Ic -amin ,
517.Ic -cmin ,
518.Ic -follow ,
519.Ic -fstype ,
520.Ic -inum ,
521.Ic -iregex ,
522.Ic -links ,
523.Ic -ls ,
524.Ic -mmin ,
525.Ic -print0
526and
527.Ic -regex
528primaries are extensions to
529.St -p1003.2 .
530.Pp
531Historically, the
532.Fl d ,
533.Fl h
534and
535.Fl x
536options were implemented using the primaries ``\-depth'', ``\-follow'',
537and ``\-xdev''.
538These primaries always evaluated to true.
539As they were really global variables that took effect before the traversal
540began, some legal expressions could have unexpected results.
541An example is the expression ``\-print \-o \-depth''.
542As \-print always evaluates to true, the standard order of evaluation
543implies that \-depth would never be evaluated.
544This is not the case.
545.Pp
546The operator ``-or'' was implemented as ``\-o'', and the operator ``-and''
547was implemented as ``\-a''.
548.Pp
549Historic implementations of the
550.Ic -exec
551and
552.Ic -ok
553primaries did not replace the string ``{}'' in the utility name or the
554utility arguments if it had preceding or following non-whitespace characters.
555This version replaces it no matter where in the utility name or arguments
556it appears.
557.Sh BUGS
558The special characters used by
559.Nm
560are also special characters to many shell programs.
561In particular, the characters ``*'', ``['', ``]'', ``?'', ``('', ``)'',
562``!'', ``\e'' and ``;'' may have to be escaped from the shell.
563.Pp
564As there is no delimiter separating options and file names or file
565names and the
566.Ar expression ,
567it is difficult to specify files named ``-xdev'' or ``!''.
568These problems are handled by the
569.Fl f
570option and the
571.Xr getopt 3
572``--'' construct.
573.Sh HISTORY
574A much simpler
575.Nm find
576command appeared in First Edition AT&T Unix.
577The syntax had become similar to the present version by
578the time of the Fifth Edition.
579