About Locate Command

We typically use find command, when we need to search for some files. However, locate can search for files very quickly and it is the simplest way to search files and directories on a Linux and other Unix-like operating systems. Find command is very slow when compared to locate command.

The basic syntax for locate is:

# locate [options] pattern

When we are using locate command without any options, it displays every absolute pathname for which the user has access permission. Check the following example, it will display all the absolute paths of the file and the directories which the user had access permission:

# locate example direc1

 

How to Install Locate

If we want to install the locate command on CentOS, we need to install the package mlocate:

# yum install mlocate

Next, we need to update the search databases. We cannot locate anything until we have updated the database for locate. So, run the following command to update the search databases.

# updatedb

mlocate installs a cron job which will update the database daily at

/etc/cron.daily/mlocate.cron

The locate utility works better and faster than the find utility because it searches for the file paths in a database rather than searching the file system when a file search is initiated. The locate database contains information about the files and their path on your system. The locate database file is located at:  /var/lib/mlocate/mlocate.db

 

Examples of Locate command

1) It is very easy to locate a file using Locate command. The following command will display the file name with Example.txt.

#locate Example.txt

 

2) The following command will display the count of all matching entries of a file. Here it will display the count of all matching entries of the file test.

# locate -c [test]*

 

3) The locate -e command will only display the files present in your system. It will not show the results of files which are not present in our system. This process will search your entire machine to check the existence of the file which you are searching for even if it still exists in your mlocate.db.

# locate -i -e *example.doc*

 

4) The mlocate database should be updated regularly for the work efficiency of the command. Because locate command is truly depended on the database mlocate. In order to update the mlocate database, we use the following utility.

# updatedb

 

5) The following command is used to review locate database mlocate.db. Here, we can easily view the locate database statistics by using the -S command.

# locate -S

 

6) Now we can discuss how to choose a different mlocate location. If we want the answers from a different mlocate.db which is located somewhere else in your system, then we can point the locate command to a different mlocate database with the -d command.

# locate -d <new db path> <filename>

 

7) We can limit our search queries to a specific number by using the -n command. By limiting the search queries will avoid redundancy with our search results. For example, if we want to display 10 results, then use the following command:

# locate “*.txt” -n 10

The results of the above command will show the first 10 files that have the extension .txt.

8) If we are constantly trying to access the locate database, then it will prompt an unnecessary error message like “we do not have the required privileges to have root access to the mlocate.db, because we are only a normal user and not the required Superuser”.

In order to suppress this error messages in locate, we use the -q command.

# locate “\*.dat” -q*

 

9) Locate is configured to process search queries in a case-sensitive manner by default which means TEXT.TXT will point you to a different result than text.txt.

To ignore these case sensitivities and show results for both uppercase and lowercase search queries, we use the locate command with the -i option.

# locate -i *text.txt*

 

If you need any further help please do reach our support department.

Was this answer helpful? 0 Users Found This Useful (0 Votes)