- Quickly Search for Files in Linux With Locate Command
- Using locate command in Linux
- Using updatedb to create your own index for locate command
- Is locate better than the find command?
- locate
- Installation
- Usage
- See also
- 10 Useful ‘locate’ Command Practical Examples for Linux Newbies
- 1. Using locate Command
- 2. Limit Search Queries to a Specific Number
- 3. Display The Number of Matching Entries
- 4. Ignore Case Sensitive Locate Outputs
- 5. Refresh mlocate Database
- 6. Display Only Files Present in Your System
- 7. Separate Output Entries Without New Line
- 8. Review Your Locate Database
- 9. Suppress Error Messages in Locate
- 10. Choose a Different mlocate Location
Quickly Search for Files in Linux With Locate Command
The locate command allows you to preform a super quick search for files. In this tutorial, you’ll learn how locate command works and how to use it.
As the name suggests, the locate command in Linux is used for locating (searching) a file. It has the following syntax:
While it is real simple to use the locate command, it comes with lots of ifs and buts. For starter, if it doesn’t find a file, it doesn’t necessarily mean that the file does not exist on the system.
Don’t be confused just now. I’ll explain how the locate command works and how you can use it for finding files.
Using locate command in Linux
The locate command works on an index i.e., a database of file locations. When you use the command, it refers to this index instead of searching the entire filesystem. This is the reason why locate is super quick at finding files in Linux.
Some Linux distributions automatically index the entire filesystem on a regular basis. You can also manually build this index (I’ll show it to you later in this article).
Using locate command is dead simple. You just have to specify the file name:
The result will show all the places where the file you specified was found.
[email protected]:~$ locate desktop.iso /home/abhishek/desktop.iso
The best part is that you don’t need to be in the certain directory to find the file. Remember, locate command doesn’t search the filesystem but the index. This is why you don’t need to specify a path or directory while looking for files.
You can also perform a case-insensitive search by using the -i option:
[email protected]:~$ locate -i desktop.iso /home/abhishek/desktop.iso /home/abhishek/Downloads/Desktop.iso
Keep in mind that the result may show all the files that have the search term in their name.
[email protected]:~$ locate -i desktop /home/abhishek/eoan-desktop-amd64.iso /home/abhishek/Bionic-Desktop-amd64.iso /home/abhishek/desktop.iso /home/abhishek/Downloads/Desktop.iso
You may also use regex with locate:
locate -r regex_expression
Using updatedb to create your own index for locate command
This is just for informational purposes and I don’t think you would need to use it.
You can manually rebuild the database for the entire system like this:
If you want to create an index of the present directory (and its subdirectories), you can use the updatedb command like this:
updatedb -l0 -U my_directory -o index_file
Once you have created this index file, you can ask locate command to explicitly use this index:
locate -d index_file file_name
Is locate better than the find command?
If you want to search for files in a directory structure that doesn’t change much, locate command is a good option. It is super quick as well.
But the find command gives you a lot of options, especially for performing complex search options.
Both commands have their usage. In my opinion, if you are feeling lazy and want a quick result, use locate. If the result is not of your liking, switch to the find command.
locate
locate is a common Unix tool for quickly finding files by name. It offers speed improvements over the find tool by searching a pre-constructed database file, rather than the filesystem directly. The downside of this approach is that changes made since the construction of the database file cannot be detected by locate . This problem can be minimised by scheduled database updates.
mlocate (Merging Locate) is a more secure version of the locate utility, that only shows files accessible to the user.
plocate (Posting Locate) is a locate based on posting lists, consuming mlocate’s database ahead-of-time and making a much faster (and smaller) index out of it.
Installation
While the GNU findutils also include a locate implementation, Arch’s findutils package does not.
Usage
Before locate(1) can be used, the database will need to be created, this is done with the updatedb(8) command, which (as the name suggests) updates the database.
mlocate contains an updatedb.timer unit, and plocate contains a plocate-updatedb.timer unit. These units invoke a database update each day. mlocate enables its timer upon installation. start it manually if you want to use it before reboot. You can also manually run updatedb as root at any time.
To save time, updatedb can be (and by default is) configured to ignore certain filesystems and paths by editing /etc/updatedb.conf . updatedb.conf(5) describes the semantics of this file. It is worth noting that among the paths ignored in the default configuration ( PRUNEPATHS ) are /media and /mnt , so locate may not discover files on external devices.
See also
10 Useful ‘locate’ Command Practical Examples for Linux Newbies
One of most obnoxious experiences that most new users of the Linux platform usually face is the inability to find the simplest and yet more effective means of looking up files on their system.
Linux, like almost any other operating system, utilizes several mechanisms to answer search queries for users. Two of the most popular file searching utilities accessible to users are called find and locate.
Now, it is important to note that both search processes work extremely well but nonetheless, the center of this article will be more on the locate utility, which is the more convenient of the two as it uses more efficient ways to quickly process queries inputted by the users.
The locate utility works better and faster than it’s find counterpart because instead of searching the file system when a file search is initiated – Something find does – locate would look through a database. This database contains bits and parts of files and their corresponding paths on your system.
Here are ten simple locate commands to set you up in becoming more productive with your Linux machine.
1. Using locate Command
Firing locate command to look for a file is pretty easy and straightforward. All you need to do is type:
$ locate LAMP-Setup.odt /home/tecmint/LAMP-Setup.odt /home/tecmint/TecMint.com/LAMP-Setup.odt
2. Limit Search Queries to a Specific Number
You can limit your search returns to a required number to avoid redundancy with your search results using the -n command.
For example, if you want just 20 results from your queries, you can type the following command:
$ locate "*.html" -n 20 /home/tecmint/.config/google-chrome/Default/Extensions/aapocclcgogkmnckokdopfmhonfmgoek/0.9_0/main.html /home/tecmint/.config/google-chrome/Default/Extensions/aohghmighlieiainnegkcijnfilokake/0.9_0/main.html /home/tecmint/.config/google-chrome/Default/Extensions/felcaaldnbdncclmgdcncolpebgiejap/1.1_0/main.html /home/tecmint/.config/google-chrome/Default/Extensions/kbfnbcaeplbcioakkpcpgfkobkghlhen/14.752.848_0/forge.html /home/tecmint/.config/google-chrome/Default/Extensions/kbfnbcaeplbcioakkpcpgfkobkghlhen/14.752.848_0/src/popup.html /home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/additional-feature.html /home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/background.html /home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/edit.html /home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/help.html /home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/options.html /home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/popup.html /home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/purchase.html /home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/upload.html /home/tecmint/.config/google-chrome/Default/Extensions/nlipoenfbbikpbjkfpfillcgkoblgpmj/3.9.16_0/oauth2/oauth2.html /home/tecmint/.config/google-chrome/Default/Extensions/nmmhkkegccagdldgiimedpiccmgmieda/1.0.0.2_0/html/craw_window.html /home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_route_details.html /home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/feedback.html /home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_setup/devices.html /home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_setup/index.html /home/tecmint/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/5516.1005.0.3_0/cast_setup/offers.html
The results will show the first 20 files that end with .html .
3. Display The Number of Matching Entries
If you want to display the count of all matching entries of file “tecmint“, use the locate -c command.
$ locate -c [tecmint]* 1550
4. Ignore Case Sensitive Locate Outputs
By default, locate is configured to process queries in a case sensitive manner meaning TEXT.TXT will point you to a different result than text.txt .
To have locate command ignore case sensitivity and show results for both uppercase and lowercase queries, input commands with the -i option.
$ locate -i *text.txt* /home/tecmint/TEXT.txt /home/tecmint/text.txt
5. Refresh mlocate Database
Since locate command relies on a database called mlocate. The said database needs to be updated regularly for the command utility to work
efficiently.
To update the mlocate database, you use a utility called updatedb. It should be noted that you will need superuser privileges for this to work properly, is it needs to be executed as root or sudo privileges.
6. Display Only Files Present in Your System
When you have an updated mlocate database**, locate command still produces results of files whose physical copies are deleted from your system.
To avoid seeing results of files not present in your machine at the time of punching in the command, you will need to use the locate-e command. The process searches your system to verify the existence of the file you’re looking for even if it is still present in your mlocate.db.
$ locate -i -e *text.txt* /home/tecmint/text.txt
7. Separate Output Entries Without New Line
locate command’s default separator is the newline (\\n) character. But if you prefer to use a different separator like the ASCII NUL, you can do so using the -0 command line option.
$ locate -i -0 *text.txt* /home/tecmint/TEXT.txt/home/tecmint/text.txt
8. Review Your Locate Database
If you’re in doubt as to the current status of your mlocate.db, you can easily view the locate database statistics by using the -S command.
$ locate -S Database /var/lib/mlocate/mlocate.db: 32,246 directories 4,18,850 files 2,92,36,692 bytes in file names 1,13,64,319 bytes used to store database
9. Suppress Error Messages in Locate
Constantly trying to access your locate database does sometimes yield unnecessary error messages stating that you do not have the required privileges to have root access to the mlocate.db, because you’re only a normal user and not the required Superuser.
To completely do away with these message, use the -q command.
10. Choose a Different mlocate Location
If you’re inputting queries looking for results not present in the default mlocate database and want answers from a different mlocate.db located somewhere else in your system, you can point the locate command to a different mlocate database at a different part of your system with the -d command.
locate command might seem like one of those utilities that does everything you asked it to do without much of a hustle but in truth, in order for the process to keep its efficiency, the mlocate.db needs to be fed with information every now and then. Failure to do so might render the program a bit useless.