xref: /freebsd/usr.bin/find/find.1 (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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.7 (Berkeley) 5/9/95
36.\"	$Id: find.1,v 1.10 1997/05/19 18:16:00 jdp Exp $
37.\"
38.Dd May 9, 1995
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.
88This is the default.
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.Xr 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.
102.It Fl d
103The
104.Fl d
105option causes
106.Nm find
107to perform a depth\-first traversal, i.e. directories
108are visited in post\-order and all entries in a directory will be acted
109on before the directory itself.
110By default,
111.Nm find
112visits directories in pre\-order, i.e. before their contents.
113Note, the default is
114.Ar not
115a breadth\-first traversal.
116.It Fl f
117The
118.Fl f
119option specifies a file hierarchy for
120.Nm find
121to traverse.
122File hierarchies may also be specified as the operands immediately
123following the options.
124.It Fl x
125The
126.Fl x
127option prevents
128.Nm find
129from descending into directories that have a device number different
130than that of the file from which the descent began.
131.El
132.Sh PRIMARIES
133.Bl -tag -width Ds
134.It Ic -atime Ar n
135True if the difference between the file last access time and the time
136.Nm find
137was started, rounded up to the next full 24\-hour period, is
138.Ar n
13924\-hour periods.
140.It Ic -ctime Ar n
141True if the difference between the time of last change of file status
142information and the time
143.Nm find
144was started, rounded up to the next full 24\-hour period, is
145.Ar n
14624\-hour periods.
147.It Ic -delete
148Delete found files and/or directories.  Always returns True.  This executes
149from the current working directory as
150.Nm
151recurses down the tree.  It will not attempt to delete a filename with a ``/''
152character in its pathname relative to "." for security reasons.
153Depth\-first traversal processing is implied by this option.
154.It Ic -exec Ar utility Op argument ... ;
155True if the program named
156.Ar utility
157returns a zero value as its exit status.
158Optional arguments may be passed to the utility.
159The expression must be terminated by a semicolon (``;'').
160If the string ``{}'' appears anywhere in the utility name or the
161arguments it is replaced by the pathname of the current file.
162.Ar Utility
163will be executed from the directory from which
164.Nm find
165was executed.
166.It Ic -execdir Ar utility Op argument ... ;
167The
168.Ic \&-execdir
169primary is identical to the
170.Ic -exec
171primary with the exception that
172.Ar Utility
173will be executed from the directory that holds
174the current file.  The filename substituted for
175the string ``{}'' is not qualified.
176.It Ic -fstype Ar type
177True if the file is contained in a file system of type
178.Ar type .
179The
180.Xr sysctl 8
181command can be used to find out the types of filesystems
182that are available on the system:
183.Bd -literal -offset indent
184sysctl vfs
185.Ed
186In addition, there are two pseudo-types, ``local'' and ``rdonly''.
187The former matches any file system physically mounted on the system where
188the
189.Nm find
190is being executed and the latter matches any file system which is
191mounted read-only.
192.It Ic -group Ar gname
193True if the file belongs to the group
194.Ar gname  .
195If
196.Ar gname
197is numeric and there is no such group name, then
198.Ar gname
199is treated as a group id.
200.It Ic -inum Ar n
201True if the file has inode number
202.Ar n  .
203.It Ic -links Ar n
204True if the file has
205.Ar n
206links.
207.It Ic -ls
208This primary always evaluates to true.
209The following information for the current file is written to standard output:
210its inode number, size in 512\-byte blocks, file permissions, number of hard
211links, owner, group, size in bytes, last modification time, and pathname.
212If the file is a block or character special file, the major and minor numbers
213will be displayed instead of the size in bytes.
214If the file is a symbolic link, the pathname of the linked\-to file will be
215displayed preceded by ``\->''.
216The format is identical to that produced by ``ls \-dgils''.
217.It Ic -mtime Ar n
218True if the difference between the file last modification time and the time
219.Nm find
220was started, rounded up to the next full 24\-hour period, is
221.Ar n
22224\-hour periods.
223.It Ic \&-ok Ar utility Op argument ... ;
224The
225.Ic \&-ok
226primary is identical to the
227.Ic -exec
228primary with the exception that
229.Nm find
230requests user affirmation for the execution of the utility by printing
231a message to the terminal and reading a response.
232If the response is other than ``y'' the command is not executed and the
233value of the
234.Ar \&ok
235expression is false.
236.It Ic -name Ar pattern
237True if the last component of the pathname being examined matches
238.Ar pattern  .
239Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'')
240may be used as part of
241.Ar pattern  .
242These characters may be matched explicitly by escaping them with a
243backslash (``\e'').
244.It Ic -newer Ar file
245True if the current file has a more recent last modification time than
246.Ar file  .
247.It Ic -nouser
248True if the file belongs to an unknown user.
249.It Ic -nogroup
250True if the file belongs to an unknown group.
251.It Ic -path Ar pattern
252True if the pathname being examined matches
253.Ar pattern  .
254Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'')
255may be used as part of
256.Ar pattern  .
257These characters may be matched explicitly by escaping them with a
258backslash (``\e'').
259Slashes (``/'') are treated as normal characters and do not have to be
260matched explicitly.
261.It Ic -perm Op Fl Ns Ar mode
262The
263.Ar mode
264may be either symbolic (see
265.Xr chmod  1  )
266or an octal number.
267If the mode is symbolic, a starting value of zero is assumed and the
268mode sets or clears permissions without regard to the process' file mode
269creation mask.
270If the mode is octal, only bits 07777
271.Pf ( Dv S_ISUID
272|
273.Dv S_ISGID
274|
275.Dv S_ISTXT
276|
277.Dv S_IRWXU
278|
279.Dv S_IRWXG
280|
281.Dv S_IRWXO )
282of the file's mode bits participate
283in the comparison.
284If the mode is preceded by a dash (``\-''), this primary evaluates to true
285if at least all of the bits in the mode are set in the file's mode bits.
286If the mode is not preceded by a dash, this primary evaluates to true if
287the bits in the mode exactly match the file's mode bits.
288Note, the first character of a symbolic mode may not be a dash (``\-'').
289.It Ic -print
290This primary always evaluates to true.
291It prints the pathname of the current file to standard output.
292If none of
293.Ic -exec ,
294.Ic -ls ,
295.Ic -print0 ,
296or
297.Ic \&-ok
298is specified, the given expression shall be effectively replaced by
299.Cm \&( Ns Ar given\& expression Ns Cm \&)
300.Ic -print .
301.It Ic -print0
302This primary always evaluates to true.
303It prints the pathname of the current file to standard output, followed by an
304.Tn ASCII
305.Tn NUL
306character (character code 0).
307.It Ic -prune
308This primary always evaluates to true.
309It causes
310.Nm find
311to not descend into the current file.
312Note, the
313.Ic -prune
314primary has no effect if the
315.Fl d
316option was specified.
317.It Ic -size Ar n Ns Op Cm c
318True if the file's size, rounded up, in 512\-byte blocks is
319.Ar n  .
320If
321.Ar n
322is followed by a ``c'', then the primary is true if the
323file's size is
324.Ar n
325bytes.
326.It Ic -type Ar t
327True if the file is of the specified type.
328Possible file types are as follows:
329.Pp
330.Bl -tag -width flag -offset indent -compact
331.It Cm b
332block special
333.It Cm c
334character special
335.It Cm d
336directory
337.It Cm f
338regular file
339.It Cm l
340symbolic link
341.It Cm p
342FIFO
343.It Cm s
344socket
345.El
346.Pp
347.It Ic -user Ar uname
348True if the file belongs to the user
349.Ar uname  .
350If
351.Ar uname
352is numeric and there is no such user name, then
353.Ar uname
354is treated as a user id.
355.El
356.Pp
357All primaries which take a numeric argument allow the number to be
358preceded by a plus sign (``+'') or a minus sign (``\-'').
359A preceding plus sign means ``more than n'', a preceding minus sign means
360``less than n'' and neither means ``exactly n'' .
361.Sh OPERATORS
362The primaries may be combined using the following operators.
363The operators are listed in order of decreasing precedence.
364.Bl -tag -width (expression)
365.It Cm \&( Ns Ar expression Ns Cm \&)
366This evaluates to true if the parenthesized expression evaluates to
367true.
368.Pp
369.It Cm \&! Ns Ar expression
370This is the unary
371.Tn NOT
372operator.
373It evaluates to true if the expression is false.
374.Pp
375.It Ar expression Cm -and Ar expression
376.It Ar expression expression
377The
378.Cm -and
379operator is the logical
380.Tn AND
381operator.
382As it is implied by the juxtaposition of two expressions it does not
383have to be specified.
384The expression evaluates to true if both expressions are true.
385The second expression is not evaluated if the first expression is false.
386.Pp
387.It Ar expression Cm -or Ar expression
388The
389.Cm -or
390operator is the logical
391.Tn OR
392operator.
393The expression evaluates to true if either the first or the second expression
394is true.
395The second expression is not evaluated if the first expression is true.
396.El
397.Pp
398All operands and primaries must be separate arguments to
399.Nm find  .
400Primaries which themselves take arguments expect each argument
401to be a separate argument to
402.Nm find  .
403.Sh EXAMPLES
404.Pp
405The following examples are shown as given to the shell:
406.Bl -tag -width findx
407.It Li "find  /  \e!  -name  \*q*.c\*q  -print"
408Print out a list of all the files whose names do not end in ``.c''.
409.It Li "find  /  -newer  ttt  -user  wnj  -print"
410Print out a list of all the files owned by user ``wnj'' that are newer
411than the file ``ttt''.
412.It Li "find  /  \e!  \e(  -newer  ttt  -user  wnj  \e)  -print"
413Print out a list of all the files which are not both newer than ``ttt''
414and owned by ``wnj''.
415.It Li "find  /  \e(  -newer  ttt  -or  -user wnj  \e)  -print"
416Print out a list of all the files that are either owned by ``wnj'' or
417that are newer than ``ttt''.
418.El
419.Sh SEE ALSO
420.Xr chmod 1 ,
421.Xr locate 1 ,
422.Xr stat 2 ,
423.Xr fts 3 ,
424.Xr getgrent 3 ,
425.Xr getpwent 3 ,
426.Xr strmode 3 ,
427.Xr symlink 7
428.Sh STANDARDS
429The
430.Nm find
431utility syntax is a superset of the syntax specified by the
432.St -p1003.2
433standard.
434.Pp
435The
436.Fl s
437and
438.Fl X
439options and the
440.Ic -inum ,
441.Ic -print0 ,
442.Ic -delete ,
443and
444.Ic -ls
445primaries are extensions to
446.St -p1003.2 .
447.Pp
448Historically, the
449.Fl d ,
450.Fl h
451and
452.Fl x
453options were implemented using the primaries ``\-depth'', ``\-follow'',
454and ``\-xdev''.
455These primaries always evaluated to true.
456As they were really global variables that took effect before the traversal
457began, some legal expressions could have unexpected results.
458An example is the expression ``\-print \-o \-depth''.
459As \-print always evaluates to true, the standard order of evaluation
460implies that \-depth would never be evaluated.
461This is not the case.
462.Pp
463The operator ``-or'' was implemented as ``\-o'', and the operator ``-and''
464was implemented as ``\-a''.
465.Pp
466Historic implementations of the
467.Ic exec
468and
469.Ic ok
470primaries did not replace the string ``{}'' in the utility name or the
471utility arguments if it had preceding or following non-whitespace characters.
472This version replaces it no matter where in the utility name or arguments
473it appears.
474.Sh BUGS
475The special characters used by
476.Nm find
477are also special characters to many shell programs.
478In particular, the characters ``*'', ``['', ``]'', ``?'', ``('', ``)'',
479``!'', ``\e'' and ``;'' may have to be escaped from the shell.
480.Pp
481As there is no delimiter separating options and file names or file
482names and the
483.Ar expression ,
484it is difficult to specify files named ``-xdev'' or ``!''.
485These problems are handled by the
486.Fl f
487option and the
488.Xr getopt 3
489``--'' construct.
490.Pp
491The
492.Ic -delete
493primary do not interact well with other options that cause the filesystem
494tree traversal options to be changed.
495.Sh HISTORY
496A
497.Nm
498command appeared in
499.At v1 .
500