The fastest way to find files by filename — mlocate (locate commands)
One of the best way for quickly finding files by filename is the locate
command. The locate
command reads one or more databases prepared by updatedb
and writes file names matching at least one of the patterns to standard output, one per line immediately.
Using locate
& updatedb
to search file
Finding files by filename (and path)
Just use simple syntax:
locate {file-name-or-path}
If the command is not found, the mlocate
package is probably not installed on your system. You need to install it first (see intruction below).
The result will appear immediately (without delay). You can further use grep
command to filter out some unneeded results. If no results found, your DB is probably too old. You would like to update it.
Update the search database
Note: You don’t need to execute this command every time you want to search for file. Just run it something like every 1 month or when you cannot find something.
sudo updatedb
More advanced syntax
Limit the number of results to 15:
locate -n 15 ghost
Using wildcast:
locate -n 20 "summer*.html"
Ignoring case:
locate -n 20 -i "sUMmer*.Html"
Even with regular expression (most advanced):
Locate all file in my home folder with the text ssh in file path:
locate -r "^/home/thuc/.*ssh.*$"
How to install mlocate
package
Ubuntu
Step 1. Install mlocate
package
sudo apt-get update
sudo apt-get install mlocate
Step 2. Update the search database
sudo updatedb
CentOS
Step 1. Install mlocate
package
As a matter of best practice we’ll update our packages:
sudo yum -y update
Then let’s install mlocate and any required packages:
sudo yum -y install mlocate
Step 2. Update the search database
The following command will update the search databases:
sudo updatedb
Happy searching!