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