We’re going to walk you through on how to find a file in Linux in this lesson. This course will concentrate on locating files using the command line.
In Linux, there are two commands you may use to find a file: locate and find. Both commands will be covered in this lesson.
You can have any linux system to try and explore these commands. If needed support for OS Installation refer to How to Install Rocky Linux
Find files with the find command
Syntax and example
The syntax would be :
find [OPTIONS] [PATH] [EXPRESSION]
- Several [OPTIONS] and flags are available to regulate the way the find command operates.
- The path or place where you wish to locate the command to look for a file is [PATH]. The script will thus begin looking for a file in that directory and any other subdirectories if you use /etc/.
- The “criteria” you wish to use in order to locate the file is the [EXPRESSION]. This might be anything else, even a string. More instances are shown below.
Let us see a very basic example
find -iname "test.txt"
The above command will output a list of all locations where a file named “test.txt” is located. This is known as “searching by name”
The “-iname” option is used to locate a file by name. The file name that the program retrieves would be case-sensitive if you supplied “-name.” It would not locate a file called “Example.txt” if one existed. By setting -iname to disregard the file’s case, it discovers all occurrences of the file name, such as “test.txt,” “TEST.TXT,” “tExT.txt,” and so on.
Finding files with no match criteria
Simply use the -not option to identify files that DON’T fit specific criteria. For instance:
find -not -iname "test.txt"
The above will locate every file that isn’t “test.txt.”
Finding files by file type
Use the “-type” option to locate and filter files based on their file type.Few exmaple for the types are
- regular files : f
- directories: d
- block devices: b
- symbolic links (symlinks): l
- character device: c
Example, if you need to list all the symlinks under the directory /usr/bin you can use this command
find /usr/bin -type l
Finding files by Combining options & criteria
The options can be combined into a single command. To locate every symlink that has the word “python” in it, for instance, you may use:
find /usr/bin -type l -iname "*python*"
Options are connected together by the operand “-and” by default, requiring the fulfillment of both requirements. To obtain results that return either of the choices, use the argument “-or.” As an illustration:
find -type -d -or -iname "python"
The above command will provide all directories AND all files that are named “python”. If you didn’t use the “-or” option, you would get all directories that are named “python”.
Finding files by size
You can also find the files using the option to filter by size. Here is the filters
- kibibytes: k
- megabytes : M
- gigabytes: G
- byte blocks: b
- bytes: c
- two-byte words: w
Therefore, you may use the following command, for instance, to discover all files in the /var/lib directory that are greater than 1 GB:
find /var/lib -size +1G
Another can can be finding files of size less than 1 GB under the folder /opt
find /opt -size -1G
Finding files by time
The find function allows you to search for and filter files based on time. To be more precise, you have the following options:
- “-atime” – to locate the files according to the most recent read or write time (Access Time)
- “-mtime” – to locate files according to their most recent modification time (Modification Time)
- “-ctime” – to locate files according to the most recent modification date of the file’s inode information (Change Time)
By default. the parameters use days. For instance, you may use the following command to locate files in the /home/admin directory that have been altered in the recent two days:
find /home/admin -mtime 2
You can use the following to locate files that were altered more than three days ago:
find /home/admin -mtime +3
Days can also be substituted with minutes. That requires the usage of the “-mmin” option. For instance, you may use the following to locate every file in the /var/log directory that has been altered in the previous 10 minutes:
find /var/log -mmin 10
Finding files based on permission
You may use the “-perm” option to search files with certain permissions. For instance, you may run the following command to locate every file in the /etc/ directory with “777” permissions:
find /etc -perm 777
Use the following command to locate every file in the /etc/ directory with at least 644 permissions:
find /var -perm -644
The above command will return files that have permissions higher than 644, like 744, 777, etc.
Find files with the locate command
Package Installation
The locate command might not be accessible by default. Simply install the “mlocate” package to get it installed.
For Debian/Ubuntu
sudo apt install mlocate -y
For RHEL/Rocky/CentOS
sudo dnf install mlocate -y
It may be noted that locate command runs based on its database. The database gets refreshed once a day. Locate is quicker than the find command since it takes use of a database. To refresh the database, just execute the below command:
sudo updatedb
Using the locate commands by files name
Let us now see the syntax to use the locate command
locate <pattern>
In the above , Where “<pattern>” is the word you want to search for in the file names. For example:
locate -i python
will locate all “python” files. Because the program ignores case (no case sensitivity) when using the “-i” option, the files “PyThon” and “python” will both be discovered.
Counting number of files
Rather than using the locate command to identify files, you may use the following command to count the number of files:
locate -c python
The above command will provide the number of files named “python”.
It’s crucial to update the database before utilizing the find command since any files added or removed after the database update will not impact your locate command searches.
Conclusion
Now that you have learnt on how to use the find and locate commands to find files in Linux you will find it easy for any troubleshooting on any flavors of Linux system. You may also refer here for more details