Special File Types  «Prev  Next»

Unix find command's Predicates

Question: What is the purpose and function of the predicates for the Unix find command
The find command in Unix is a powerful tool designed for searching and locating the list of files and directories based on conditions you specify for files that match the arguments. The conditions or filters used in the find command are often referred to as "predicates."
Predicates in the find command are used to specify the type of file we're looking for, its name, or even its size, among other properties. Predicates help to narrow down a search to the exact file or directory required.
The purpose and function of the predicates in the find command are as follows:
  1. File Name Predicates (-name, -iname): This predicate is used to search for files with a specific name. The -name predicate is case-sensitive, whereas -iname is case-insensitive.
    Example:
    find /home/user/ -name myfile.txt
    
  2. File Type Predicates (-type): This predicate is used to identify the type of file. For instance, f for regular files, d for directories, l for symbolic links, etc.
    Example:
    find /home/user/ -type f
    
  3. File Size Predicates (-size): This predicate is used to find files of a certain size. You can search for files larger or smaller than a certain size, or exactly a certain size.
    Example:
    find /home/user/ -size +10M
    
  4. Time-related Predicates (-atime, -mtime, -ctime): These predicates are used to search for files based on the time of their last access (-atime), modification (-mtime), or status change (-ctime).
    Example:
    find /home/user/ -atime -2
    
  5. Permission-related Predicates (-perm): This predicate is used to find files with specific permissions.
    Example: find /home/user/ -perm 644
  6. Ownership Predicates (-user, -group): These predicates are used to find files owned by a specific user or group.
    Example: find /home/user/ -user username
  7. Execution Predicates (-exec, -ok): These predicates are used to execute a specific command on each file found.
    Example:
    find /home/user/ -type f -exec rm {} \;
    

Each predicate narrows down the search parameters, allowing users to find the exact file or directory they're looking for. By combining predicates, users can create complex queries to locate files based on a variety of attributes.
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