## Find a file with a given set of attributes ##

[Author](authors.html): Fred Crowson openbsd@crowsons.net OpenBSD

[Reviewer](technical_reviewers.html): _name_ _[[contact]]_ _[[BSD_flavour]]_

[Reviewer](technical_reviewers.html): _name_ _[[contact]]_ _[[BSD_flavour]]_

----



### Concept ###

The find(1)(((find))) utility is invaluable when searching for
files matching a specific set of attributes.
Be comfortable in using this utility and may be asked to locate files
according to last modification time, size, type, file flags, UID or
GID, permissions or by a text pattern.


### Introduction ###

The find utility is one that you cannot live without as a BSD system's administrator.  Although find is a powerful tool it can be complex to master.  The find utility descends through the file hierarchy looking for matches to the criteria it was given.  When learning find it is useful to understand that conceptually the command is:

""$ find _start-dir(s)_  _criteria-for-matching-and-actions_

Where _start-dir(s)_ is the set of directories where find should start.  The _criteria-for-matching-and-actions_
tell find what to look for, and how to process what it finds, these are described as the primaries in the man pages.  Many of the search criteria expect to find an exact match - this can lead to frustration when first using find, as it apparently does match files that you expect it to, due to the file not matching exactly.  However, with time periods, sizes and other numeric quantities can be prefixed with a **+** (meaning more than) or with a **-** (meaning less than), this is illustrated below in the examples.

The common primaries (criteria) for matching are:

**-name _filename_**
You can also use wildcards and regular expressions with the **-name** option, as long as you quote them.  If wildcards are not quoted then you will end up with an unknown option error after the first matching file is found.

**-perm _mode_**
find files with an octal access mode of _mode_

**-type _c_** 
find files by type.  Where _c_ is **b** for block device, **c** for character special, **f** for regular files, **l** for symbolic links, **p** for FIFO, **d** for directories, and **s** for socket.

**-size _n[c]_**
find files whose size rounded up, in 512-byte blocks is _n_.  If the _n_ is followed by a _c_ then it is true if the files size is _n_ bytes.

**-ls** 
this always evaluates to true, and the following information for the current file is output: inode, size 512-byte blocks, file permissions, number of hardlinks, owner, group, size in bytes, las modification time and pathname.

**-print** 
don't forget print - this prints the pathname of the current file to standard output.

### Examples ###

find [-f pathname] [pathname ...] expression (expression consists of primaries and operands)

last modification time (less than 2 days):

**find / -mtime -2**

note: -mtime _n_[smhdw] works with FreeBSD and DragonFlyBSD, hence the above example for finding files less than 2 hours old rather than two days old would be **find / -mtime -2h** but it is **-mtime _n_** for NetBSD and OpenBSD.

by size:

**find / -size 2048c** (finds files that are 2KB in size)

note: FreeBSD find allows a scale factor to the _n_ -size _n_[ckMGTP]

by type:

**find / -type t** (where t is b:block special, c:character special, d:directory, f:regular file, l:symbolic link, p: FIFO, s:socket)

file flags:

**find / -flags [-|+]flags, notflags** 

UID:

**find / -perm [-|+]4000**

GID:

**find / -perm [-|+]2000**

Permissions:

**find / -perm [-|+]0777** (or similar mode)

**find / -perm -644**

By text pattern:

**find / -regex pattern**

find / -regex .*/[xyz]* finds ./foo/xyzzy

### Practice Exercises ###


### More information ###

find(1)


