find(C) 06 January 1993 find(C) Name find - find files Syntax find pathname-list expression Description The find command is used to find files matching a certain set of selec- tion criteria. find recursively descends the directory hierarchy for each pathname in the pathname-list (that is, one or more pathnames) seeking files that match a boolean expression written in the primaries given below. Expressions For each file encountered, find evaluates the specified expression, formed of one or more of the following primary expressions, which may evaluate as true or false. In the descriptions, the argument n is used as a decimal integer where +n means more than n, -n means less than n and n means exactly n. -name pattern True if pattern matches the current file name. pattern is similar to sh(C)'s filename matching syntax and there- fore care must be taken to escape or quote patterns con- taining the following characters: the left bracket ([), the question mark (?) and the star (*). -perm onum True if the file permission flags exactly match onum (see chmod(C)). If onum is prefixed by a minus sign, all other modes become significant (see mknod(S)), including the file type, setuid, setgid, and sticky bits rather than just read/write/execute modes for owner/group/other. -type x True if the type of the file is x, where x is b for block special file, c for character special file, d for direc- tory, p for named pipe (first-in-first-out (FIFO)), f for regular file, or l for symbolic link. -links n True if the file has n links. -size n [ c ] True if the file is n blocks long (1024 bytes per block), not including indirect blocks. If n is followed by a ``c'', the size is in characters. -follow Always true; causes symbolic links to be followed. When following symbolic links, find keeps track of the direc- tories visited so that it can detect infinite loops. For example, an infinite loop in a find would occur if a sym- bolic link pointed to an ancestor. This expression should not be used with the -type l expression. -mount Always true; restricts the search to the file system con- taining the directory specified, or if no directory was specified, the current directory. -local True if the file physically resides on the local system. -inum num True if the file's inode is num. This is useful for locating files with matching inodes. -user uname True if the file belongs to the user uname. If uname is numeric and does not appear as a login name in the /etc/passwd file, it is taken as a user ID. -group gname True if the file belongs to the group gname. If gname is numeric and does not appear in the /etc/group file as a group name, it is taken as a group ID. -atime n True if the file was last accessed n days ago. -mtime n True if the data in the file was last modified n days ago. -ctime n True if the file's status was last changed (that is, cre- ated or modified) n days ago. -exec cmd Executes shell command cmd. The end of cmd must be punc- tuated by an escaped semicolon. A command argument {} is replaced by the current path name. True if the executed cmd returns a zero value as exit status (most commands return a zero value on successful completion and a non- zero value if an error is encountered). -ok cmd Like -exec except that the generated command line is printed with a question mark first, and is executed only if the user responds by typing ``y''. -cpio device Writes the current file on device in cpio(F) format (5120-byte records). Always true. -depth Causes all entries in a directory to be acted upon before the directory itself. This can be useful when used with cpio(C) or the -cpio expression to transfer files located in directories without write permission. Always true. -print Causes the current path name to be printed. This option is used to create a list of all files matched by the pre- vious primaries. Always true. -newer file True if the current file has been modified more recently than the argument file. ( expression ) True if the parenthesized expression is true. Usually used with the -o operator (see below), parentheses are used for grouping. Parentheses are special to the shell and must be escaped. The primaries may be combined using the following operators (in order of decreasing precedence): ! The ``!'' operator specifies the negation of the next primary (that is, !-newer file is true if the current file is not newer than file). This is the equivalent of the logical ``not'' operator. -o Placing the -o operator between two primaries creates an expression that is true if either of the two primaries is true. It should be used with parentheses (that is, \( -perm 644 -o -perm 664 \) is true if the current file has permissions 644 or 664). This is equivalent to the logical ``inclusive or'' operator. Note that placing two primaries next to each other is the equivalent of the logical ``and'' operation. The precedence of this operation is less than that of the ``!'' operator but greater than that of the -o operator. Examples The following command searches for files named chapter1 in the current directory and all directories below it and sends the pathname of any such files it finds to the standard output: find . -name chapter1 -print The following removes all files named core or filenames ending in .out that have not been accessed in the last seven days. find / \( -name core -o -name "*.out" \) -atime +7 -exec rm {} \; The next example uses find with the -cpio expression to make a tape archive of all files modified within the last seven days. Note the use of the -depth option to prevent any problems with read-only directories: find / -depth -mtime -7 -print -cpio /dev/rct0 find is used here to list all files within a given range of sizes (between 50 and 100 kilobytes) by including the -size expression twice: find / -size +50 -size -100 -exec ls -s {} \; For comparison, the ls(C) command is called with the -s option to report the size of each file in 512-byte blocks (including indirect blocks). Files /etc/passwd User names and uids /etc/group Group names and gids See also cpio(C), cpio(F), sh(C), stat(S) and test(C). Standards conformance find is conformant with: AT&T SVID Issue 2 ; and X/Open Portability Guide, Issue 3, 1989.