Users usually try to find a file using the GUI. However, sometimes it becomes a very tedious job to find an individual file in the huge pile of files on your system. Finding it conventionally may be a time-consuming task. To make this task easier, Linux offers certain commands to do this job for you.

find is a popular command used in the Linux systems that can help you search for various files based on their names, type, extension, permissions, owner, etc.

In this tutorial, we will be focusing on finding the files by name. We will look at the different arguments that can be used with the find command. We will also learn about the locate command which is also a faster way to search for the files by name.


Syntax for find command

Using the find command in a proper way can make your task easier. Finding the files of particular type or extension or searching by the name will be possible if you understand the general syntax of this command well.

The general syntax for the find command is as follows.

find [search_path] [expression] [options] [find_what]

I have tried to simplify the syntax of the find command to understand the command better.

Let us look at each attribute of the syntax to get an idea of the significance of each of them.

Search_Path: Here comes the path where we specify the path from where we wish the system to start searching for the file. In short, the starting directory to start the search is specified.

Expression: You can specify the search patterns for the particular file you are searching.

Options: You can use the available options used with the find command in this space.

find_what: In this attribute, put in the name or part of the name of the file to be searched.

Let us illustrate this command with an example.

find /home/gaurav/workspace -name "source.c"

In this command, I am using the find command to search for a file “source.c”. I have asked specifically to search in the ‘/home/gaurav/workspace’ path. Using the -name option allows me to search the file specified by ‘source.c’.

This is the simplest demonstration for using the find command.


Searching files in the current directory

Using the find command in its simplest form is to search for your files in your current working directory.

find .

This command will display all the files in your current working directory. Here the ‘.‘ means the ‘current working directory’. Following is the output from my current working directory. All the files present in this directory are listed without any filters

Output:

.
./context_log.policy
./snap
./snap/couchdb
./snap/couchdb/current
./snap/eclipse
./snap/eclipse/current
./snap/vim-editor
./snap/vim-editor/current
./snap/vim-editor/common
./snap/vim-editor/1
./snap/htop
./snap/htop/current
./snap/htop/common
./snap/htop/common/.local
./snap/htop/common/.local/lib
./snap/htop/common/.local/lib/locale
./snap/htop/common/.local/lib/locale/en_IN.UTF-8
./snap/htop/common/.local/lib/locale/en_IN.UTF-8/LC_CTYPE

To search for a file whose approximate name is known, use the find command as shown below.

find . -name [string_from_filename\*]

Example:

find . -name context\*

This command searches for the files which contains the string ‘context’ in it.

Output:

./context_log.policy
./context.xml
./context_preview.c

All the files in the current directory are listed which contains the string ‘context’ in it.

Now the problem arises with this command when you try to take the liberty about the case sensitivity whilst typing in the filename or the approximate string.

Linux is pretty cautious about the case-sensitivity and hence, there’s a good chance that your search may fail. I will not get output for the find command if I use the string as ‘CONTEXT’ instead of ‘context’. Even if a single letter in the filename is in a different case than that of the original filename then, the search will fail.

But no need to worry about it. You can simply replace the -name option with the -iname. This allows you to search for the files irrespective of the cases in which their name is. Just make this simple change in your command and you will be fine.

find . -iname CONT\*

Output will be the same, even if I have used the string in the upper case.

./context_log.policy
./context.xml
./context_preview.c

Searching files in different directories

You can easily search for the files in any directory on the Linux system irrespective of the directory you are currently working in.

find [directory_path] -iname [specific_filename]

Example:

find /home/gaurav/tomcat -iname ath.html

Here, I have searched for a specific file ‘ath.html’ and not for the similar files to this string. So the output will be only the specific file as mentioned above.

/home/gaurav/tomcat/ath.html

Now, suppose we do not know the complete filename but only the string of this file name. Then we can search the files in the following way.

find /home/gaurav/tomcat -iname ath\*

This command will search for all the files which include the string ‘ath’ in it at the beginning. I have used the -iname option here, so I do not need to worry about the case sensitivity.

Output:

/home/gaurav/tomcat/ATHENIAN_ART.html
/home/gaurav/tomcat/ath_things.html
/home/gaurav/tomcat/ath.html
/home/gaurav/tomcat/ATHENIAN_ART.pdf
/home/gaurav/tomcat/ATHHHHhow.html

You can also use the command to search multiple files which end with a similar extension in their name.

Example:

find /home/gaurav/tomcat -iname "*.c"

The command will search for all the files in the specified directory which contains .c as a extension in their filename.

Output:

/home/gaurav/tomcat/stiil.c
/home/gaurav/tomcat/project/temp.c
/home/gaurav/tomcat/copy.c
/home/gaurav/tomcat/gy.c

We learnt about the find command to search for the files specified by the file name. Now, let us explore one more command which is faster than the conventional find command.


locate command overview

There is one more command to search for the files on your system which is faster than the find command. It’s the locate command. This command does not come pre-installed on the Linux distributions. You can use the following steps to download and install the locate command package onto your system.

For Ubuntu and Debian systems, use:

sudo apt update 
sudo apt install mlocate

For Cent Os and Fedora systems, use:

sudo yum install mlocate

The locate command searches for the file according to the given pattern in the input. locate uses the database file to search for the files, this database file is generated by the updatedb command.

sudo updatedb

The time taken to update this database file may vary from user to user depending on the files on your system.


Using the locate command

You can use the locate command in the following way. Be sure to update your database file using the command sudo updatedb.

Syntax:

locate [filename_or_part_of_filename]

This command will start the search from the root directory. It will return the list of all the files on the system that contains the file name or a part of the file name as specified in the command.

Example:

locate copy.c

Output:

/home/gaurav/Downloads/git-2.23.0/copy.c
/snap/core/9804/usr/lib/python3.5/__pycache__/copy.cpython-35.pyc
/snap/core/9993/usr/lib/python3.5/__pycache__/copy.cpython-35.pyc
/snap/core18/1880/usr/lib/python3.6/__pycache__/copy.cpython-36.pyc
/snap/core18/1885/usr/lib/python3.6/__pycache__/copy.cpython-36.pyc
/snap/core20/634/usr/lib/python3.8/__pycache__/copy.cpython-38.pyc
/usr/lib/python3.5/__pycache__/copy.cpython-35.pyc
/usr/lib/python3.6/__pycache__/copy.cpython-36.pyc
/usr/share/icons/MacBuntu-OS/apps/128/copy.com.png

The highlighted line shows that the exact file ‘copy.c’ is found. Along with this result, certain other files are also displayed which contains ‘copy.c’ as a part of their filename.

In order to avoid the clutter of other unwanted files and only find the desired file, you can use the locate command in the following way.

locate -b '\filename'

Example:

locate -b '\copy.c'

Output:

/home/gaurav/Downloads/git-2.23.0/copy.c

The specific file mentioned in the search criteria is listed along with the path of the directory where it is located.

Using the locate command may seem a little tricky, but is a faster method to search for files when you do not know the exact location of that file. Retrieval of the files becomes super fast when you have updated the database file which the locate command uses.


Conclusion

In this short tutorial, we learnt about two important commands, find and locate. Now you can easily search for files on your system without getting lost in the stack of files. Using these commands will surely prove time-saving and efficient for your task for searching files.