The ‘find’ is a command utility for searching files and directories recursively based on the simple conditional mechanism. The ‘find’ command utility is available on the majority Linux Unix-like operating system, including the Ubuntu, RedHat, CentOS, Debian, etc.
By default, the ‘find‘ command is supported by a variety of conditions. You can search files and directory based on their specific names, file type, owner of the file (user and group), date modified and access, size, etc.
Also, the ‘find’ command will automatically search recursively to all directories, and to make your search faster, specify your path directory and use the ‘-maxdepth’ option to limit the level on your recursive search directories.
Additionally, you can use ‘find’ command to search files/directories and process it immediately through the ‘-exec’ or ‘-execdir’ options or you can use the ‘-delete’ options to immediately delete all files that match your search criteria.
What you will learn?
In this tutorial, you will learn how to search for files or directories using the ‘find’ command on the Linux system. You will learn how to use the ‘find’ command with some different scenarios that you can apply to the real world.
Step 1 – Search Files using the Find Command on Linux
As you can see on top, the find command can search different kind of files based on some criteria, including the file type/extensions, size, date, permission, owner, etc.
1. Find Files by Name
To find files with a specific name, use the ‘-name’ option as below.
find /path/to/directory -name "filename.md"
2. Find Files by Type/Extensions/Format
Next, if you forgot about the name of your file, but you knew the type/extensions of your file, you can use the ‘-type’ option to specify file type extensions of your search.
Below is an example for all files with the specific extensions ‘.txt’.
find /path/to/directory -type f -name "*.txt"
Additionally, you can combine your search with the ‘grep’ command if you get the pattern of your file name as below.
find /path/to/directory -type f -name "*.txt" | grep pattern
3. Find Files Ignore Case-Sensitive
The ‘find’ command is case sensitive, to ignore it, you can use the ‘-iname’ option as below.
find /path/to/directory -iname filename.md
4. Find Files by Permission
The ‘find’ files is can be used to search files for specific permission. And you can search for files by permission number (Numeric Mode), SUID and GUID.
– Find Files with Specific Permission Number
To find files with a specific permission number, you can use the ‘-perm’ option as below.
find /path/to/directory -type f -perm 0664
– Find Files Except for the Permission
Next, if you want to find all files except file with the permission ‘0664’, you can add the ‘!’ before the ‘-perm’ option.
find /path/to/directory -type f ! -perm 0664
– Search for SUID Files with find Command
SUID is a special permission fo executable files that allow other users to execute the file with the same permission as the owner. In general, SUID is applied to binary executable files on the Linux system with the ‘x’ execute permission with the ‘s’ as special permission for the user.
To search for all SUID files, you can use the find command below.
sudo find / -perm /u=s
– Find Executable Files
An executable file on Linux has a attributes permission as ‘x’ or the permission number ‘7’.
To search for all executable files, you can use the ‘-perm’ option as below.
find /path/to/directory -type f -perm /a=x
– Search for Read-only Files
The read-only files have attributes with ‘r’ for the user.
To find all read-only files on the Linux system, use the following command.
find /path/to/directory -perm 0444
5. Find Files by Ownership
The find command allows you to find files owned by specific users and groups. To find files by a specific user, use the ‘-user’ option, and for the group, use the ‘-group’ option.
– Find Files Owned by User
To find files owned by a specific user, run the following command.
find /path/to/directory -user username
– Find Files Owned by Group
And to find files owned by a specific group, you can use the command below.
find /path/to/directory -group www-data
6. Find Command by Size
To find files based on file size, use the ‘-size’ option, and define the min/max number of your criteria. This option is useful for finding files smaller, or bigger, or between the N number. And you can use the ‘k’ as kilobytes, ‘M’ as megabytes, and ‘G’ as Gigabytes.
Below some examples of the find command for searching files based on size.
– Find Files Smaller than N
Search for files smaller than 5MB, use the ‘-size’ option following by the ‘-N’ as minus number.
find /path/to/directory -type f -size -5M
– Find Files Bigger than N
Next, find files bigger than 50MB, us the ‘-size’ option following by the ‘+N’ as plus a number.
find /path/to/directory -size +50M
– Find Files Between N Size
Now find files based on size between the ‘N’ number. You can use the two ‘-size’ option with the ‘+N’ and ‘-N’ number.
find /path/to/directory -size +5M -size -10M
7. Find Command by Time
The find command allows you to search for files based on time modified and time accessed. Also, you can search for files based on time minutes and hours.
– Find command by Days
Search files with the last 10 days modified with the ‘-mtime’ option as below.
find /path/to/directory -type f -mtime 1
Next, to search files for the last 10 days accessed, use the find command with the ‘-atime’ option as below.
find /path/to/directory -atime 10
– Find command by Minutes
Now if you want to search files based on time minutes, use the ‘-amin’ for last accessed in minutes, ‘-mmin’ for last modified in minutes, and ‘-cmin’ for last change in minutes.
Search files for last modified in minutes.
find /path/to/directory -mmin 2
Search files for last accessed in minutes.
find /path/to/directory -atime 10
Next, search files for last changed in minutes.
find /path/to/directory -cmin -60
– Find command between Time
To search files between N times, you can use two options with ‘+’ and ‘-‘ on each option.
Find the last modified files in the range of 10-30 days.
find /path/to/directory -mtime +10 -mtime -30
Find last accessed files in range 10-30 days.
find /path/to/directory -atime +10 -atime -30
Step 2 – Search Directory using the Find Command on Linux
To search the directory using the find command, you can use the ‘-type d’ option, which means as type directory.
1. Find Directory by Name
To search directory with the specific name, use the find command with the ‘-name’ option as below.
find /path/to/search -type d -name "dir3"
2. Find Directory and Ignore Case Sensitive
To ignore the case sensitive on your search, use the ‘-iname’ option as below.
find /path/to/search -type d -iname "DiR3"
3. Find Directory and Ignore Permission Denied Error
When you’re searching for a directory with on the ‘/’ root path directory, you will get the “Permission Denied” error message.
To ignore the error message, append “2> /dev/null” to the end of command, which means redirect all stderr (FD 2) to black hole ‘/dev/null’.
Below is an example searching for a directory named ‘icons’ by ignoring case sensitivity and the error message.
find -O2 / -maxdepth 2 -type d -iname "Nginx" 2> /dev/null find -O2 / -maxdepth 3 -type d -iname "Nginx" 2> /dev/null
Step 3 – Optimizing Find Command with the Level and Maxdepth
To optimize your search using the find command, use the level and max depth options.
1. Find Optimization using Level
The find command provides 4 level optimization for searching files and folders.
- Level 0: This equivalent to level 1.
- Level 01: The default optimization, which is traditional search behavior, filter-based file name first.1. Find Optimization using Level
- Level 02: The level 02 optimization allows you to search the file name first ‘-name/-iname’, then the file type ‘-type/-xtype’.
- Level 03: At this optimization level, the search will automatically re-order using the efficiency of resources and the likelihood to get the best and fastest results.
To use the level optimization, add the ‘-Olevel’ before the starting point of your find command as below.
find -O2 /path/to/directory -type d -iname "Dir*" find -O3 /path/to/directory -type f -perm 0664 -name "*.md"
2. Find Optimization using Maxdepth (Exclude Directory)
By default, the find command will automatically be searching for files and directory recursively on your starting point directory. To optimize and make your search process fast, you can define the ‘maxdepth’ with N level directory on your find command.
The ‘-maxdepth’ option with the ‘0’ value will have no result, and the value level ‘1’ will only search for files on the current starting point directory, and if you use the level number ‘2’, it will perform search files on the starting directory and one level inside your current starting directory.
Below is an example of the find command with the ‘-maxdepth’ with level 3 to search on the starting point directory and two directories inside it.
find /path/to/directory -maxdepth 3 -type f -name "*.md" find /path/to/directory -maxdepth 2 -type f -name "*.md" find /path/to/directory -maxdepth 4 -type f -name "*.md"
3. Combine the Maxdepth and Level to Optimize the Find Command
Below is an example to make your searching process more optimal and fast by combining the ‘-maxdepth’ and the level options.
find -O2 / -maxdepth 3 -type d -iname "Nginx" 2> /dev/null
Finally, you’ve learned how to search for files and folders using the find command on Linux systems.