The find command
The find command searches a directory tree for files that match the
specified pattern. The command syntax is:
find [path...] [expression]
You must specify at least one path, but you do not have to specify an
expression. If you do not specify an expression, find will print all files in
the specified directories.
Common options:
- -name
- Specifies file name to search for. Can use regular expressions.
- -type
- Type of file to search for:
- b
- block special
- c
- character special
- d
- directory
- p
- named pipe (FIFO)
- f
- regular file
- l
- symbolic link
- s
- socket
- -print
- Prints file name to standard output (default).
- -exec
- Executes a command, substituting {} with the file name.
- -xdev
- Do not seach mounted file systems.
- -o
- Or operator.
- -a
- And operator.
Examples:
- Search for all files named index.html, starting in the current
directory:
find . -name index.html
- Search for all files in the include directory that end in ".h":
find /usr/include -name "*.h"
- Search for and list all regular files and symbolic named core.
find / \( -type f -o -type l \) -name core -ls
- Find where INT_MAX is defined in /usr/include:
find /usr/include/ -name "*.h" -exec grep "#define
*INT_MAX" {} /dev/null \;
- Search for and delete all regular files and symbolic named core.
find / \( -type f -o -type l \) -name core -exec rm -f {} \;
Next topic: xargs