Cyberithub

15 Best Examples of locate command in Linux (RedHat/CentOS 7/8)

Advertisements

In this article, I will take you through 15 Best Examples of locate command in Linux (RedHat/CentOS 7/8). locate is a very useful command in Linux to search a file in almost no time. It is traditionally faster in comparison to other searching tools like find command in Linux due to its separate maintenance of a database to keep the records of all file paths and location.

Difference between locate and Find command

locate command maintains a Database to do all its queries whereas find command does not have this feature. There is a background daemon process which runs and populates the locate database so that it will query and get the location of files in almost no time whereas find command has to physically search in every location to find any files which makes it a slow process. Hence locate command is much faster than find command.

 

15 Best Examples of locate command in Linux (RedHat/CentOS 7/8) 1

locate command in Linux

Also Read: How to Install locate command in Linux (RedHat/CentOS 7/8) Using 5 Easy Steps

Example 1. Check locate command version

If you want to check the locate command version then you need to run locate -V command as shown below. As you can check from the output current locate command version is 0.26

[root@localhost ~]# locate -V
mlocate 0.26
Copyright (C) 2007 Red Hat, Inc. All rights reserved.
This software is distributed under the GPL v.2.

This program is provided with NO WARRANTY, to the extent permitted by law.

-V : Write information about the version and license of locate on standard output and exit successfully.

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to User.

Example 2. Search a File using locate command in Linux

If you want to search a file say file.txt then you need to use locate file.txt command as shown below. Please do not forget to run updatedb command if the file is created after last updatedb command run or else it won't detect the file. Also it is good idea to run updatedb command first before using locate command in Linux.

[root@localhost ~]# locate file.txt
/home/centos/file.txt
/root/file.txt

Example 3: Display total number of Matching Files

If you want to count the total number of occurrence of a file then you need to use below locate command in Linux. In below demonstration we are counting the total number of file.txt using locate -c file.txt command.

[root@localhost ~]# locate -c file.txt
2

-c : Instead of writing file names on standard output, write the number of matching entries only.

Example 4: Display total number of Specific Matching Pattern Files

If instead of giving complete file name if you use wild card character(*) to count the occurrence of all the files ending with .txt then you need to use below locate command in Linux.

[root@localhost ~]# locate -c *.txt
4

Example 5: Limit the Number of Search Output

If you want to limit the number of search output during from locate run then you can do it by using -n option and passing the number of total search as an argument to locate command as shown below.

[root@localhost ~]# locate *.txt -n 3
/home/centos/file.txt
/root/file.txt
/root/file1.txt

-n : Exit successfully after finding LIMIT entries.

Example 6: Ignore Case Sensitiveness

If you want to ignore the case sensitivity then you need to use -i option with locate command in Linux. As shown in the below demonstration, we are trying to search a file File.txt using locate -i File.txt command. It will ignore the Case sensitive F and search all the files matches with file.txt in the System.

[root@localhost ~]# locate -i File.txt
/home/centos/file.txt
/root/file.txt

-i : Ignore case distinctions when matching patterns.

Example 7: Show locate DB Stats using locate command in Linux

If you want to know more about mlocate database then you can check it through locate -S command as shown below. Using this command you can check database path, number of directories, number of files, total size of files and total size of the database as shown below.

[root@localhost ~]# locate -S
Database /var/lib/mlocate/mlocate.db:
                           5,253 directories
                           35,428 files
                           1,755,455 bytes in file names
                           818,049 bytes used to store database

-S : Write statistics about each read database to standard output instead of searching for files and exit successfully.

Example 8: Search File Using Regular Expressions

If you want to search any file using regular expressions then you can search it by using -r option with locate command in Linux as shown below. As shown in the below example, here we are trying to search all the matches with file keyword at the end($).

[root@localhost ~]# locate -r /file$
/home/centos/file
/usr/bin/file
/usr/share/file

-r : Search for a basic regexp.

Example 9: Display Output in One Line using locate command in Linux

If you want to display the output of searches in one line then you need to use -0 option with -i option as shown below. As you can see from below demonstration, output of locate -i -0 file.txt command is showing in one line.

