[Next] [Previous] [Up] [Top] [Contents]

8.1 Working With Files

8.1.14 find - find files

The find command will recursively search the indicated directory tree to find files matching a type or pattern you specify. find can then list the files or execute arbitrary commands based on the results.

Syntax

find directory [search options] [actions]

Common Options

For the time search options the notation in days, n is:

+n more than n days

n exactly n days

-n less than n days

Some file characteristics that find can search for are:

time that the file was last accessed or changed

-atime n access time, true if accessed n days ago

-ctime n change time, true if the files status was changed n days ago

-mtime n modified time, true if the files data was modified n days ago

-newer filename true if newer than filename

-type type type of file, where type can be:

b block special file

c character special file

d directory

l symbolic link

p named pipe (fifo)

f regular file

-fstype type type of file system, where type can be any valid file system type, e.g.: ufs (Unix File System) and nfs (Network File System)

-user username true if the file belongs to the user username

-group groupname true if the file belongs to the group groupname

-perm [-]mode permissions on the file, where mode is the octal modes for the chmod command. When mode is precede by the minus sign only the bits that are set are compared.

-exec command execute command. The end of command is indicated by and escaped semicolon (\;). The command argument, {}, replaces the current path name.

-name filename true if the file is named filename. Wildcard pattern matches are allowed if the meta-character is escaped from the shell with a backslash (\).

-ls always true. It prints a long listing of the current pathname.

-print print the pathnames found (default for SVR4, not for BSD)

Complex expressions are allowed. Expressions should be grouped within parenthesis (escaping the parenthesis with a backslash to prevent the shell from interpreting them). The exclamation symbol (!) can be used to negate an expression. The operators: -a (and) and -o (or) are used to group expressions.

Examples

find will recursively search through sub-directories, but for the purpose of these examples we will just use the following files:

14 -rw-r--r-- 1 frank staff 6682 Feb 5 10:04 library

6 -r--r----- 1 frank staff 3034 Mar 16 1995 netfile

34 -rw-r--r-- 1 frank staff 17351 Feb 5 10:04 standard

2 -rwxr-xr-x 1 frank staff 386 Apr 26 09:51 tr25*

To find all files newer than the file, library:

% find . -newer library -print

./tr25

./standard

To find all files with general read or execute permission set, and then to change the permissions on those files to disallow this:

% find . \( -perm -004 -o -perm -001 \) -exec chmod o-rx {} \; -exec ls -al {} \;

-rw-r----- 1 frank staff 6682 Feb 5 10:04 ./library

-rwxr-x--- 1 frank staff 386 Apr 26 09:51 ./tr25

-rw-r----- 1 frank staff 17351 Feb 5 10:04 ./standard

In this example the parentheses and semicolons are escaped with a backslash to prevent the shell from interpreting them. The curly brackets are automatically replaced by the results from the previous search and the semicolon ends the command.

We could search for any file name containing the string "ar" with:

% find . -name \*ar\* -ls

326584 7 -rw-r----- 1 frank staff 6682 Feb 5 10:04 ./library

326585 17 -rw-r----- 1 frank staff 17351 Feb 5 10:04 ./standard

where the -ls option prints out a long listing, including the inode numbers.


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]