AMP AMP

Locate command in Linux with Examples

Locate command

Locate command is normally used to find the files location very quickly by name.
Though it works fast it can’ t replace the ' find' command since it comes with some limitations.

Syntax

locate < options> < filename>

Working process

It works fast because it won’ t search the files on disk. It simply searches the path in the databases. Here, the database is a file which contains information about the files and their path on the system. The database of the locate command is located in:
/var/lib/mlocate/mlocate.db

The utility called ' updatedb' which is used to update this db. When running this, it will scan the entire system and update the mlocate.db database file. Hence, it can be the negative that it should be run by manually in the specified time interval.

To Locate a file

It will search and shows the path of the httpd.conf where it placed with exact location.

Example

[user1@linuxhelp Desktop]$ locate httpd.conf 
/etc/httpd.conf/httpd.conf

To Locate with count option

Using &ndash c option get the number of count matching entry about httpd.conf

Example

[user1@linuxhelp Desktop]$ locate &ndash c httpd.conf 
1

To update Locate-Database

Example

[user1@linuxhelp dir1]# touch file.txt FILE.txt
[user1@linuxhelp ~]# locate file.txt 
[empty]

So, it won’ t display the location since the DB is not updated.


Example

[user1@linuxhelp dir1]# updatedb
[user1@linuxhelp dir1]# cd
[user1@linuxhelp ~]# locate file.txt 
/home/user1/Desktop/dir1/file.txt
[user1@linuxhelp ~]# locate FILE.txt 
/home/user1/Desktop/dir1/FILE.txt

Now, after updating the locate db the new files can be viewed.

To Locate with case sensitive

Using locate with -i option will ignore case, and look for both lowercase and uppercase file.

Example

[user1@linuxhelp ~]$ locate -i file.txt 
/home/user1/Desktop/dir1/FILE.txt
/home/user1/Desktop/dir1/file.txt

Search limit

If you want to display only certain number of records, use locate -l option and specify how many records you want to see in the locate command output.

Example

[user1@linuxhelp ~]$ locate -l 5 passwd 
/etc/passwd
/etc/passwd~
/etc/passwd.OLD
.
.
.
/usr/bin/htpasswd
/usr/bin/kpasswd

To Locate-Database location

The default location of the locate database is /var/lib/mlocate/mlocate.db. Here only it will be updating the files which we made on every run. This location can also be changed to our desired path using -d option. But if the specified location is empty or does not exist then it will automatically detect the default one.

Example

[user1@linuxhelp ~]# locate -d 
/home/user1/Documents/locate-db

Now, the given location is considered as located database.

File existence

If some file is deleted and we are looking for that file with locate command, it will still display the file because it is not yet updated in that DB. So it leads to the wrong result. Once db is updated then it won’ t show the file.

And there is a ' -e' option to check if the file exist not only in the locate DB also in the physical machine too.

Example

[user1@linuxhelp ~]# rm /home/user1/Desktop/dir1/file.txt
[user1@linuxhelp ~]# locate file.txt 
/home/user1/Desktop/dir1/file.txt
[user1@linuxhelp ~]# locate -e file.txt 

Since -e option is used it checked physically and it returns the blank (i.e) no such file in the machine, before the db is updated. But that file still exist in the locate db.

FAQ
Q
Whether the "locate" command is default package in Linux or not?
A
No. The "locate" command is not a default package in Linux. If you want to use the "locate" functionality, you should install it manually as per your linux distro.
Q
How to print a number of found entries while executing the "locate" command?
A
You can use the option of "-c" with "locate" command to print the number of found entries while executing the "locate" command. For ex: "locate -c file"
Q
How to display the information about the locate DB using the "locate" command?
A
You can use the option of "-S" with "locate" command to display the information about the locate DB. For Syntax: "locate -S"
Q
How to set a limit for search queries to a Specific Number of any file?
A
You can use the option of "-n" with "locate" command to set a limit for search queries to a Specific Number of any file. For Ex: "locate "*.html" -n 20"
Q
Can I execute the "locate" command to find any file (or) directory anywhere in the Linux system?
A
You can execute the "locate" command to find any file (or) directory anywhere in the Linux system. For Ex: "locate file1.txt"