xref: /freebsd/usr.bin/find/find.1 (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
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 none of
269.Ic -exec ,
270.Ic -ls ,
271.Ic -print0 ,
272or
273.Ic \&-ok
274are specified.
275.It Ic -print0
276This primary always evaluates to true.
277It prints the pathname of the current file to standard output, followed by a
278.Tn ASCII
279.Tn NUL
280character (character code 0).
281.It Ic -prune
282This primary always evaluates to true.
283It causes
284.Nm find
285to not descend into the current file.
286Note, the
287.Ic -prune
288primary has no effect if the
289.Fl d
290option was specified.
291.It Ic -size Ar n Ns Op Cm c
292True if the file's size, rounded up, in 512\-byte blocks is
293.Ar n  .
294If
295.Ar n
296is followed by a ``c'', then the primary is true if the
297file's size is
298.Ar n
299bytes.
300.It Ic -type Ar t
301True if the file is of the specified type.
302Possible file types are as follows:
303.Pp
304.Bl -tag -width flag -offset indent -compact
305.It Cm b
306block special
307.It Cm c
308character special
309.It Cm d
310directory
311.It Cm f
312regular file
313.It Cm l
314symbolic link
315.It Cm p
316FIFO
317.It Cm s
318socket
319.El
320.Pp
321.It Ic -user Ar uname
322True if the file belongs to the user
323.Ar uname  .
324If
325.Ar uname
326is numeric and there is no such user name, then
327.Ar uname
328is treated as a user id.
329.El
330.Pp
331All primaries which take a numeric argument allow the number to be
332preceded by a plus sign (``+'') or a minus sign (``\-'').
333A preceding plus sign means ``more than n'', a preceding minus sign means
334``less than n'' and neither means ``exactly n'' .
335.Sh OPERATORS
336The primaries may be combined using the following operators.
337The operators are listed in order of decreasing precedence.
338.Bl -tag -width (expression)
339.It Cm \&( Ns Ar expression Ns Cm \&)
340This evaluates to true if the parenthesized expression evaluates to
341true.
342.Pp
343.It Cm \&! Ns Ar expression
344This is the unary
345.Tn NOT
346operator.
347It evaluates to true if the expression is false.
348.Pp
349.It Ar expression Cm -and Ar expression
350.It Ar expression expression
351The
352.Cm -and
353operator is the logical
354.Tn AND
355operator.
356As it is implied by the juxtaposition of two expressions it does not
357have to be specified.
358The expression evaluates to true if both expressions are true.
359The second expression is not evaluated if the first expression is false.
360.Pp
361.It Ar expression Cm -or Ar expression
362The
363.Cm -or
364operator is the logical
365.Tn OR
366operator.
367The expression evaluates to true if either the first or the second expression
368is true.
369The second expression is not evaluated if the first expression is true.
370.El
371.Pp
372All operands and primaries must be separate arguments to
373.Nm find  .
374Primaries which themselves take arguments expect each argument
375to be a separate argument to
376.Nm find  .
377.Sh EXAMPLES
378.Pp
379The following examples are shown as given to the shell:
380.Bl -tag -width findx
381.It Li "find  /  \e!  -name  \*q*.c\*q  -print"
382Print out a list of all the files whose names do not end in ``.c''.
383.It Li "find  /  -newer  ttt  -user  wnj  -print"
384Print out a list of all the files owned by user ``wnj'' that are newer
385than the file ``ttt''.
386.It Li "find  /  \e!  \e(  -newer  ttt  -user  wnj  \e)  -print"
387Print out a list of all the files which are not both newer than ``ttt''
388and owned by ``wnj''.
389.It Li "find  /  \e(  -newer  ttt  -or  -user wnj  \e)  -print"
390Print out a list of all the files that are either owned by ``wnj'' or
391that are newer than ``ttt''.
392.El
393.Sh SEE ALSO
394.Xr chmod 1 ,
395.Xr locate 1 ,
396.Xr stat 2 ,
397.Xr fts 3 ,
398.Xr getgrent 3 ,
399.Xr getpwent 3 ,
400.Xr strmode 3 ,
401.Xr symlink 7
402.Sh STANDARDS
403The
404.Nm find
405utility syntax is a superset of the syntax specified by the
406.St -p1003.2
407standard.
408.Pp
409The
410.Fl s
411and
412.Fl X
413options and the
414.Ic -inum ,
415.Ic -print0 ,
416and
417.Ic -ls
418primaries are extensions to
419.St -p1003.2 .
420.Pp
421Historically, the
422.Fl d ,
423.Fl h
424and
425.Fl x
426options were implemented using the primaries ``\-depth'', ``\-follow'',
427and ``\-xdev''.
428These primaries always evaluated to true.
429As they were really global variables that took effect before the traversal
430began, some legal expressions could have unexpected results.
431An example is the expression ``\-print \-o \-depth''.
432As \-print always evaluates to true, the standard order of evaluation
433implies that \-depth would never be evaluated.
434This is not the case.
435.Pp
436The operator ``-or'' was implemented as ``\-o'', and the operator ``-and''
437was implemented as ``\-a''.
438.Pp
439Historic implementations of the
440.Ic exec
441and
442.Ic ok
443primaries did not replace the string ``{}'' in the utility name or the
444utility arguments if it had preceding or following non-whitespace characters.
445This version replaces it no matter where in the utility name or arguments
446it appears.
447.Sh BUGS
448The special characters used by
449.Nm find
450are also special characters to many shell programs.
451In particular, the characters ``*'', ``['', ``]'', ``?'', ``('', ``)'',
452``!'', ``\e'' and ``;'' may have to be escaped from the shell.
453.Pp
454As there is no delimiter separating options and file names or file
455names and the
456.Ar expression ,
457it is difficult to specify files named ``-xdev'' or ``!''.
458These problems are handled by the
459.Fl f
460option and the
461.Xr getopt 3
462``--'' construct.
463