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.
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. |
The following section discusses the
find
command's predicates.
Matching the
find
command predicates to descriptions of what they would find. Here are the correct answers:
-type d
: All directories
-perm 755
: Permission = rwxr-xr-x
-atime +7
: No match (This would find all files last accessed more than 7 days ago.)
-mtime +7
: Files last modified more than a week ago
-perm -002
: “Other” write permission is set
-type f
: No match (This would find all “regular” files.)
To look for all the occurrences of getrlimit in the man pages on our system, we could use
find /usr/share/man -type f -print | xargs grep getrlimit
If the man pages on our system are compressed, however, we could try
find /usr/share/man -type f -print | xargs bzgrep getrlimit
-f option
We use the type
-f option
to the find command to restrict the list so that it contains only regular files, because the grep commands cannot search for patterns in directories, and we want to avoid unnecessary error messages.
It has been mentioned that the process ID does not change after an exec, but the new program inherits additional properties from the calling process:
- Process ID and parent process ID
- Real user ID and real group ID
- Supplementary group IDs
- Process group ID
- Session ID
- Controlling terminal
- Current working directory
- Root directory
- File mode creation mask
- File locks
- Process signal mask
- Pending signals
- Resource limits
- Values for tms_utime, tms_stime, tms_cutime, and tms_cstime
In the UNIX `find` command, predicates are used to filter and control the search criteria for files and directories within a file system. These predicates allow users to specify conditions that must be met for a file or directory to be included in the search results.
Types of Predicates in `find`
Predicates in `find` are typically categorized into the following types:
-
File Type Predicates
-
Name-Based Predicates
-
Size-Based Predicates
-
Time-Based Predicates
-
Permission and Ownership Predicates
-
Logical Operators with Predicates
-
Action-Based Predicates
Conclusion: Predicates in `find` serve as filtering and action mechanisms that allow users to precisely locate and manipulate files based on various conditions, making `find` a powerful tool for system administration and scripting in UNIX/Linux environments.