[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

Commands Reference, Volume 2


find Command

Purpose

Finds files with a matching expression.

Syntax

find Path ... [ Expression ]

Description

The find command recursively searches the directory tree for each specified Path parameter, seeking files that match a Boolean expression written using the terms given in the following text. When the find command is recursively descending directory structures, it will not descend into directories that are symbolically linked into the current hierarchy. The output from the find command depends on the terms specified by the Expression parameter.

The find command does not support the 4.3 BSD fast find syntax.

Expression Terms

These Boolean expressions and variables describe the search boundaries of the find command as defined in the Path and Expression parameters.

Note: In the following definitions, the Number variable specifies a decimal integer that can be expressed as +Number (more than Number), -Number (less than Number), or Number (exactly Number).

\( Expression \) Evaluates to the value True if the expression in parentheses is true.
-cpio Device Writes the current file to the specified device in the cpio command format.
-depth Always evaluates to the value True. Causes the descent of the directory hierarchy to be done so that all entries in a directory are affected before the directory itself is affected. This can be useful when the find command is used with the cpio command to transfer files that are contained in directories without write permission.
-exec Command Evaluates to the value True if the specified command runs and returns a 0 value as exit status. The end of the specified command must be punctuated by a quoted or escaped semicolon. A command parameter {} (braces) is replaced by the current path name.
-fstype Type Evaluates to the value True if the file system to which the file belongs is of the specified type, where the Type variable has a value of jfs (journaled file system) or nfs (network file system).
-group Group Evaluates to the value True if the file belongs to the specified group. If the value of the Group variable is numeric and does not appear in the /etc/group file, it is interpreted as a group ID.
-nogroup Evaluates to the value True if the file belongs to a group not in the /etc/group database.
-inum Number Evaluates to the value True if file has an i-node matching the value of the Number variable.
-links Number Evaluates to the value True if the file has the specified number of links. See the ln command for a description of links.
-ls Always evaluates to the value True. Causes the current path name to be printed together with its associated statistics. These statistics include the following:
  • I-node number
  • Size in kilobytes (1024 bytes)
  • Protection mode
  • Number of hard links
  • User
  • Group
  • Size in bytes
  • Modification time

If the file is a special file, the size field contains the major and minor device numbers. If the file is a symbolic link, the path name of the linked-to file is printed preceded by the -> (dash, greater than) symbols. Formatting is similar to that of the ls -filds command, however formatting is done internally without executing the ls command, therefore differences in output with the ls command may exist, such as with the protection mode.

-name File Evaluates to the value True if the value of the File variable matches the file name. Normal shell filename generation characters (see, the sh command) may be used. The pattern should either be enclosed in quotes or the escape character used when the find command is used from the shell. A backslash ( \ ) is used as an escape character within the pattern. You can use wildcard (pattern-matching) characters, provided they are quoted. See "Pattern Matching with Wildcards and Metacharacters" in Files Overview in AIX 5L Version 5.1 System User's Guide: Operating System and Devices for more information on using wildcard characters.

 
In an expression such as [a-z], the dash means "through" according to the current collating sequence. A collating sequence may define equivalence classes for use in character ranges. See the National Language Support Overview for Programming in AIX 5L Version 5.1 General Programming Concepts: Writing and Debugging Programs for more information on collating sequences and equivalence classes.

-newer File Evaluates to the value True if the current file has been modified more recently than the file indicated by the File variable.
-ok Command The same as the -exec expression, except that the find command asks you whether it should start the specified command. An affirmative response starts the command. The end of the specified command must be punctuated by a semicolon enclosed in quotes or the \; (backslash-escape semicolon).
-perm [ - ] OctalNumber

Evaluates to the value True if the permission code of the file exactly matches the OctalNumber parameter (see the chmod command for an explanation of file permissions). If the optional - (dash) is present, this expression evaluates to true if at least these permissions are set. The OctalNumber parameter may be up to nine octal digits.

-perm [ - ] Mode

The mode argument is used to represent file mode bits. It will be identical in format to the <symbolicmode> operand described in chmod, and will be interpreted as follows:

Initially, a template will be assumed with all file mode bits cleared. Op symbols have the following function:

+
sets the appropriate mode bits in the template

-
clears the appropriate bits

=
sets the appropriate mode bits, without regard to the contents of the process' file mode creation mask

The op symbol - cannot be the first character of mode. This avoids ambiguity with the optional leading hyphen. Since the initial mode is all bits off, there are no symbolic modes that need to use - as the first character.

If the hyphen is omitted, the primary evaluates as True when the file permission bits exactly match the value of the resulting template. Otherwise, if mode is prefixed by a hyphen, the primary will evaluate as True if at least all bits in the resulting template are set in the file permission bits.

The Mode parameter is identical to the chmod command syntax. This expression evaluates to the value True if the file has exactly these permissions. If the optional - (dash) is present, this expression evaluates to the value True if at least these permissions are set.

-print Always evaluates to the value True. Displays the current path name. The find command assumes a -print expression, unless the -exec, - ls, or -ok expressions are present.
-prune Always evaluates to the value True. Stops the descent of the current path name if it is a directory. If the -depth flag is specified, the -prune flag is ignored.
-size Number Evaluates to the value True if the file is the specified Number of blocks long (512 bytes per block). The file size is rounded up to the nearest block for comparison.
-size Numberc Evaluates to the value True if the file is exactly the specified Number of bytes long. Adding c to the end of the Number variable indicates that the size of the file is measured in individual bytes not blocks.
-atime Number Evaluates to the value True if the file has been accessed in Number-1 to Number multiples of 24 hours. For example, -atime 2 is true if the file has been accessed within 24 to 48 hours.
-ctime Number Evaluates to the value True if the file i-node (status information) has been changed in the specified number of 24-hour periods.
-mtime Number Evaluates to the value True if the file has been modified in Number-1 to Number multiples of 24 hours.
-type Type Evaluates to the value True if the Type variable specifies one of the following values:

b
Block special file

c
Character special file

d
Directory

f
Plain file

l
Symbolic link

p
FIFO (a named pipe)

s
Socket
-user User Evaluates to the value True if the file belongs to the specified user. If the value of the User variable is numeric and does not appear as a login name in the /etc/passwd file, it is interpreted as a user ID.
-nouser Evaluates to the value True if the file belongs to a user not in the /etc/passwd database.
-xdev Always evaluates to the value True. Prevents the find command from traversing a file system different from the one specified by the Path parameter.

These expressions can be combined using the following operators in the order of decreasing precedence:

  1. ( Expression ) - A parenthetic group of expressions and operators (parentheses are special to the shell and require the backslash-escape sequence).
  2. ! Expression - The negation of an expression ('!' is the unary NOT operator).
  3. Expression [ -a ] Expression - Concatenation of expressions (the AND operation is implied by the juxtaposition of two primaries or may be explicitly stated as -a).
  4. Expression -o Expression - Alternation of primaries; -o is the OR operator. The second expression will not be evaluated if the first expression is true.
Note:
When using the find and cpio commands together, you must use the -follow option with the cpio command when using the -L option with the cpio command, and visa versa. Not using these two options together produces undesirable results. If no expression is present, -print as used in the default expression. For example, if the given expression does not contain any of the primaries -exec, -ok, or -print, the given expression will be replaced by (given_expression) -print. The -user, -group, and -newer primaries each evaluate their respective arguments only once. Using a command specified by -exec or -ok does not affect subsequent primaries on the same file.

Exit Status

This command returns the following exit values:

0 All Path parameters were traversed successfully.
>0 An error occurred.

Examples

  1. To list all files in the file system with a given base file name, enter:
    find / -name .profile -print
    This searches the entire file system and writes the complete path names of all files named .profile. The / (slash) tells the find command to search the root directory and all of its subdirectories. In order not to waste time, it is best to limit the search by specifying the directories where you think the files might be.
  2. To list files having a specific permission code in the current directory tree, enter:
    find . -perm 0600 -print
    This lists the names of the files that have only owner-read and owner-write permission. The . (dot) tells the find command to search the current directory and its subdirectories. See the chmod command for an explanation of permission codes.
  3. To search several directories for files with certain permission codes, enter:
    find manual clients proposals -perm -0600 -print
    This lists the names of the files that have owner-read and owner-write permission and possibly other permissions. The manual, clients, and proposals directories and their subdirectories are searched. In the previous example, -perm 0600 selects only files with permission codes that match 0600 exactly. In this example, -perm -0600 selects files with permission codes that allow the accesses indicated by 0600 and other accesses above the 0600 level. This also matches the permission codes 0622 and 2744.
  4. To list all files in the current directory that have been changed during the current 24-hour period, enter:
    find . -ctime 1 -print
  5. To search for regular files with multiple links, enter:
    find . -type f -links +1 -print
    This lists the names of the ordinary files (-type f) that have more than one link (-links +1).

    Note: Every directory has at least two links: the entry in its parent directory and its own . (dot) entry. The ln command explains multiple file links.
  6. To find all accessible files whose path name contains find, enter:
    find . -name '*find*' -print
  7. To remove all files named a.out or *.o that have not been accessed for a week and that are not mounted using nfs, enter:


    find / \( -name a.out -o -name '*.o' \) -atime +7 ! -fstype nfs -exec \ rm {} \;

    Note: The number used within the -atime expression is +7. This is the correct entry if you want the command to act on files not accessed for more than a week (seven 24-hour periods).
  8. To print the path names of all files in or below the current directory, except the directories named SCCS or files in the SCCS directories, enter:
    find . -name SCCS -prune -o -print
    To print the path names of all files in or below the current directory, including the names of SCCS directories, enter:

    find . -print -name SCCS -prune
    
  9. To search for all files that are exactly 414 bytes long, enter:

    find . -size 414c -print
    
  10. To find and remove every file in your home directory with the .c suffix, enter:

    find /u/arnold -name "*.c" -exec rm {} ;
    

    Every time the find command identifies a file with the .c suffix, the rm command deletes that file. The rm command is the only parameter specified for the -exec expression. The {} (braces) represent the current path name.

Files


/usr/bin/find Contains the find command.
/bin/find Symbolic link to the find command.
/etc/group Contains a list of all known groups.
/etc/passwd Contains a list of all known users.

Related Information

Backup Overview for System Management in AIX 5L Version 5.1 System Management Concepts: Operating System and Devices introduces archiving methods, including the use of the cpio command.

Directories Overview in AIX 5L Version 5.1 Files Reference describes the structure and characteristics of directories in the file system.

Files Overview in AIX 5L Version 5.1 System User's Guide: Operating System and Devices describes files, file types, how to name files, and how to use wildcard characters.

Input and Output Redirection Overview in AIX 5L Version 5.1 System User's Guide: Operating System and Devices describes how the operating system processes input and output.

Shells Overview in AIX 5L Version 5.1 System User's Guide: Operating System and Devices describes shells, the different types of shells, and how shells affect the way commands are interpreted.

File and Directory Access Modes in AIX 5L Version 5.1 System User's Guide: Operating System and Devices introduces file ownership and permissions to access files and directories.


[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]