find Command in Linux
The find command in Linux searches for files and folders based on their name, type, or when they were last changed. Searching for files is an important task in any operating system, and the findis a powerful tool to do this quickly.
The find command searches through the folders and files starting from a location you choose. It reads the search conditions you give from left to right and continues until it finds what you're looking for.
Table of Contents
Here is a comprehensive guide to the options available with the find command −
Syntax of find Command
The basic syntax of the Linux find command is as follows −
find [options] [path] [expression] [actions]
In the syntax −
- [path] is the path of the directory from which the search begins. If no path is specified, a dot (.) is assumed, indicating the current working directory.
- [options] manage how symbolic links are handled. For instance, the -P option excludes symbolic links from the search, while the -L option includes them.
- [expression] specifies additional criteria for the search. For example, to search for files by name, the -name option is used with the file name.
- [actions] define what to do with the found files, such as deleting them, executing a command, or printing the output, and may include various modifiers.
Options / Flags Commonly Used with find Command
The options of the find command are listed below −
Options | Description |
---|---|
-name pattern | Finding the files by specified pattern |
-iname pattern | Finding the files by specified pattern ignoring the case-sensitivity |
-type [ d | f| l ] | Finding files by types d, f, and l indicates directories, files, and symbolic links respectively |
-size n[cwbkMG] | Finding files by size n |
-path pattern | Finding files by the specified path pattern |
-perm mode | Finding files by permission (mode can be specified as 754 or u+x) |
-prune | To exclude directories from the search |
-user uname | Finding files owned by a specific user |
-mtime n | Finding files by last modification time (nx24) |
-atime n | Finding files by last access time (nx24) |
-ctime n | Finding files by last change-time (nx24) |
-executable | Finding files that are executable and searchable directories |
-exec command {} \; | Executing a command on the found files |
-ok command {} \; | Executing a command on the found files but prompt for confirmation |
-delete | Deleting the matched files |
Displaying the matched file names | |
-newer file | Finding files that have recently modified |
-maxdepth levels | Searching files up to specified levels of directories deep |
-mindepth levels | Skipping files up to specified levels of directories deep |
-empty | Finding empty files or directories |
-regex pattern | Finding files using regular expression |
-fstype type | Finding files by filesystem type |
-mount | Skipping search across different filesystems |
--help | To display help related to command |
--version | To display the command version |
Examples of find Command in Linux
This section demonstrates the usage of the find command in Linux with examples −
Finding Files by Types
On Linux, files come in several types, including regular files, block device files, and character device files. To locate all regular files, use the -type f option with the find command.
For example, to list all the regular files in the current working directory, use the command given below −
find -type f

To find files in a specific directory, use the following command.
find directory -type f

Note that the command will only list the files in the specified directory, including their paths. To find the block device files, use the b flag with the type.
find / -type b

Here, the forward slash / signifies the root directory.
To find the character type files, use the c flag.
find / -type c

Finding Files by Name
To find files by name, use the -name option followed by the file name. For example, to locate a file named myDoc in the directory named directory, use the find command in the following way −
find directory -type f -name "myDoc*"

In the above command, the asterisk (*) is a wildcard that matches zero or more characters. This means that any file whose name starts with myDoc followed by any characters (or none) will be listed in the output.
For example, files named myDoc1, myDoc_report, or myDoc itself will all be matched. Moreover, the find searches the file through a directory tree, which means the outcomes of the command will be true if the file is in any subdirectory.
Finding Files by Extension
To find all the files by extension, use the -name option with the file extension and * wildcard. For example, to find all the files with the .html extension in a directory, use the following command.
find directory -type f -name "*.html"

The wildcard * precedes the extension pattern to indicate that it will match one or more files in the specified directory whose names end with .html. For example, *.html will list all HTML files. Likewise, to find log files, use *.log, and for configuration files, use *.conf.
Finding Hidden Files
On Linux, the hidden file names are started with a dot (e.g. .file). To find all the hidden files in a directory use -name option with the dot and asterisk wildcard.
find directory -type f -name ".*"

Note that by default, the find command does not display hidden files.
Finding Directories
To find a directory on Linux, the d flag is used with the type option. For example, to find the directory dir in the current working directory, use the following command.
find -type d -name "dir*"

To find a directory in a specific directory, simply mention the directory path.
find directory -type d -name "dir*"

Finding Files by Size
The -size option with the findcommand is used to search for files based on their sizes. Files can be searched for exact sizes, sizes greater than, or sizes smaller than the specified value.
For sizes greater than the specified value, a + sign is prefixed; and for sizes smaller than the specified value, a - sign is used. For example, to find all files larger than 5 megabytes, +5M is used with the -sizeoption.
find . -type f -size +5M

