Cyberithub

40 Best Examples of Find Command in Linux

Table of Contents

Advertisements

In this article, I will take you through 40 best examples of find command in Linux. Find is a powerful tool that allow us to quickly and easily scan through our Filesystems to find files and directories based on certain criteria. We can even perform some action on the results we get back.

Sometimes you might end up in writing long code using any of the programming languages to find files and directories based on some criteria which you can easily do through a simple find command in Linux. So it is very important to fully understand this command in Linux.

40 Best Examples of Find Command in Linux 2

40 Best Examples of Find Command in Linux

Also Read: How to Update or Upgrade CentOS 7.X to 7.7 with Easy Steps

Example 1: Check Find command version

To check version of find command, you need to use find -version command as shown below.

root@localhost:~# find -version
find (GNU findutils) 4.5.11
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.

 

Example 2: Find all the files Only created in last 40 mins 

You need to use find /home/example -type f -cmin -40 command in Linux to search all the files created in last 40 mins under /home/example.

NOTE:

Please note that I am using either current path(.) or /home/example as a reference path to show the usage of find command in below examples. This path could be different for you. So do the changes accordingly before using the below examples in your System.
root@localhost:~# find /home/example -type f -cmin -40

 -cmin n: File's status was last changed n minutes ago.

-type f: regular file

 

Example 3: Find all the files Only modified in last 40 mins 

You can use find /home/example -type f -mmin -40 command to find all the files modified under /home/example location in last 40 mins.

root@localhost:~# find /home/example -type f -mmin -40

-mmin n: File's data was last modified n minutes ago.

 

Example 4: Find all the files Only accessed in last 40 mins

You need to use find /home/example -type f -amin -40 command to find all the files accessed from /home/example location in last 40 mins.

root@localhost:~# find /home/example -type f -amin -40

-amin n: File was last accessed n minutes ago.

 

Example 5: Find all the files and directories created in last 40 mins 

You can use find /home/example -cmin -40 command to search all the files and directories created in last 40 mins under /home/example location.

root@localhost:~# find /home/example -cmin -40

 

Example 6: Find all the files and directories modified in last 40 mins 

You can use find /home/example -mmin -40 command to search all the files and directories modified under /home/example in last 40 mins.

root@localhost:~# find /home/example -mmin -40

 

Example 7: Find all the files and directories accessed in last 40 mins 

You can use find /home/example -amin -40 command to find all the files and directories accessed under /home/example path in last 40 mins.

root@localhost:~# find /home/example -amin -40

 

Example 8: Find all the files only created in last 40 mins 

You can use find /home/example -type f -cmin -40 command to find all the files created in last 40 mins under /home/example path.

root@localhost:~# find /home/example -type f -cmin -40

 

Example 9: Find all the files only modified in last 40 mins 

You need to use find /home/example -type f -mmin -40 command to find all the files modified in last 40 mins under /home/example path.

root@localhost:~# find /home/example -type f -mmin -40

 

Example 10: Find all the files only accessed in last 40 mins 

You need to use find /home/example -type f -amin -40 command to find all the files accessed in last 40 mins under /home/example directory.

root@localhost:~# find /home/example -type f -amin -40

 

Example 11: Find all the created files and directories older than 40 mins

You can use below command to check all the created files and  directories older than 40 mins.

root@localhost:~# find /home/example -cmin +40

 

Example 12: Find all the modified files and directories older than 40 mins 

You can use below command to check all the modified files and directories older than 40 mins.

root@localhost:~# find /home/example -mmin +40

 

Example 13: Find all the accessed files and directories older than 40 mins 

You can use below command to search all the accessed files and directories older than 40 mins.

root@localhost:~# find /home/example -amin +40

 

Example 14:  Find all the created files only in last 5 days

You can use below command to find only files created in last 5 days.

root@localhost:~# find /home/example -type f -mtime -5

-mtime n: File's data was last modified n*24 hours ago.

 

Example 15: Find all the modified files only older than 5 days

You can use below command to find only modified files older than 5 days.

root@localhost:~# find /home/example -type f -mtime +5

 

Example 16: Find all the files and directories modified in last 5 days 

You can use below command to find all the files and directories modified in last 5 days.

root@localhost:~# find /home/example -mtime -5

 

Example 17: Find all the modified files and directories older than 5 days 

You can use below command to find all the modified files and directories older than 5 days.

root@localhost:~# find /home/example -mtime +5

 

Example 18: Find a file "test.txt" at the maxdepth of level 2

You can use below find command in linux to search a file "test.txt" at the maxdepth of Level 2.

root@localhost:~# find /home/example/ -maxdepth 2 -name test.txt

-maxdepth levels: Descend at most levels (a non-negative integer) levels of directories below the starting-points.

 

Example 19: Find a file "test.txt" at the mindepth of level 2

You can use below find command in linux to find a file "test.txt" at the mindepth of Level 2.

