find

search for files in a directory hierarchy

Syntax

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

Description

find searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.

Options

-P     Never follow symbolic links.

-L     Follow symbolic links.
       When the -L option is in effect, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself (unless the symbolic link is broken). Using -L causes the -lname and -ilname predicates always to return false.

-H     Do not follow symbolic links, except while processing the command line arguments.

-Olevel
       Enables query optimisation. The find program reorders tests to speed up execution while preserving the overall effect; that is, predicates with side effects are not reordered relative to each other. The optimisations performed at each optimisation level are as follows.

       0      Equivalent to optimisation level 1.

       1      This is the default optimisation level and corresponds to the traditional behaviour. Expressions are reordered so that tests based only on the names of files (for example -name and -regex) are performed first.

       2      Any -type or -xtype tests are performed after any tests based only on the names of files, but before any tests that require information from the inode. On many modern versions of Unix, file types are returned by readdir() and so these predicates are faster to evaluate than predicates which need to stat the file first.

       3      At this optimisation level, the full cost-based query optimiser is enabled. The order of tests is modified so that cheap (i.e. fast) tests are performed first and more expensive ones are performed later, if necessary. Within each cost band, predicates are evaluated earlier or later according to whether they are likely to succeed or not. For -o, predicates which are likely to succeed are evaluated earlier, and for -a, predicates which are likely to fail are evaluated earlier.

Expressions

-maxdepth levels
       Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments.

-mindepth levels
       Do not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files except the command line arguments.

-mount Don’t descend directories on other filesystems. An alternate name for -xdev, for compatibility with some other versions of find.

-noleaf
       Do not optimize by assuming that directories contain 2 fewer subdirectories than their hard link count.

-regextype type
       Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line.

-xautofs
       Don’t descend directories on autofs filesystems.

-xdev  Don’t descend directories on other filesystems.

Tests

Numeric arguments can be specified as

       +n     for greater than n,

       -n     for less than n,

       n      for exactly n.

-amin n
       File was last accessed n minutes ago.

-anewer file
       File was last accessed more recently than file was modified.

-atime n
       File was last accessed n*24 hours ago.

-cmin n
       File’s status was last changed n minutes ago.

-cnewer file
       File’s status was last changed more recently than file was modified.

-ctime n
       File’s status was last changed n*24 hours ago.

-empty File is empty and is either a regular file or a directory.

-executable
       Matches files which are executable and directories which are searchable (in a file name resolution sense).

-false Always false.

-fstype type
       File is on a filesystem of type type.

-gid n File’s numeric group ID is n.

-group gname
       File belongs to group gname (numeric group ID allowed).

-iname pattern
       Like -name, but the match is case insensitive.

-ipath pattern
       Behaves in the same way as -iwholename. This option is deprecated, so please do not use it.

-iregex pattern
       Like -regex, but the match is case insensitive.

-links n
       File has n links.

-lname pattern
       File is a symbolic link whose contents match shell pattern pattern.

-mmin n
       File’s data was last modified n minutes ago.

-mtime n
       File’s data was last modified n*24 hours ago.

-name pattern
       Base of file name (the path with the leading directories removed) matches shell pattern pattern.

-newer file
       File was modified more recently than file.

-nogroup
       No group corresponds to file’s numeric group ID.

-nouser
       No user corresponds to file’s numeric user ID.

-path pattern
       File name matches shell pattern pattern.

-perm mode
       File’s permission bits are exactly mode (octal or symbolic).

-perm -mode
       All of the permission bits mode are set for the file.

-perm /mode
       Any of the permission bits mode are set for the file.

-perm +mode
       Deprecated, old way of searching for files with any of the permission bits in mode set.

-readable
       Matches files which are readable.

-regex pattern
       File name matches regular expression pattern.

-size n[cwbkMG]
       File uses n units of space. The following suffixes can be used:

              ‘b’    for 512-byte blocks (this is the default if no suffix is used)

              ‘c’    for bytes

              ‘w’    for two-byte words

              ‘k’    for Kilobytes (units of 1024 bytes)

              ‘M’    for Megabytes (units of 1048576 bytes)

              ‘G’    for Gigabytes (units of 1073741824 bytes)

-type c
       File is of type c:

              b      block (buffered) special

              c      character (unbuffered) special

              d      directory

              p      named pipe (FIFO)

              f      regular file

              l      symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is  broken. If you want to search for symbolic links when -L is in effect, use -xtype.

              s      socket

              D      door (Solaris)

-uid n File’s numeric user ID is n.

