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