root@localhost:~# find /home/example/ -mindepth 2 -name test.txt

-mindepth levels: Do not apply any tests or actions at levels less than levels(a non-negative integer).

 

Example 20: Find a file test.txt between mindepth of 2 and maxdepth of 4

You can run below command to find a file test.txt between mindepth of 2 and maxdepth of 4.

root@localhost:~# find /home/example -mindepth 2 -maxdepth 4 -name test.txt

 

Example 21: Find all the files created by User "example"

You can use find /home -user example command to find all the files created by User example as shown below.

root@localhost:~# find /home -user example

 

Example 22: Find all Empty Files

You can use find /home/example -empty command to find all empty files under /home/example path.

root@localhost:~# find /home/example -empty

-empty: File is empty and is either a regular file or a directory.

 

Example 23: Find all Executable Files and Directories

You need to use find /home/example -executable command to find all executable files and directories under /home/example location.

root@localhost:~# find /home/example -executable

-executable: Matches files which are executable and directories which are searchable (in a file name resolution sense) by the current user.

 

Example 24: Find all Executable Files Only

You need to use find /home/example -type f -executable command to find only executable files under /home/example location.

root@localhost:~# find /home/example -type f -executable

 

Example 25: Find all Empty Files Only

If you want to find all empty files under /home/examples location then you need to use find /home/examples -type f -empty command as shown below.

root@localhost:~# find /home/examples -type f -empty

 

Example 26: Find all the Case Insensitive match pattern 

You can use below command to find the match pattern which does not need to be case sensitive under /home/example.

root@localhost:~# find /home/example -iname File1

-iname pattern: Like -name, but the match is case insensitive.

 

Example 27: Find all the files on ext4 filesystem

You can use below command to find all the files on ext4 filesystem under current path.

root@localhost:~# find . -fstype ext4

-fstype type: File is on a filesystem of type ext4.

 

Example 28: Find all the files with group name test

You can use below command to find all the files with its group name test under /home/example path.

root@localhost:~# find /home/example -group test

-group gname: File belongs to group gname (numeric group ID allowed).

 

Example 29: Find all the files which does not belong to any known group

You can use below command to find all the files under current directory which does not belong to any known group.

root@localhost:~# find . -nogroup

-nogroup: No group corresponds to file's numeric group ID.

 

Example 30: Find all the files which does not belong to any known User

You can run below command to find all the files under current directory which does not belong to any known user.

root@localhost:~# find . -nouser

-nouser: No user corresponds to file's numeric user ID.

 

Example 31: Find all the files by their Group ID

You can use below command to find all the files with the file group ID 500.

root@localhost:~# find /home/example -group 500

 

Example 32: Find all the files by their User ID

You can run below command to find all the files with its file User ID 500.

root@localhost:~# find /home/example -user 500

-user uname: File is owned by user uname (numeric user ID allowed).

 

Example 33: Find all the files which are either executable by User or by Group

You can use below command to find all the files which is either executable by user or by group.

root@localhost:~# find /home/example -perm /u=x,g=x

-perm /mode: Any of the permission bits mode are set for the file. Symbolic modes are accepted in this form. You must specify `u', `g' or `o' if you use a symbolic mode.

 

Example 34: Find all the files which are executable by both User and Group

You can use below command to find all the files which are executable by both user as well as group.

root@localhost:~# find /home/example -perm -g+w,u+w

-perm -mode: All of the permission bits mode are set for the file. Symbolic modes are accepted in this form, and this is usually the way in which you would want to use them. You must specify `u', `g' or `o' if you use a symbolic mode.

 

Example 35: Find all files and directories having execute permission

You can execute find /home/example -perm /111 command to find all the files and directories which has execute permission.

root@localhost:~# find /home/example -perm /111

 

Example 36: Find all the files created between last 5-10 days

You can use find . -ctime -10 -ctime +5 command to find all the files created between last 5 to 10 days in the current location.

root@localhost:~# find . -ctime -10 -ctime +5

 

Example 37: Find all the files accessed between last 5-10 days

You can use find . -atime -10 -atime +5 command to find all the files accessed between last 5-10 days from the current location.

root@localhost:~# find . -atime -10 -atime +5

 

Example 38: Find all the files modified between last 5-10 days

You can use find . -mtime -10 -mtime +5 command to search all the files modified between last 5 to 10 days in the current location.

root@localhost:~# find . -mtime -10 -mtime +5

 

Example 39: Find all the files which is either readable by User or by Group

You can use below command to find all the files which is either readable by user or by user and does not necessarily need to be readable by both.

root@localhost:~# find /home/example -perm /u=r,g=r

 

Example 40: Find all the files which is readable by both User and Group

To find all the files which is readable by both User and Group, you need to run below command.

root@localhost:~#find /home/example -perm -g+r,u+r

 

Also Read: How to check Port Connectivity in Linux

Reference: Find Man Page

Leave a Comment