Use Locate Command to Quickly Find Files
When you want a simple file search by its name, locate is sufficient. Learn how to use locate command in Ubuntu.
Find is the go-to command for searching for files in the Linux terminal. It is extensive and you can use it to find files based on numerous criteria.
However, find is not the only tool for finding files in the terminal. There is a locate command as well.
But do you need another utility? The find command works just fine, right? Let me tell you why you may prefer locate over find at times.
Locate does not have as wide a range of options. Which makes it simpler to use. It utilizes its local database which makes it faster than find.
In other words, when you are just looking for a file by its name and you are not sure about the location, locate comes to the rescue.
Sounds interesting? Let's see about using locate on Ubuntu.
Install locate command on Ubuntu
The sad part is that locate utility is not a part of default packages in Ubuntu and needs manual installation by the given command:
sudo apt install locate
In Ubuntu 22.04, you can install it from plocate package (a newer, faster implementation of locate):
sudo apt install plocate
And then you'd need to update its local database otherwise, you'd get no response from any query.
sudo updatedb
Using locate command
First, let me introduce the simple syntax of locate command:
locate [options] [pattern]
Here,
[options]
are going to let you decide what kind of search you want to go with such as whether it is a database, want to go with whole name, and many others.
[pattern] allows you to search for specific patterns such as the only search for the .txt files and so on.
Now, let's jump to the examples.
Find files with specific names
This is the easiest way to find files if you're aware of the file name.
locate File_name
As you can see, it brought all the filenames starting with Helloworld
and if you're looking for a way to find files with exact names, you can skip to the later section.
Ignore case-sensitive filenames
To widen your search, you can use -i
option to ignore cases in file names.
locate -i File_name
Use regex
You can use regular expressions to find files with matching patterns in the name.
locate -r File_name
If you give it the file name instead of a pattern, it will show files with the exact file name.
Avoid showing deleted files in the result
As locate utilizes a local database, I often myself in situations where it brings deleted files in search results as given:
In that case, you can force locate utility to only search for those files that are currently available using -e
option.
locate -e File_name
You won't find Helloworld.txt
in the home directory when I used the -e
option in the 2nd command of the above snapshot.
Count the search results instead of showing files
If you're reading numerous amount of files, before attempting to search, having an idea of the exact number will always be beneficial.
Still confused about its use case? Let me show you why it is crucial.
I'd be printing the number of .txt
files available in my system using -c
option. Sounds ridiculous right?
locate -c .txt
2795 results. Enough to make anyone mad, right?
Find or locate?
That's entirely up to you. The locate is good at quickly finding files based on their name. The find command is good at locating files based on name, size, modification, type and whatnot.
Both commands have their usage. When you want a simple file search by its name, locate is sufficient. If you want something more than that, find is your ally.
Let me know if you have any questions.