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.

The following section discusses the find command's predicates.

Unix find Command's Predicates

Matching the find command predicates to descriptions of what they would find. Here are the correct answers:
  1. -type d: All directories
  2. -perm 755: Permission = rwxr-xr-x
  3. -atime +7: No match (This would find all files last accessed more than 7 days ago.)
  4. -mtime +7: Files last modified more than a week ago
  5. -perm -002: “Other” write permission is set
  6. -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:
  1. Process ID and parent process ID
  2. Real user ID and real group ID
  3. Supplementary group IDs
  4. Process group ID
  5. Session ID
  6. Controlling terminal
  7. Current working directory
  8. Root directory
  9. File mode creation mask
  10. File locks
  11. Process signal mask
  12. Pending signals
  13. Resource limits
  14. Values for tms_utime, tms_stime, tms_cutime, and tms_cstime

Purpose of the Predicates in the UNIX find Command

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:
  1. File Type Predicates
    • Used to filter by file type.
    • Example:
      find /path/to/search -type f    # Finds regular files
      find /path/to/search -type d    # Finds directories
      find /path/to/search -type l    # Finds symbolic links
              
  2. Name-Based Predicates
    • Used to match files and directories by name.
    • Example:
      find /path/to/search -name "*.txt"    # Finds all .txt files
      find /path/to/search -iname "file*"   # Case-insensitive search
              
  3. Size-Based Predicates
    • Used to filter by file size.
    • Example:
      find /path/to/search -size +100M    # Finds files larger than 100MB
      find /path/to/search -size -1k     # Finds files smaller than 1KB
              
  4. Time-Based Predicates
    • Used to filter by modification, access, or change time.
    • Example:
      find /path/to/search -mtime -7    # Finds files modified in the last 7 days
      find /path/to/search -atime +30   # Finds files accessed more than 30 days ago
      find /path/to/search -ctime 1     # Finds files changed exactly 1 day ago
              
  5. Permission and Ownership Predicates
    • Used to filter by file permissions and ownership.
    • Example:
      find /path/to/search -perm 644      # Finds files with 644 permissions
      find /path/to/search -user root      # Finds files owned by 'root'
      find /path/to/search -group users    # Finds files belonging to 'users' group
              
  6. Logical Operators with Predicates
    • Used to combine multiple predicates.
    • Example:
      find /path/to/search -type f -size +50M -o -name "*.log"
              
      - -o (OR) finds files larger than 50MB or those with a .log extension.
      find /path/to/search -type f -name "*.log" -a -size +1M
              
      - -a (AND) finds .log files that are also larger than 1MB.
  7. Action-Based Predicates
    • Used to perform actions on matching files.
    • Example:
      find /path/to/search -name "*.log" -delete    # Deletes matching files
      find /path/to/search -type f -exec ls -lh {} \;    # Lists details of matching files
              
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.

SEMrush Software 7 SEMrush Banner 7