One of the most annoying things according to me while managing servers is trying to search for a file in Linux servers. At one point or another, we are required to find a file’s location in a Linux server where you need to make changes or have to locate a file with logs, backup, etc.

In this tutorial, we will learn how to search a file in Linux, using the following two commands,

1- Find command

2- Locate command

Let’s discuss these two commands find / locate a file in Linux, one by one.

Recommended Read: Essential Linux Commands You Should Know

Also Read: Importance TERRAFORM commands that we should know


1- Using the ‘find command’ to locate a file in Linux

Find is one the most widely used and powerful command Linux. Not only can search for files in Linux but can also perform follow-up actions like removing them, renaming them, etc, once the find command has located the intended files. 

To use the find command to locate a file in Linux, use the following command as a reference,

# find /root -name ‘*.txt’

This will find all the files end with ‘.txt’ in the folder ‘/root’ folder. 

Find command is so powerful utility that can locate files based on their sizes or permissions or owner or hidden files or based on the time they were accessed, modified, or created. We have created a detailed tutorial regarding how to use the FIND command in Linux.


2- Using the ‘locate command’ to find a file in Linux

Another command to search for a file in Linux is the ‘locate command’. Not all Linux distributions have the ‘locate command’ pre-installed but we can install it with a single line command. 

To install the ‘locate command in CentOS or other RHEL based Linux distros, the command is,

# yum install mlocate

To install it on all the Debian-based Linux distributions, the command is,

# sudo apt install locate

Once the package for the ‘locate command’ has been installed, we need to update its database with the following command,

# updatedb

This command creates or updates a database with the location of all files or folders on your Linux system & when we search for a file with the ‘locate command’, then it will search the db & not the Linux system for the required file’s location. Hence, it is much faster than the ‘find command’.

After this operation completes, then we can start to find files in your Linux system, like this, 

# locate shusain.txt

Locate command needs to have the exact name of the file that you are trying to find. 

Note: One other thing to consider while searching for a file with the ‘locate command’ is that file is searched against the db for locate command. So if you are trying to locate recently created files, then you might not find those. If that is the case, you need to update the db again before searching for the file. Rebuild the DB with,

# updatedb

That’s it guys, this was our tutorial on how to search a file in Linux using the ‘find’ & ‘locate command. Please send in any questions or queries using the comment below.

We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,

DigitalOcean - 100$ free credit & Linode - 100$ free credit

Check some Exclusive Deals, HERE.

Also, check out DevOps Book You should read section.