HxHippy

find

Search for files in a directory hierarchy

Overview

The find command searches the directory tree rooted at each given starting-point 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.

Syntax

find [path...] [expression]

Common Options

-name pattern

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

-iname pattern

Like -name, but the match is case insensitive.

-type c

File is of type c: f (regular file), d (directory), l (symbolic link), c (character special), b (block special), p (FIFO), s (socket).

-user uname

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

-group gname

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

-perm mode

File`s permission bits are exactly mode (octal or symbolic).

-mtime n

File`s data was last modified n*24 hours ago.

-atime n

File`s data was last accessed n*24 hours ago.

-ctime n

File`s status was last changed n*24 hours ago.

-size n[cwbkMG]

File uses n units of space (c for bytes, w for two-byte words, b for 512-byte blocks (default), k for Kilobytes, M for Megabytes, G for Gigabytes).

-empty

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

-delete

Delete files; true if removal succeeded. If the removal failed, an error message is issued.

-exec command {} \;

Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ‘;’ is encountered. The string ‘{}’ is replaced by the current file name being processed.

-maxdepth levels

Descend at most levels (a non-negative integer) levels of directories below the starting-points.

-mindepth levels

Do not apply any tests or actions at levels less than levels (a non-negative integer).

Examples

$ find . -name "*.txt"

Find all .txt files in the current directory and its subdirectories.

$ find /home -user john -type f

Find all files owned by user john in the /home directory.

$ find /var/log -name "*.log" -mtime +7

Find .log files in /var/log modified more than 7 days ago.

$ find . -type d -empty -delete

Find and delete all empty directories in the current path.

$ find . -name "*.tmp" -exec rm {} \;

Find all .tmp files and remove them.

$ find /usr/bin -type f -perm /a=x

Find all executable files in /usr/bin.

searchlocatefilesdirectorycriteriafind