[root@localhost ~]# locate -i -0 file.txt
/home/centos/file.txt/root/file.txt

Example 10: Update Database and Search File using locate command in Linux

After doing any changes in the System in the form of creation, deletion or updation of any files in the System you need to run updatedb command to update the changes in the mlocate database else system won't detect the changes while running locate command.

[root@localhost ~]# updatedb
[root@localhost ~]# locate file.txt
/home/centos/file.txt
/root/file.txt

Example 11: Find the location of deleted files using locate command in Linux

Sometimes you can exploit the locate database property to find the location of some deleted files. Let's say you have a file.txt which you deleted but forgot to run updatedb command to update the locate database about the deletion. Then later on if you want to check the Original location of file from where it got deleted then you can run same locate file.txt command and find the location.

This command might be helpful sometimes if you want to find the location of deleted files. In below example I have first run locate command to check the location of file.txt.

[root@localhost ~]# locate file.txt
/home/centos/file.txt
/root/file.txt

Then i removed the file.txt from /root path and did not ran updatedb command to update the database about the changes done.

[root@localhost ~]# rm /root/file.txt
rm: remove regular file ‘/root/file.txt’? y

When again run the same locate command you can see that location of file.txt is still showing under /root path.

[root@localhost ~]# locate file.txt
/home/centos/file.txt
/root/file.txt

Example 12: Check if file exists during locate command run

If you want to check a file which exists or not during locate command run then you need to check it through locate -e file.txt command where it will check for file.txt availability in the System.

[root@localhost ~]# locate -e file.txt
/root/file.txt

-e : Print only entries that refer to files existing at the time locate is run.

Example 13: Block search in some of the directories

If you want locate command to not to look into some directories while running a search then you can add the path of that directory under /etc/updatedb.conf and block the search as shown below. Here you need to add the directory path in PRUNEPATHS and then save and exit the file. Then run the updatedb command once to update the changes and we are done.

[root@localhost ~]# cat /etc/updatedb.conf

PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp /var/lib/ceph"

Example 14: Filter files and commands using locate command in Linux

If you want to find the location of which command and if you have some idea from which path you basically want the output then it will be easy to grep the which command path from its parent directory. In this example, we know that all system tools will be available under bin or sbin so we will search which command path under bin directory and will get our output.

[root@localhost ~]# locate bin | grep which
/usr/bin/which

NOTE:

Please note that in above example if you do not search it under bin path then direct search using locate command in Linux will give you a long list of all the path where which command will be available.

Example 15: Check other locate command options

If you want to check all the options that can be used be locate command then you can check it through --help as shown below.

[root@localhost ~]# locate --help
Usage: locate [OPTION]... [PATTERN]...
Search for entries in a mlocate database.

-A, --all only print entries that match all patterns
-b, --basename match only the base name of path names
-c, --count only print number of found entries
-d, --database DBPATH use DBPATH instead of default database (which is
/var/lib/mlocate/mlocate.db)
-e, --existing only print entries for currently existing files
-L, --follow follow trailing symbolic links when checking file
existence (default)
-h, --help print this help
-i, --ignore-case ignore case distinctions when matching patterns
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries
-m, --mmap ignored, for backward compatibility
-P, --nofollow, -H don't follow trailing symbolic links when checking file
existence
-0, --null separate entries with NUL on output
-S, --statistics don't search for entries, print statistics about each
used database
-q, --quiet report no error messages about reading databases
-r, --regexp REGEXP search for basic regexp REGEXP instead of patterns
--regex patterns are extended regexps
-s, --stdio ignored, for backward compatibility
-V, --version print version information using locate command in Linux
-w, --wholename match whole path name (default)

Report bugs to mitr@redhat.com.

 

 

Popular Recommendations:-

locate command in Linux

10 Best Examples of head command in Linux (RedHat/CentOS 7/8)

12 Best Examples of tail command in Linux for Beginners

How to Install netcat(nc) command on Linux (RedHat/CentOS 7/8) in 5 Easy Steps

12 Most Popular rm command in Linux with Examples

Create a Self Signed Certificate using OpenSSL

Leave a Comment