The default unit is b (bytes), other units are listed below.
Units | Description |
---|---|
b | Used for 512 bytes blocks |
c | Used for bytes |
w | Used for two-byte words |
k | For Kilobytes |
M | For Megabytes |
G | For Gigabytes |
Using the following syntax, files can be found by size −
find [path] -type f -size <+/->N[bcwkMG]
The [path] is the directory and N is the size. Skipping the + or - sign in the above syntax will give you the exact size of the files.
Finding Empty Files
The -empty option is used to find empty files in the specified directory. For example, to locate empty files in the current working directory, use the following command −
find . -type f -empty
Finding Files by Modification, Access, and Change Time
To find files based on their modification time, the -mtimeoption is used with n, where n represents a number multiplied by 24 hours.
Prefixing the number with a + or - sign allows searching for files modified more than or less than the specified number of days ago, respectively. For example, to find files modified exactly three days ago, use 3 with the -mtimeoption.
find . -type f -mtime 3
To find files that were modified more than 3 days ago, +3 will be used.
find . -type f -mtime +3
In the same manner, to list files that were last modified less than 3 days, -3 will be used.
find . -type f -mtime -3
To find files based on their modification time in minutes, the -mmin option is used. The prefixes + and - are used in the same way as with -mtime to search for files modified more than or less than the specified number of minutes ago.
For example, to find files modified exactly 10 minutes ago, use the following command −
find . -type f -mmin 10
To find files based on their last access time, the -atime option is used with a number indicating days. For example, to find files that were last accessed more than 3 days ago, use -atime +3.
find . -type f -atime +3
To set the search criteria by the number of minutes, use the -amin.
Similarly, to find files based on the last change time, use the -ctime option. For instance, to locate files that were changed less than 5 days ago, use the -ctime-5.
find . -type f -ctime -5
To set the search criteria by a number of minutes, use the -cmin option.
Finding Files by Permissions
To find files based on their permissions, use the -perm option. For example, to locate all files in the current working directory that are writable by the current user, use -perm -u=w, where u signifies the user and w represents writable permissions.
find . -type f -perm -u=w

The above command can also be used with octal notation.
find . -type f -perm -200
Other approaches to using the -perm option are given below.
-perm mode | Matches files with exact permission bits as specified (octal or symbolic) |
-perm -mode | Matches files where all specified permission bits are set. |
-perm /mode | Matches files where any of the specified permission bits are set. |
Let's understand the above mode with an example.
find . -type f -perm 444 find . -type f -perm -444 find . -type f -perm /444
In the above example, the 444 is the octal notation of giving readable permission to the user, group, and others.
- 4 Readable permissions to user
- 4 Readable permissions to group
- 4 Readable permissions to others
The first command will find the files that exactly match the permissions. The second command is finding files that have the mentioned permission, and the third command is finding files that have one or more of the mentioned permission bits.
Finding and Deleting Files
To find the files in a directory and to delete them, the -delete option is used. For example, to delete all the pngfiles from the directory directory,use the find command in the following way −
find directory -type f -name "*.png" -delete

Finding Files and Executing a Command
In many cases, you might need to use the findcommand to locate files and then execute another command on those results. The find utility offers the -exec option for this purpose, allowing you to run a command on each file found.
For instance, you can find files in a specific directory and use the file command to display the types of those files.
find directory -type f -exec file '{}' \;
In the command above, curly braces ({}) serve as placeholders for the matched files and are enclosed in single quotes to prevent the shell from interpreting them as special characters.
Similarly, the semicolon (;) is also protected, though you can use single quotes to handle it in this context as well. Similarly, to find files that contain the helloLinux word the grep command will be used.
find directory -type f -exec grep -l "helloLinux" '{}' \;

To find files and perform an operation on them, you can use the -exec option with the find command. For example, to locate all text files in a directory named directory and change their permissions to read-only for users and groups, use the following command −
sudo find directory -type f -name "*.txt" -print -exec chmod 440 '{}' \;

Managing Search Depth
To manage the depth of the search into the directories using the findcommand, the -pruneoption is used. The -prune option essentially skips the mentioned directory while performing the search for the find command.
For example, to skip the dir2 directory within dir1 while searching for all files with the .pdf extension, you can use the find command with the -prune option like this −
find dir1 -path "dir1/dir2" -prune -o -type f -name "*.pdf" -print

In the command above, -o represents the logical OR, meaning if the first condition is not satisfied, the second criterion will be evaluated.
The -pruneoption helps skip specific directories, such as backups or large data directories, to speed up the search process.
By default, the find command searches the contents of directories first, followed by the directories themselves. If you want to reverse this order and list files from the deepest directories first, you can use the -depth option.
For example, to search and list files starting from the deepest directory, include the -depth option in your command.
find dir1 -depth -type f -name "*.txt"
The command above will locate text files in the deepest directories first. This can be particularly useful when you want to delete files from the deepest directories before higher-level ones. Below are other options related to depth in the find command −
-maxdepth level | Limits the search to n levels of depth. For example, -maxdepth 2 will search only the current directory and one level below. |
-mindepth level | Ensures the search starts at a depth of n. For example, -mindepth 2 will skip the current directory and begin searching two levels down. |
Conclusion
Browsing files and directories on Linux using a GUI is straightforward, but doing so from the command line can be more challenging.
The find command-line utility is a powerful and flexible tool for locating files and directories. It offers a variety of options to search by name, type, modification time, permissions, and more. Additionally, find allows you to execute commands on the search results.