You cannot always remember where you created and stored all the files and directories, and what good a file would do if you cannot locate it when you need it. Well, your Linux operating system comes equipped with the find command to help you do just that. Find is a powerful command-line utility that offers you a number of ways to search for files based on different criteria, not just file names. You can use this single command to search and locate files and directories that meet the conditions specified in the arguments.
In this tutorial, we learn about find command in Linux and useful examples to locate files and directories.
Prerequisites
- A Linux system or UNIX-like system.
- Basic Knowlege of command line.
- A user with required permissions.
What is Find Command in Linux
The find command is used to find files and directories in Linux. You can specify various parameters and arguments with this command to narrow down the search results based on your specific requirements. The find command supports file name, file types, folder name, file creation date, file modification date, permissions, and ownership arguments.
You can also use the -exec
option to run other commands on the files and folders that you locate using the find command in Linux.
Find Command Syntax
The basic syntax of find command:
find [options] [path...] [expression]
Find Command Options
Some of the main options of the find command are as follows:
Options | Description |
-P | Never follow the symbolic links. |
-L | Follow the symbolic links. |
-H | Follow the symbolic links only while processing the command-line arguments. |
-D | Supply debug options, such as tree, stat, opt, and rates. |
-Olevel | Enable query optimization, and specify a decimal number that represents the optimization level. Accepted values are 0, 1, 2, and 3. |
-name | Search files and directories that have the given name. |
-user | Search files and directories owned by the specified user name. |
-exec | Execute specified command on the files and folders searched using the find command. |
How to use the find Command
To search and locate a file named as the given name, use the Linux find command with the -name
option. This is probably one of the most frequent uses of this command. You can also specify multiple files after the -name
option.
For example, use the following search string to find the specified directory in the home directory:
find . /home -name Documents
If the specified file or directory is not available, then it does not return any error. It just shows your Linux prompt. In such a case, you can try either finding files in a different directory to widen the search area, or use other parameters like file type, file size, owner name, and ignore file name case, as explained in the examples below.
To search and locate a file in the current working directory, use the following command:
find . -name sample.txt
You can also define the type of objects as f
to search just for the regular files.
When searching for the files, you must always specify the correct file extension. If you do not specify an extension, then this command does not return any results, even after a successful command execution.
For example, the following command does not return any results or errors because you did not specify a file extension:
find . –name sample
Linux Find Command Examples
The following examples show different use cases for the command.
Find Files by Using Extension
If you do not remember all the file names, but you remember the file type or extension of the file, then you can search all the files with a particular extension. Some of the most commonly used extensions are .txt
for txt files, .php
for PHP files, and .exe
for executable files.
The following example shows how to find files with the .txt
extension in the current directory:
find . -name *.txt
Find Files by Using Name, but Ignore Case
Unlike Windows, Linux file system and operating system are case-sensitive, which means you can create files or directories with the same name but different cases. For example, a directory can have files named as sample, Sample, and SAMPLE. However, sometimes it could prove difficult to remember the exact name and case of the file name. In such a case, this command proves helpful with its -iname
option that ignores file name casing and find files with lowercase, uppercase, and title case names.
For example to perform a case-insensitive search for the file named sample.txt, type:
find . -iname sample.txt
Find Files and Directories by Using Type
The Linux operating system treats everything as a file, even the directories. You can create files and directories with the same name. It means when you search for files, you need to supply specific arguments to narrow the search results. For this purpose, the find command enables you to specify the type of file you are looking for. Some of the most common options are f (regular file), d (directory), l (symbolic links), and c (character devices).
The following example shows you how to search and locate a directory with the name sample in the Documents
directory by specifying the -type d
to find directories:
find ./Documents –type d –name sample
Find Files by Using Size
Every file that you store on your Linux hard drive takes certain disk space, which you can use as a parameter to find files that are either equal to, lower than, or higher than the given size.
You can specify the following operators before the size:
- - (minus) before the size, like -2MB, to search for all the files lower than 2MB in size.
- + (plus) before the size, like +2MB, to search for all the files higher than 2MB in size.
- Do not specify any operator before the size, like 2MB, to search and list files that are exactly 2MB in size.
Furthermore, you can use different suffixes to specify the size, such as k (KB), m (MB), and g (GB). You can specify 0k to find empty files and empty directories, and other size parameters to find non-empty files.
For example, use the following command to find files that are exactly 2MB in size:
find . -size 2M
You can specify 0k to find empty files and empty directories, and other size parameters to find non-empty files.
Find Files by Using Owner
Linux is a multi-user operating system. You can create multiple users on your Linux computer, which can have different access levels, and can own different files and directories.
To find files owned by the demo user name and matches the specified file name, run the following find command in Linux:
find . -user demo -name SAMPLE.txt
This option is helpful in various scenarios, including the one where you can search for the files that are owned by a particular user and change their ownership to other users.
The same way can find files owned by specific group:
find /project -group devusers
Find all *.c files owned by group named devusers:
find /project -name "*.c" -group devusers
Find and Execute Command
You can execute other Linux commands on the resulting files of this command. For example, to delete files found using the find command, run the following command:
find . –name sample.txt –exec rm –i {} \;
The above command returns a single file, which you can confirm to delete by typing yes.
Find Files and Directories with Permission
Multiple users can access a Linux system simultaneously, and to avoid any unauthorized access, Linux provides very effective access control on the files and directories. This capability is achieved by file permissions, attributes, and ownership.
The find command can also be used to search files or directories with specific permission. For example to find SUID files that have permissions set to 4000 in the current directory:
find . -perm 4000
The ls -l
command prints the files in a list long format which shows SUID bit and same permission files we can search using the find -perm command.
Conclusion
In this tutorial, we learned about find command in Linux and its useful options. For more information visit the find man page or simply type man find from the terminal.
Comments