1FIND(1P) POSIX Programmer's Manual FIND(1P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 find — find files
13
15 find [-H|-L] path... [operand_expression...]
16
18 The find utility shall recursively descend the directory hierarchy from
19 each file specified by path, evaluating a Boolean expression composed
20 of the primaries described in the OPERANDS section for each file
21 encountered. Each path operand shall be evaluated unaltered as it was
22 provided, including all trailing <slash> characters; all pathnames for
23 other files encountered in the hierarchy shall consist of the concate‐
24 nation of the current path operand, a <slash> if the current path oper‐
25 and did not end in one, and the filename relative to the path operand.
26 The relative portion shall contain no dot or dot-dot components, no
27 trailing <slash> characters, and only single <slash> characters between
28 pathname components.
29
30 The find utility shall be able to descend to arbitrary depths in a file
31 hierarchy and shall not fail due to path length limitations (unless a
32 path operand specified by the application exceeds {PATH_MAX} require‐
33 ments).
34
35 The find utility shall detect infinite loops; that is, entering a pre‐
36 viously visited directory that is an ancestor of the last file encoun‐
37 tered. When it detects an infinite loop, find shall write a diagnostic
38 message to standard error and shall either recover its position in the
39 hierarchy or terminate.
40
41 If a file is removed from or added to the directory hierarchy being
42 searched it is unspecified whether or not find includes that file in
43 its search.
44
46 The find utility shall conform to the Base Definitions volume of
47 POSIX.1‐2017, Section 12.2, Utility Syntax Guidelines.
48
49 The following options shall be supported by the implementation:
50
51 -H Cause the file information and file type evaluated for each
52 symbolic link encountered as a path operand on the command
53 line to be those of the file referenced by the link, and not
54 the link itself. If the referenced file does not exist, the
55 file information and type shall be for the link itself. File
56 information and type for symbolic links encountered during
57 the traversal of a file hierarchy shall be that of the link
58 itself.
59
60 -L Cause the file information and file type evaluated for each
61 symbolic link encountered as a path operand on the command
62 line or encountered during the traversal of a file hierarchy
63 to be those of the file referenced by the link, and not the
64 link itself. If the referenced file does not exist, the file
65 information and type shall be for the link itself.
66
67 Specifying more than one of the mutually-exclusive options -H and -L
68 shall not be considered an error. The last option specified shall
69 determine the behavior of the utility. If neither the -H nor the -L
70 option is specified, then the file information and type for symbolic
71 links encountered as a path operand on the command line or encountered
72 during the traversal of a file hierarchy shall be that of the link
73 itself.
74
76 The following operands shall be supported:
77
78 The first operand and subsequent operands up to but not including the
79 first operand that starts with a '-', or is a '!' or a '(', shall be
80 interpreted as path operands. If the first operand starts with a '-',
81 or is a '!' or a '(', the behavior is unspecified. Each path operand
82 is a pathname of a starting point in the file hierarchy.
83
84 The first operand that starts with a '-', or is a '!' or a '(', and
85 all subsequent arguments shall be interpreted as an expression made up
86 of the following primaries and operators. In the descriptions, wherever
87 n is used as a primary argument, it shall be interpreted as a decimal
88 integer optionally preceded by a <plus-sign> ('+') or <hyphen-minus>
89 ('-'), as follows:
90
91 +n More than n.
92
93 n Exactly n.
94
95 -n Less than n.
96
97 The following primaries shall be supported:
98
99 -name pattern
100 The primary shall evaluate as true if the basename of the
101 current pathname matches pattern using the pattern matching
102 notation described in Section 2.13, Pattern Matching Nota‐
103 tion. The additional rules in Section 2.13.3, Patterns Used
104 for Filename Expansion do not apply as this is a matching
105 operation, not an expansion.
106
107 -path pattern
108 The primary shall evaluate as true if the current pathname
109 matches pattern using the pattern matching notation described
110 in Section 2.13, Pattern Matching Notation. The additional
111 rules in Section 2.13.3, Patterns Used for Filename Expansion
112 do not apply as this is a matching operation, not an expan‐
113 sion.
114
115 -nouser The primary shall evaluate as true if the file belongs to a
116 user ID for which the getpwuid() function defined in the Sys‐
117 tem Interfaces volume of POSIX.1‐2017 (or equivalent) returns
118 NULL.
119
120 -nogroup The primary shall evaluate as true if the file belongs to a
121 group ID for which the getgrgid() function defined in the
122 System Interfaces volume of POSIX.1‐2017 (or equivalent)
123 returns NULL.
124
125 -xdev The primary shall always evaluate as true; it shall cause
126 find not to continue descending past directories that have a
127 different device ID (st_dev, see the stat() function defined
128 in the System Interfaces volume of POSIX.1‐2017). If any
129 -xdev primary is specified, it shall apply to the entire
130 expression even if the -xdev primary would not normally be
131 evaluated.
132
133 -prune The primary shall always evaluate as true; it shall cause
134 find not to descend the current pathname if it is a direc‐
135 tory. If the -depth primary is specified, the -prune primary
136 shall have no effect.
137
138 -perm [-]mode
139 The mode argument is used to represent file mode bits. It
140 shall be identical in format to the symbolic_mode operand
141 described in chmod, and shall be interpreted as follows. To
142 start, a template shall be assumed with all file mode bits
143 cleared. An op symbol of '+' shall set the appropriate mode
144 bits in the template; '-' shall clear the appropriate bits;
145 '=' shall set the appropriate mode bits, without regard to
146 the contents of the file mode creation mask of the process.
147 The op symbol of '-' cannot be the first character of mode;
148 this avoids ambiguity with the optional leading <hyphen-
149 minus>. Since the initial mode is all bits off, there are
150 not any symbolic modes that need to use '-' as the first
151 character.
152
153 If the <hyphen-minus> is omitted, the primary shall evaluate
154 as true when the file permission bits exactly match the value
155 of the resulting template.
156
157 Otherwise, if mode is prefixed by a <hyphen-minus>, the pri‐
158 mary shall evaluate as true if at least all the bits in the
159 resulting template are set in the file permission bits.
160
161 -perm [-]onum
162 If the <hyphen-minus> is omitted, the primary shall evaluate
163 as true when the file mode bits exactly match the value of
164 the octal number onum (see the description of the octal mode
165 in chmod). Otherwise, if onum is prefixed by a <hyphen-
166 minus>, the primary shall evaluate as true if at least all of
167 the bits specified in onum are set. In both cases, the behav‐
168 ior is unspecified when onum exceeds 07777.
169
170 -type c The primary shall evaluate as true if the type of the file is
171 c, where c is 'b', 'c', 'd', 'l', 'p', 'f', or 's' for block
172 special file, character special file, directory, symbolic
173 link, FIFO, regular file, or socket, respectively.
174
175 -links n The primary shall evaluate as true if the file has n links.
176
177 -user uname
178 The primary shall evaluate as true if the file belongs to the
179 user uname. If uname is a decimal integer and the getpwnam()
180 (or equivalent) function does not return a valid user name,
181 uname shall be interpreted as a user ID.
182
183 -group gname
184 The primary shall evaluate as true if the file belongs to the
185 group gname. If gname is a decimal integer and the getgr‐
186 nam() (or equivalent) function does not return a valid group
187 name, gname shall be interpreted as a group ID.
188
189 -size n[c]
190 The primary shall evaluate as true if the file size in bytes,
191 divided by 512 and rounded up to the next integer, is n. If
192 n is followed by the character 'c', the size shall be in
193 bytes.
194
195 -atime n The primary shall evaluate as true if the file access time
196 subtracted from the initialization time, divided by 86400
197 (with any remainder discarded), is n.
198
199 -ctime n The primary shall evaluate as true if the time of last change
200 of file status information subtracted from the initialization
201 time, divided by 86400 (with any remainder discarded), is n.
202
203 -mtime n The primary shall evaluate as true if the file modification
204 time subtracted from the initialization time, divided by
205 86400 (with any remainder discarded), is n.
206
207 -exec utility_name [argument ...] ;
208
209 -exec utility_name [argument ...] {} +
210 The end of the primary expression shall be punctuated by a
211 <semicolon> or by a <plus-sign>. Only a <plus-sign> that
212 immediately follows an argument containing only the two char‐
213 acters "{}" shall punctuate the end of the primary expres‐
214 sion. Other uses of the <plus-sign> shall not be treated as
215 special.
216
217 If the primary expression is punctuated by a <semicolon>, the
218 utility utility_name shall be invoked once for each pathname
219 and the primary shall evaluate as true if the utility returns
220 a zero value as exit status. A utility_name or argument con‐
221 taining only the two characters "{}" shall be replaced by the
222 current pathname. If a utility_name or argument string con‐
223 tains the two characters "{}", but not just the two charac‐
224 ters "{}", it is implementation-defined whether find replaces
225 those two characters or uses the string without change.
226
227 If the primary expression is punctuated by a <plus-sign>, the
228 primary shall always evaluate as true, and the pathnames for
229 which the primary is evaluated shall be aggregated into sets.
230 The utility utility_name shall be invoked once for each set
231 of aggregated pathnames. Each invocation shall begin after
232 the last pathname in the set is aggregated, and shall be com‐
233 pleted before the find utility exits and before the first
234 pathname in the next set (if any) is aggregated for this pri‐
235 mary, but it is otherwise unspecified whether the invocation
236 occurs before, during, or after the evaluations of other pri‐
237 maries. If any invocation returns a non-zero value as exit
238 status, the find utility shall return a non-zero exit status.
239 An argument containing only the two characters "{}" shall be
240 replaced by the set of aggregated pathnames, with each path‐
241 name passed as a separate argument to the invoked utility in
242 the same order that it was aggregated. The size of any set of
243 two or more pathnames shall be limited such that execution of
244 the utility does not cause the system's {ARG_MAX} limit to be
245 exceeded. If more than one argument containing the two char‐
246 acters "{}" is present, the behavior is unspecified.
247
248 The current directory for the invocation of utility_name
249 shall be the same as the current directory when the find
250 utility was started. If the utility_name names any of the
251 special built-in utilities (see Section 2.14, Special Built-
252 In Utilities), the results are undefined.
253
254 -ok utility_name [argument ...] ;
255 The -ok primary shall be equivalent to -exec, except that the
256 use of a <plus-sign> to punctuate the end of the primary
257 expression need not be supported, and find shall request
258 affirmation of the invocation of utility_name using the cur‐
259 rent file as an argument by writing to standard error as
260 described in the STDERR section. If the response on standard
261 input is affirmative, the utility shall be invoked. Other‐
262 wise, the command shall not be invoked and the value of the
263 -ok operand shall be false.
264
265 -print The primary shall always evaluate as true; it shall cause the
266 current pathname to be written to standard output.
267
268 -newer file
269 The primary shall evaluate as true if the modification time
270 of the current file is more recent than the modification time
271 of the file named by the pathname file.
272
273 -depth The primary shall always evaluate as true; it shall cause
274 descent of the directory hierarchy to be done so that all
275 entries in a directory are acted on before the directory
276 itself. If a -depth primary is not specified, all entries in
277 a directory shall be acted on after the directory itself. If
278 any -depth primary is specified, it shall apply to the entire
279 expression even if the -depth primary would not normally be
280 evaluated.
281
282 The primaries can be combined using the following operators (in order
283 of decreasing precedence):
284
285 ( expression )
286 True if expression is true.
287
288 ! expression
289 Negation of a primary; the unary NOT operator.
290
291 expression [-a] expression
292 Conjunction of primaries; the AND operator is implied by the
293 juxtaposition of two primaries or made explicit by the
294 optional -a operator. The second expression shall not be
295 evaluated if the first expression is false.
296
297 expression -o expression
298 Alternation of primaries; the OR operator. The second expres‐
299 sion shall not be evaluated if the first expression is true.
300
301 If no expression is present, -print shall be used as the expression.
302 Otherwise, if the given expression does not contain any of the pri‐
303 maries -exec, -ok, or -print, the given expression shall be effectively
304 replaced by:
305
306
307 ( given_expression ) -print
308
309 The -user, -group, and -newer primaries each shall evaluate their
310 respective arguments only once.
311
312 When the file type evaluated for the current file is a symbolic link,
313 the results of evaluating the -perm primary are implementation-defined.
314
316 If the -ok primary is used, the response shall be read from the stan‐
317 dard input. An entire line shall be read as the response. Otherwise,
318 the standard input shall not be used.
319
321 None.
322
324 The following environment variables shall affect the execution of find:
325
326 LANG Provide a default value for the internationalization vari‐
327 ables that are unset or null. (See the Base Definitions vol‐
328 ume of POSIX.1‐2017, Section 8.2, Internationalization Vari‐
329 ables for the precedence of internationalization variables
330 used to determine the values of locale categories.)
331
332 LC_ALL If set to a non-empty string value, override the values of
333 all the other internationalization variables.
334
335 LC_COLLATE
336 Determine the locale for the behavior of ranges, equivalence
337 classes, and multi-character collating elements used in the
338 pattern matching notation for the -n option and in the
339 extended regular expression defined for the yesexpr locale
340 keyword in the LC_MESSAGES category.
341
342 LC_CTYPE This variable determines the locale for the interpretation of
343 sequences of bytes of text data as characters (for example,
344 single-byte as opposed to multi-byte characters in argu‐
345 ments), the behavior of character classes within the pattern
346 matching notation used for the -n option, and the behavior of
347 character classes within regular expressions used in the
348 extended regular expression defined for the yesexpr locale
349 keyword in the LC_MESSAGES category.
350
351 LC_MESSAGES
352 Determine the locale used to process affirmative responses,
353 and the locale used to affect the format and contents of
354 diagnostic messages and prompts written to standard error.
355
356 NLSPATH Determine the location of message catalogs for the processing
357 of LC_MESSAGES.
358
359 PATH Determine the location of the utility_name for the -exec and
360 -ok primaries, as described in the Base Definitions volume of
361 POSIX.1‐2017, Chapter 8, Environment Variables.
362
364 Default.
365
367 The -print primary shall cause the current pathnames to be written to
368 standard output. The format shall be:
369
370
371 "%s\n", <path>
372
374 The -ok primary shall write a prompt to standard error containing at
375 least the utility_name to be invoked and the current pathname. In the
376 POSIX locale, the last non-<blank> in the prompt shall be '?'. The
377 exact format used is unspecified.
378
379 Otherwise, the standard error shall be used only for diagnostic mes‐
380 sages.
381
383 None.
384
386 None.
387
389 The following exit values shall be returned:
390
391 0 All path operands were traversed successfully.
392
393 >0 An error occurred.
394
396 Default.
397
398 The following sections are informative.
399
401 When used in operands, pattern matching notation, <semicolon>, <left-
402 parenthesis>, and <right-parenthesis> characters are special to the
403 shell and must be quoted (see Section 2.2, Quoting).
404
405 The bit that is traditionally used for sticky (historically 01000) is
406 specified in the -perm primary using the octal number argument form.
407 Since this bit is not defined by this volume of POSIX.1‐2017, applica‐
408 tions must not assume that it actually refers to the traditional sticky
409 bit.
410
412 1. The following commands are equivalent:
413
414
415 find .
416 find . -print
417
418 They both write out the entire directory hierarchy from the current
419 directory.
420
421 2. The following command:
422
423
424 find / \( -name tmp -o -name '*.xx' \) -atime +7 -exec rm {} \;
425
426 removes all files named tmp or ending in .xx that have not been
427 accessed for seven or more 24-hour periods.
428
429 3. The following command:
430
431
432 find . -perm -o+w,+s
433
434 prints (-print is assumed) the names of all files in or below the
435 current directory, with all of the file permission bits S_ISUID,
436 S_ISGID, and S_IWOTH set.
437
438 4. The following command:
439
440
441 find . -name SCCS -prune -o -print
442
443 recursively prints pathnames of all files in the current directory
444 and below, but skips directories named SCCS and files in them.
445
446 5. The following command:
447
448
449 find . -print -name SCCS -prune
450
451 behaves as in the previous example, but prints the names of the
452 SCCS directories.
453
454 6. The following command is roughly equivalent to the -nt extension to
455 test:
456
457
458 if [ -n "$(find file1 -prune -newer file2)" ]; then
459 printf %s\\n "file1 is newer than file2"
460 fi
461
462 7. The descriptions of -atime, -ctime, and -mtime use the terminology
463 n ``86400 second periods (days)''. For example, a file accessed at
464 23:59 is selected by:
465
466
467 find . -atime -1 -print
468
469 at 00:01 the next day (less than 24 hours later, not more than one
470 day ago); the midnight boundary between days has no effect on the
471 24-hour calculation.
472
473 8. The following command:
474
475
476 find . ! -name . -prune -name '*.old' -exec \
477 sh -c 'mv "$@" ../old/' sh {} +
478
479 performs the same task as:
480
481
482 mv ./*.old ./.old ./.*.old ../old/
483
484 while avoiding an ``Argument list too long'' error if there are a
485 large number of files ending with .old and without running mv if
486 there are no such files (and avoiding ``No such file or directory''
487 errors if ./.old does not exist or no files match ./*.old or
488 ./.*.old).
489
490 The alternative:
491
492
493 find . ! -name . -prune -name '*.old' -exec mv {} ../old/ \;
494
495 is less efficient if there are many files to move because it exe‐
496 cutes one mv command per file.
497
498 9. On systems configured to mount removable media on directories under
499 /media, the following command searches the file hierarchy for files
500 larger than 100000 KB without searching any mounted removable
501 media:
502
503
504 find / -path /media -prune -o -size +200000 -print
505
506 10. Except for the root directory, and "//" on implementations where
507 "//" does not refer to the root directory, no pattern given to
508 -name will match a <slash>, because trailing <slash> characters are
509 ignored when computing the basename of the file under evaluation.
510 Given two empty directories named foo and bar, the following com‐
511 mand:
512
513
514 find foo/// bar/// -name foo -o -name 'bar?*'
515
516 prints only the line "foo///".
517
519 The -a operator was retained as an optional operator for compatibility
520 with historical shell scripts, even though it is redundant with expres‐
521 sion concatenation.
522
523 The descriptions of the '-' modifier on the mode and onum arguments to
524 the -perm primary agree with historical practice on BSD and System V
525 implementations. System V and BSD documentation both describe it in
526 terms of checking additional bits; in fact, it uses the same bits, but
527 checks for having at least all of the matching bits set instead of hav‐
528 ing exactly the matching bits set.
529
530 The exact format of the interactive prompts is unspecified. Only the
531 general nature of the contents of prompts are specified because:
532
533 * Implementations may desire more descriptive prompts than those used
534 on historical implementations.
535
536 * Since the historical prompt strings do not terminate with <newline>
537 characters, there is no portable way for another program to inter‐
538 act with the prompts of this utility via pipes.
539
540 Therefore, an application using this prompting option relies on the
541 system to provide the most suitable dialog directly with the user,
542 based on the general guidelines specified.
543
544 The -name file operand was changed to use the shell pattern matching
545 notation so that find is consistent with other utilities using pattern
546 matching.
547
548 The -size operand refers to the size of a file, rather than the number
549 of blocks it may occupy in the file system. The intent is that the
550 st_size field defined in the System Interfaces volume of POSIX.1‐2017
551 should be used, not the st_blocks found in historical implementations.
552 There are at least two reasons for this:
553
554 1. In both System V and BSD, find only uses st_size in size calcula‐
555 tions for the operands specified by this volume of POSIX.1‐2017.
556 (BSD uses st_blocks only when processing the -ls primary.)
557
558 2. Users usually think of file size in terms of bytes, which is also
559 the unit used by the ls utility for the output from the -l option.
560 (In both System V and BSD, ls uses st_size for the -l option size
561 field and uses st_blocks for the ls -s calculations. This volume of
562 POSIX.1‐2017 does not specify ls -s.)
563
564 The descriptions of -atime, -ctime, and -mtime were changed from the
565 SVID description of n ``days'' to n being the result of the integer
566 division of the time difference in seconds by 86400. The description is
567 also different in terms of the exact timeframe for the n case (versus
568 the +n or -n), but it matches all known historical implementations. It
569 refers to one 86400 second period in the past, not any time from the
570 beginning of that period to the current time. For example, -atime 2 is
571 true if the file was accessed any time in the period from 72 hours to
572 48 hours ago.
573
574 Historical implementations do not modify "{}" when it appears as a sub‐
575 string of an -exec or -ok utility_name or argument string. There have
576 been numerous user requests for this extension, so this volume of
577 POSIX.1‐2017 allows the desired behavior. At least one recent implemen‐
578 tation does support this feature, but encountered several problems in
579 managing memory allocation and dealing with multiple occurrences of
580 "{}" in a string while it was being developed, so it is not yet
581 required behavior.
582
583 Assuming the presence of -print was added to correct a historical pit‐
584 fall that plagues novice users, it is entirely upwards-compatible from
585 the historical System V find utility. In its simplest form (find direc‐
586 tory), it could be confused with the historical BSD fast find. The BSD
587 developers agreed that adding -print as a default expression was the
588 correct decision and have added the fast find functionality within a
589 new utility called locate.
590
591 Historically, the -L option was implemented using the primary -follow.
592 The -H and -L options were added for two reasons. First, they offer a
593 finer granularity of control and consistency with other programs that
594 walk file hierarchies. Second, the -follow primary always evaluated to
595 true. As they were historically really global variables that took
596 effect before the traversal began, some valid expressions had unex‐
597 pected results. An example is the expression -print -o -follow.
598 Because -print always evaluates to true, the standard order of evalua‐
599 tion implies that -follow would never be evaluated. This was never the
600 case. Historical practice for the -follow primary, however, is not con‐
601 sistent. Some implementations always follow symbolic links on the com‐
602 mand line whether -follow is specified or not. Others follow symbolic
603 links on the command line only if -follow is specified. Both behaviors
604 are provided by the -H and -L options, but scripts using the current
605 -follow primary would be broken if the -follow option is specified to
606 work either way.
607
608 Since the -L option resolves all symbolic links and the -type l primary
609 is true for symbolic links that still exist after symbolic links have
610 been resolved, the command:
611
612
613 find -L . -type l
614
615 prints a list of symbolic links reachable from the current directory
616 that do not resolve to accessible files.
617
618 A feature of SVR4's find utility was the -exec primary's + terminator.
619 This allowed filenames containing special characters (especially <new‐
620 line> characters) to be grouped together without the problems that
621 occur if such filenames are piped to xargs. Other implementations have
622 added other ways to get around this problem, notably a -print0 primary
623 that wrote filenames with a null byte terminator. This was considered
624 here, but not adopted. Using a null terminator meant that any utility
625 that was going to process find's -print0 output had to add a new option
626 to parse the null terminators it would now be reading.
627
628 The "-exec...{}+" syntax adopted was a result of IEEE PASC Interpreta‐
629 tion 1003.2 #210. It should be noted that this is an incompatible
630 change to IEEE Std 1003.2‐1992. For example, the following command
631 printed all files with a '-' after their name if they are regular
632 files, and a '+' otherwise:
633
634
635 find / -type f -exec echo {} - ';' -o -exec echo {} + ';'
636
637 The change invalidates usage like this. Even though the previous stan‐
638 dard stated that this usage would work, in practice many did not sup‐
639 port it and the standard developers felt it better to now state that
640 this was not allowable.
641
643 None.
644
646 Section 2.2, Quoting, Section 2.13, Pattern Matching Notation, Section
647 2.14, Special Built-In Utilities, chmod, mv, pax, sh, test
648
649 The Base Definitions volume of POSIX.1‐2017, Chapter 8, Environment
650 Variables, Section 12.2, Utility Syntax Guidelines
651
652 The System Interfaces volume of POSIX.1‐2017, fstatat(), getgrgid(),
653 getpwuid()
654
656 Portions of this text are reprinted and reproduced in electronic form
657 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
658 table Operating System Interface (POSIX), The Open Group Base Specifi‐
659 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
660 Electrical and Electronics Engineers, Inc and The Open Group. In the
661 event of any discrepancy between this version and the original IEEE and
662 The Open Group Standard, the original IEEE and The Open Group Standard
663 is the referee document. The original Standard can be obtained online
664 at http://www.opengroup.org/unix/online.html .
665
666 Any typographical or formatting errors that appear in this page are
667 most likely to have been introduced during the conversion of the source
668 files to man page format. To report such errors, see https://www.ker‐
669 nel.org/doc/man-pages/reporting_bugs.html .
670
671
672
673IEEE/The Open Group 2017 FIND(1P)