BSD Newsletter.com
   Front | Info | Lists | Newsfeeds | Study Guide | What is BSD?
Advertisement: The OpenBSD PF Packet Filter Book: PF for NetBSD, FreeBSD, DragonFly and OpenBSD

BSD Links
·New Links
·Advocacy
·Drivers
·Events
·Flavours
·FAQs
·Guides
·Programming
·Security
·Software
·User Groups

This is the BSDA Study Guide Book written via a wiki collaboration. This is a work in progress. You may contribute to or discuss this specific page at http://bsdwiki.reedmedia.net/wiki/Find_a_file_with_a_given_set_of_attributes.html.

Find a file with a given set of attributes

Concept

The find(1) 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 a 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)



Front | Information | Lists | Newsfeeds