52. Find Utilities - Advanced Files Searching
Contents
The find command provides some useful options for finding files or directories based on creation time, modification time, owner, group, permissions, type, size, etc.
Operators
Here are some operators for find command:
Option | Meaning |
---|---|
! or -not | Logical NOT |
-a or -and | Logical AND |
-o or -or | Logical OR |
Arguments
Here are numeric arguments for find command:
Option | Meaning |
---|---|
+n | Greater than n |
-n | Less than n |
n | Exactly n |
Tests
Here are some tests for find command:
Option | Meaning |
---|---|
-admin n | Files visited recently n minutes ago. |
-atime n | Files visited recently n days ago. |
-cmin n | Files status changed recently n minutes ago. |
-ctime n | Files status changed recently n days ago. |
-empty | Empty files or directories. |
-executable | Files that the current user can run and directories that the current user can search for. |
-gid n | Files that match the group ID n. |
-group gname | Files that match the group name gname |
-mmin n | Files modified recently n minutes ago. |
-mtime n | Files modified recently n days ago. |
-newer file | Files that have been updated more recently than file. |
-nogroup | Files that do not belong to any group. |
-nouser | Files that do not belong to any user. |
-perm mode | Files that exactly match the specified permissions. |
-perm -mode | Files that match all permissions. |
-perm /mode | Files that match any permissions. |
-readable | Files that the current user can read. |
-size n [cwbkMG] | Files that use n amount of space. |
-type c {b|c|d|p|f|l|s|D} | Files that match the specified type. |
-uid n | Files that match the User ID n. |
-user uname | Files that match the user name uname |
-writable | Files that the current user can write. |
Actions
Here are some actions for find command:
Option | Meaning |
---|---|
-delete | Delete matching files. |
-exec command ; | Execute command on found files. |
-ls | List found files in ls -dils format. |
Let’s prepare some files first:
|
|
Examples
Files Visited in 30 Minutes
|
|
Files That the Current User Can Read and Write
With the combination of -readable, -a and -writable options, we can find the files and directories that the current user can read and write:
|
|
Files That Are Writable to Its Group
With the -perm g=w option, we can find files that can be written by the group to which the file belongs:
|
|
Files That Are Equal to 5MB
With the -size option, we can find files greater than, less than or equal to the specified size:
|
|
Execute the Command on the Found Files
With the -exec; option, we can use the command on the found file:
|
|
List the Found Files With -dils Options
With the -ls action, we can list the found files in ls -dils format:
|
|
Delete All the Links
Through -type -l tests followed by -delete action, we can remove all the links:
|
|
References FIND(1) EXPRESSION, FIND(1) EXAMPLES
Author Dong Chen
LastMod Sun Mar 24 2019