plocate Is A Much Faster locate (Drop-In Replacement For mlocate)

plocate find files command line

locate is a Unix tool used to find files by name on the filesystem, which uses a prebuilt database of generated files (created using updatedb). Using locate is faster than find, but it requires having the database updated to find newly added files.

locate was created in 1982, with the BSD and GNU Findutils versions deriving from the original implementation.

plocate is a newer (the first stable release was less than a year ago), much faster locate. It’s based on posting lists, giving much faster searches on a much smaller index. 

The command-line tool is a drop-in replacement for mlocate (Merging Locate; a restricted-access database, only showing filenames accessible to the user) in nearly all aspects, including reusing the mlocate database (plocate creates its own index using plocate-build which reads the database made by updatedb), and is fast on SSDs and HDDs alike.

plocate works by creating an inverted index over trigrams (combinations of three bytes) in the search strings, which allows it to rapidly narrow down the set of candidates to a very small list, instead of linearly scanning through every entry. It does nearly all I/O asynchronously using io_uring if available (Linux 5.1+), which reduces the impact of seek latency on systems without SSDs.

You might also like: How To Find Files Modified In The Last N Days Or Minutes Using find

To show how fast plocate is compared to mlocate, the plocate developer shows this benchmark on the tool’s homepage in which plocate is able to find 2 files out of 27 million in just a few milliseconds:

plocate performance

Also, like mlocate, plocate shows all the files matching the search query only if they are visible to the user running the command, skipping all restricted files.

How to install and use plocate

plocate is available in the official repositories of a few Linux distributions, including Arch Linux / Manjaro and Debian (Bullseye and newer, as well as the Buster backports) / Ubuntu (21.04 and newer). You can install it using:

  • Arch Linux / Manjaro:

sudo pacman -S plocate
  • Debian (Bullseye and newer, as well as the Buster backports) / Ubuntu (21.04 and newer):
plocate find files command linesudo apt install plocate

To build it on other Linux distributions you’ll need a C++17 compiler, Zstd (development headers) and Meson. In my test on Fedora I also had to install libatomic to get the program to build. Optionally, you’ll also want liburing and a kernel supporting io_uring (Linux 5.1 or newer) for best performance, especially if you’re not using a SSD. Another optional dependency is systemd, which can be used to run the built-in plocate database update timer.

To install the plocate build-dependencies on Fedora (including the optional liburing), use:

sudo dnf install libzstd-devel liburing-devel libatomic gcc-c++ meson

Or to build it on e.g. Ubuntu 20.04 (note that Ubuntu 20.04 doesn’t have liburing in its repositories so plocate will not use all of its power if you’re using a regular HDD), you’ll need these dependencies:

sudo apt install libzstd-dev meson build-essential

Once you have the build-dependencies installed, download and extract plocate, and use the terminal to navigate to its folder. Now you can proceed to build and install plocate on your system:

meson obj

cd obj

ninja

sudo groupadd --system plocate

sudo ninja install

sudo systemctl enable plocate-updatedb.timer

Now you can start using plocate. Before using it for the first time, create its database (file index) using the following command:

sudo updatedb

The first time you run updatedb, it scans the entire filesystem, and it may take a while. Subsequent runs should be much faster.

Now use plocate like you would use locate. Search for a file using plocate:

locate <MY_FILE.EXT>

Replacing MY_FILE.EXT with the file you want to find.

For more options like ignore case, regex search, etc., see the tool’s help and man page.

You might like: How To Delete Files Older Or Newer Than N Days Using find (With Extra Examples)

Posted by WordPress Guru