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:

1
2
3
4
5
6
mkdir find_dir
touch find_dir/file{1,2,3}
ln -s file1 find_dir/file1_link
dd if=/dev/zero of=find_dir/file_5MB bs=5000000 count=1
cd find_dir
ll
img

Prepare some files and change to the directory

Examples

Files Visited in 30 Minutes

1
find . -amin -30
img

Find files and directories 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:

1
find . -readable -a -writable
img

Find readable and writable files and directories

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:

1
find . -perm g=w
img

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:

1
find . -size 5M
img

Find files whose size are equal to 5MB

Execute the Command on the Found Files

With the -exec; option, we can use the command on the found file:

1
find . -readable -a -writable -exec ls {} \;
img

Find readable and writable files and directories and list them using ls command

List the Found Files With -dils Options

With the -ls action, we can list the found files in ls -dils format:

1
find . -readable -a -writable -ls;
img

Find readable and writable files and directories

Through -type -l tests followed by -delete action, we can remove all the links:

1
2
3
ll
find . -type l -delete
ll
img

Remove all the links

References FIND(1) EXPRESSION, FIND(1) EXAMPLES

Buy me a coffeeBuy me a coffee