-used n
       File was last accessed n days after its status was last changed.

-user uname
       File is owned by user uname (numeric user ID allowed).

-writable
       Matches files which are writable.

Actions

-delete
       Delete files; true if removal succeeded.

-exec command ;
       Execute command; true if 0 status is returned.

-exec command {} +
       This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files.

-fprint file
       True; print the full file name into file file.

-fprintf file format
       True; like -printf but write to file like -fprint.

-ok command ;
       Like  -exec but ask the user first.

-print True;  print  the  full  file  name  on the standard output, followed by a newline.

-quit  Exit immediately

Example 1

search for a file using find command.

[eleiss@eleiss ~]$ find ./ -name "*hello*"
./hello-1.1.c
./hello-1.c
[eleiss@eleiss ~]$

This command finds files matching the pattern given in the current directory recursively.

Example 2

Find empty files in a directory.

[eleiss@eleiss ~]$ ls -l
total 12
-rw-rw-r--. 1 eleiss eleiss  81 Jan  1  2011 dhristadhyum
-rw-rw-r--. 1 eleiss eleiss   0 Sep  4 18:53 empty_file
-rw-rw-r--. 1 eleiss eleiss 125 Sep  4 16:02 hello-1.1.c
-rw-rw-r--. 1 eleiss eleiss  63 Sep  4 16:02 hello-1.c
[eleiss@eleiss ~]$ find ./ -empty
./empty_file
[eleiss@eleiss ~]$

The above command will search for an empty directory or regular file in the provided path recursively.

Example 3

Find all the files that were modified in last 24 hours.

[eleiss@eleiss ~]$ ls -l
total 12
-rw-rw-r--. 1 eleiss eleiss  81 Jan  1  2011 dhristadhyum
-rw-rw-r--. 1 eleiss eleiss   0 Sep  4 18:53 empty_file
-rw-rw-r--. 1 eleiss eleiss 125 Sep  4 16:02 hello-1.1.c
-rw-rw-r--. 1 eleiss eleiss  63 Sep  4 16:02 hello-1.c
[eleiss@eleiss ~]$ find ./ -mtime -1
./
./hello-1.1.c
./hello-1.c
./empty_file
[eleiss@eleiss ~]$

The command finds all the files and directories that were modified in last 24 hours.

Example 4

Find all the files which have read and write permissions for others in a directory.

[eleiss@eleiss ~]$ ls -l
total 16
-rw-rw-r--. 1 eleiss eleiss  81 Jan  1  2011 dhristadhyum
-rw-rw-r--. 1 eleiss eleiss   0 Sep  4 18:53 empty_file
-rw-rw-r--. 1 eleiss eleiss 125 Sep  4 16:02 hello-1.1.c
-rw-rw-r--. 1 eleiss eleiss  63 Sep  4 16:02 hello-1.c
-rw-r--rw-. 1 eleiss eleiss   6 Sep  4 19:01 world_writable
[eleiss@eleiss ~]$ find ./ -perm -o+rw
./world_writable
[eleiss@eleiss ~]$

The output says that "world_writable" has both read and write permissions for others.

Example 5

Remove all the files which have read and write permissions for others in a directory.

[eleiss@eleiss ~]$ ls -l
total 16
-rw-rw-r--. 1 eleiss eleiss  81 Jan  1  2011 dhristadhyum
-rw-rw-r--. 1 eleiss eleiss   0 Sep  4 18:53 empty_file
-rw-rw-r--. 1 eleiss eleiss 125 Sep  4 16:02 hello-1.1.c
-rw-rw-r--. 1 eleiss eleiss  63 Sep  4 16:02 hello-1.c
-rw-r--rw-. 1 eleiss eleiss   9 Sep  4 19:10 world_writable
[eleiss@eleiss ~]$ find ./ -type f -perm -o+rw -exec rm -fv {} \;
removed `./world_writable'
[eleiss@eleiss ~]$ ls -l
total 12
-rw-rw-r--. 1 eleiss eleiss  81 Jan  1  2011 dhristadhyum
-rw-rw-r--. 1 eleiss eleiss   0 Sep  4 18:53 empty_file
-rw-rw-r--. 1 eleiss eleiss 125 Sep  4 16:02 hello-1.1.c
-rw-rw-r--. 1 eleiss eleiss  63 Sep  4 16:02 hello-1.c
[eleiss@eleiss ~]$

First the find command will search for the files matching the given criteria and then operates the rm command on each of them.


You may write to resources@eleiss.com for any Linux related queries.

advertisement