How to Search Files & Directories in Linux with find Command
Developers and experienced users of Linux choose Linux Terminal to interact with the operating system and perform their tasks because of the capabilities that Linux Terminal offers to facilitate their tasks. Contrary to the thought of the majority of Linux users regarding the complexity of the Linux terminal, this command line interface provides useful tools and valuable features to speed up and facilitate your tasks and the progress of your projects.
Many Linux users prefer the Linux graphical interface to advance their goals, but for your progress, you should not rely only on the graphical interface and stay away from the exciting world of the Linux terminal. The key to the success of your projects when using Linux is to be familiar with comprehensive Linux commands. After familiarizing yourself with Linux commands, you will understand the value of Linux tools and commands when performing your tasks.
Therefore, after buying a Linux VPS, learning Linux commands should be one of the priorities of Linux server managers, who often shirk this responsibility, while they are unaware of the miraculous effects of Linux commands in improving the performance of their projects.
Have you ever searched for the desired file or directory in Linux, but you don’t know how to find the desired file with specific filters? Searching for files and directories in the Linux terminal may be difficult for many users and they don’t even know how to search for the files they want.
The Find command can help you in searching for the desired files and directories. you can find the files in a directory hierarchy of your Linux system based on various filters and parameters that you have specified, such as size, name, permissions, type, extension, access level, owner, date& time, etc by the Find command. Also, you can apply an operation on the files after finding your desired file.
The Find command is a powerful and amazing tool in Linux that uses a simple conditional mechanism to find a list of files matching certain conditions specified by the user. The Find command is one of the most important and most used commands in all Linux and Unix-based operating systems that every Linux user should be familiar with. In this article, you will learn how to use the Find command to search for files in a specific directory and its subdirectories.
Syntax of “find” command
At first, it is better to familiarize yourself with the main syntax of the Find command, which is as follows:
find [path] [options] [expression]
[path] attribute: In this section, you specify the directory where you want to search for a file.
[options] attribute: You define the factors that you filter when searching for a file, such as a name, permission, and date, in this section. This feature can monitor operations such as the Find process optimization method, symbolic links, and debugging options.
[expression] attribute: It defines the actions you perform with the file and it consists of search patterns and options. It also controls the directory hierarchy to find files in the directory.
For example, to find files that end with .conf in the /usr directory, you must run the following command:
find /usr -type f -name "*.conf"
Output:
/usr/src/linux-headers-5.4.0-88-generic/include/config/auto.conf
/usr/src/linux-headers-5.4.0-88-generic/include/config/tristate.conf
/usr/src/linux-headers-5.4.0-90-generic/include/config/auto.conf
/usr/src/linux-headers-5.4.0-90-generic/include/config/tristate.conf
/usr/share/adduser/adduser.conf
/usr/share/ufw/ufw.conf
/usr/share/popularity-contest/default.conf
/usr/share/byobu/keybindings/tmux-screen-keys.conf
/usr/share/libc-bin/nsswitch.conf
/usr/share/rsyslog/50-default.conf
. . .
As you can see, in this example, we searched for files by their type and name, the Find command has made it possible to consider different criteria in searching for files by combining options. In the main syntax of the Find command, separate the terms by -and or -or. Pay attention as follows:
find -name query_1 -or -name query_2
Therefore, in the results of the previous command, we will see files whose names match query1 or query2.
You can replace the features you want in the main syntax of the Find command based on your needs.
Note: To execute the Find command to search for a file in a specified directory, the user must have the necessary privileges to read the files in the directory.
Optimizing the process of finding files by the Find command
To optimize the performance of the find command in finding files and directories, you can change the default configuration for the Find command using applicable options. The Find command options are as follows:
- -L option: To search and find symbolic links using the Find command, add the -L option in the main syntax of the Find command.
- -O1 option: It is a kind of Find command strategy to search by default with a file name filter.
- -O2 option: In addition to prioritizing the file name filter, it runs the file type filter before running other tests.
- -O3 Option: It is the best way to optimize the file search process because by default it organizes all tests based on the probability of success and efficient use of resources.
- -iname option: Ignores case sensitivity in the search.
- -maxdepth X option: Searches the depth of the main branch and sub-branches of X.
- -not option: It finds all items except the test item you defined. (reverse search)
Finding files based on file type
One of the file search filters using the find command is the Type filter. which you can execute in the form of the following syntax:
find -type "type_descriptor "query
By adding the “-type” flag to the Find command, you can search the file and directory based on its type. In addition, there are options to search for the file based on its type that you can add to the Find command for different purposes, including the following options:
- -type f option: searches for files.
- -type d option: searches directories.
- -type l option: searches for symbolic links.
- -type c option: searches for character devices.
- -type b option: searches for block devices.
- -type s option: searches for the desired socket.
- -type p option: Searches named pipe (FIFO).
For example, to find all the character devices in the /dev directory of your Linux system, you should run thefind /dev -type c
command, or if you want to find all the directories in your system, you should enterfind . -type d
command.
Finding files based on the file name
The easiest way to find files is to search for files by file name. To find a file using the Find command based on the file name, you must run the following command:
find -name "query"
Note: When searching for a file by specifying the file name criteria in the Find command, you must remember that the Find command pays attention to the uppercase and lowercase letters of the file name. To ignore the case sensitivity when you search with the Find command, you must Add the-inname
option to the Find command:
find -iname "query"
To find files whose names do not match the pattern you specified, use the following command:
find -not -name "query_to_avoid"
To reverse the search, you can also use the symbol (!) in the syntax of the Find command:
find \! -name "query_to_avoid"
Finding files based on File Extension
You can search for a specific file through the file extension by executing the Find command, this method is not significantly different from searching for a file based on the file name. For example, if you want to search for files ending in *.txt in the /home/username/ directory and its subdirectories, you must enter the following command:
find /home/username/ -name "*.txt "
Note that in this method, you must put the pattern in the quote symbol along with the * sign so that the process of finding the file goes correctly. As a result, you will find the files ending in .txt along with the directory to which those files belong.
If you want to find files that do not end with .txt extension and do not match the pattern you defined, you should run the following command:
find /home/username/ -not -name "*.txt "
Finding files based on permissions
The -perm option is used to search the file through the permission in the Find command, in which case you must enter the permission code in the Find command. For example, if you want to find a file that matches permission 644, you should enter the Find command as follows:
find / -perm 644
In the output of this command, files matched with 644 permission are searched and found. To specify the minimum permission in the file search criteria, you need to change the main syntax of the Find command as follows:
find / -perm -644
As you can see, to determine at least the permission before the license code, we use the minus sign to find all the files that have additional licenses, for example, in the output of the previous command, files with 844 permission are also found.
Finding files based on the owner
In a situation where you may be looking for a file that belongs to a user or a group and you do not have any other information about the files except the ownership of the files, in this situation, the Find command will help you. you can reach your goal by using options like -user and -group and find the desired file easily.
Suppose you are looking for a file created and used by a user named “Opera” and located in the /var directory, run the following command to find this file:
find /var -user opera
By replacing -group in the previous command, you can also search for a file that belongs to a group.
Finding files based on size
By using the Find command, Linux users can search for a file by its size. For this purpose, just add the -size parameter to the Find command along with the number that belongs to the size of the file you are looking for.
To filter the file search by size, you must specify the size of the file in bytes, megabytes, gigabytes, or another size. The suffix c is used to determine the size in bytes, the suffix K is used to determine the size in kilobytes, the suffix M is used to determine the size in megabytes, the suffix G is used to determine the size in gigabytes, and the suffix b is used to determine the size in terms of 512-byte blocks.
Pay attention to this example to better understand how to filter the search by size using the Find command:
find /tmp -size 1024c
The output of this command will show all files with a size of 1024 bytes in the /tmp directory.
For example, if you are looking for files with a size less than 1024, you should execute the following syntax to achieve what you want:
find /tmp -size -1024c
To search for files whose size is more than 1024 bytes, you must enter the following command:
find /tmp -size +1024c
In determining the size filter, you can consider a range for the size of the file and search for files that have a size within the range of your definition range. For example, if you are looking for files between 1 and 2 MB in size, you should run the following command:
find . -type f -size +1M -size 2M
Note : The “.” symbol indicates searching in the current directory.
Finding files based on Time
When the user reads the content of a file or makes a change in it and saves it, that time data is saved in the file. This time data of each file is divided into three categories, Access Time (when you access the content of the file), Modification Time(the last time you edited the file), and Change Time (the last time you modified the file’s inode metadata).
Using the powerful Find command, you can filter the directory hierarchy based on the last file modification time to search for files. To filter based on time, you can search for your files in the desired time range using the – and + symbols along with the time period you are considering. For example, to search for a file that has been last modified for the past 5 days and is in the /usr directory, the following command is executed:
find /usr -mtime 5
To search for files that you accessed less than 5 days ago, enter the following command:
find /usr -atime -5
To find files whose meta information you edited more than 5 days ago, run the following command:
find /usr -ctime +5
The Find command does not limit you to specifying the day in determining the time filter when searching for files, you can define the minute parameter. Pay attention to the following example to learn how to run the command to find files in minutes, if you want to find files that were edited less than 5 minutes ago, run the following command:
find /usr -mmin -5
With the Find command, you can specify a reference file and search for the most recent files created in comparison to the reference file:
find / -newer reference_file
Finding files based on content by combining find and grep commands
The Find command alone is not able to search files through their content and can search files through type, name, time, ownership, and metadata. If you want to find a file through its content, the grep tool helps the Find tool to search for a specific string of characters and words.
If you want to search for a text file based on the content of “example” in the current directory hierarchy, the command you should run is as follows:
find . -type f -exec grep "example" '{}' \; -print
As a result, the previous command searches for the term “example” in the text files in the current directory and displays text strings containing the term “example”.
Finding the file using the Find command and processing the file
Another advantage of the Find command is processing files by executing commands after finding the desired file. For this purpose, add the-exec
option to the Find command to run commands with -exec in the root directory of the Find process. For example, to find files named” example.conf “in the current directory and then change the permissions of the files found, run the following command:
find . -name "example.conf" -exec chmod o+r '{}' \;
By executing the previous command, the files under the title “example.conf” are found in the current directory and thechmod o+r
command is executed to change the permissions of the files found.
For better processing performance and improved security, it is recommended to use the-execdir
option.
Note: When executing the-exec
or-execdir
parameters, you will not see a request for confirmation before processing. You should replace-exec
with-ok
and-execdir
with-okdir
if you want to accept the requested confirmation before actions.
Finding the file using the Find command and deleting the file
After finding a file that matches the pattern and criteria you specified, you can delete the desired file by adding the-delete
option to the Find command. To delete a file found based on your criteria, use the following command:
find . -iname mytextfile1.txt -exec rm {} \;
The output of the previous command deletes Mytext file 1. To delete all hierarchical files that have the extension .txt and are located in the current directory, enter the following command:
find . -name *.txt -delete
FAQ
How to change ownership of files using the find command?
With an example, we will explain how to change the ownership of files after finding a file that matches the specific pattern you specified. If you want to change the ownership of the files belonging to the Opera user to Nginx, you must run the following command:
find / -user Opera -type f -exec chown nginx {} \;
Conclusion
In this article, you got acquainted with the Find command and learned how to use the Find command to search files and directories by defining different criteria in the Linux system. The Find command allows users to process files and even delete files after finding them.
The Find command can be used for different purposes and functions. In this article, we have comprehensively taught how to use the Find command. We hope that reading this article will speed up and facilitate the process of searching files for your various purposes.
Thank you for choosing our article to read.