How to search for files on Linux

Using the command line on a Linux machine can be very inconvenient, especially for beginners. Without a GUI, it’s very difficult to navigate directories and find the files you want. in this tutorial, we’ll show you how to find a specific file if you’re using Linux.

The first thing you need to do is connect to Linux via SSH. There are two common ways to find a file on Linux. One way is to use the command findand another way is to use the command locate… Let’s start with the first one.

Find command

Find command in Linux allows you to search the directory tree using different search criteria such as name, type, size, etc. Basic syntax:

find path expression search-term

Here’s a quick example of how to use the find command on Linux to find a specific file by its name:

find -name test.file

The command will search the entire directory tree for the file named test.fileΒ and will provide you with the location of it. You can try using a filename that actually exists on your Linux VPS.

Sometimes it can take a few minutes for the search command to search the entire directory tree, especially if you have many files and directories on your system. To save a significant amount of time, you can specify the directory to search. For example, if you know that test.fileΒ somewhere in the directory /var, there is no need to look for other directories. So you can use below command:

find /var -name test.file

There is also the ability to search for a file by time, size, type, access rights, etc. For more information on these parameters, you can check Linux find command on the man page.

man find

Locate command

In order to be able to use the command in Linux locate, you need to install it first.

If you are using Ubuntu, run the following commands to install locate:

apt-get update
apt-get install mlocate

If you are using CentOS, run the following command to install locate:

yum install mlocate

Find a quicker option to locate the file as it searches the database for files. Run the following command to update the search database:

updatedb

To find files with location, use the following syntax:

locate test.file

Just like with the find command, there are many options for filtering the search output. To find out more about this you can check Linux find command on the man page.

man locate

hjjk

Sidebar