DistributedNetworks DistributedNetworks


Special File Types  «Prev  Next»
Lesson 7 Finding files
Objective Match the find Command's Predicates with Descriptions of their Purpose.

Match the find Command's Predicates with Descriptions of their Purpose

The find command descends a directory tree from an initial point and locates all files matching a set of criteria. For example, the command
find $HOME –print

traverses the directory tree starting at $HOME and prints every file it encounters. The command
find / -user jeremy –print

traverses the entire directory tree, starting from root, printing files owned by jeremy.
Predicates may be combined with OR, AND, or NOT:
find / \( -user jeremy –o –user betty\) –print

lists all files, starting from the root, owned by jeremy OR betty. The tricky part is the parentheses.
These need to be escaped with a backslash so that the shell interprets them correctly. One more example:
find / \! –newer /etc/passwd –print

This command lists all files that are not newer than /etc/passwd, starting from the root. The ! needs to be escaped from the shell.
Multiple conditions are joined with AND by default. The options –atime +7 –mtime +14 would look for files last accessed more than 7 days ago and last modified more than 14 days ago.

Command predicates

The use of find is an art in itself, and many other tricks and options are available.
The following table summarizes a small, useful set of predicates:

Predicate Meaning
-o, -a, ! OR, AND, NOT respectively
-atime +/-n
-ctime +/-n
-mtime +/-n
Select files accessed (atime), modified (mtime), or changed (ctime) more (+) or less (-) than n days ago. Changed means modified or had ownership or permission changes.
-newer file Select files newer than the specified file.
-type b/c/d/p/l/f Select files of type block (b), character (c), directory (d), pipe (p), link (l), or regular (f).
-user name
-group name
Select files with specified user or group.
-name pattern Select files with name matching pattern. Pattern should be enclosed in single quotes to protect it from the shell.
-perm mode
- perm -mode
Select files of the given permission mode. To check if a particular bit is set, use –mode, where mode is the numeric value for the desired bit. For example, -004 checks if the "other read" permission bit is set.
-print Needed to generate a list of files; otherwise (on some systems), find will find the files, but not print them.

Find Command Predicates

Click the Exercise link below to practice your knowledge of the find command's predicates.
Find Command Predicates