A modern search tool
Command Line – plocate
As the latest successor to locate, plocate produces some of the quickest search results possible on any system.
Real estate agents sometimes say that the key to success is location, location, location. This saying might almost summarize the history of the locate
command [1] and its various successors, particularly mlocate
[2] and plocate
[3]. As a replacement for find
, all three commands use a database solution to reduce search time. While all three share many of the same options, plocate
is widely considered the most efficient choice.
Slightly different versions of locate
are available in the BSD and GNU findutils, but you can also find locate
, mlocate
, and plocate
as separate commands. Because the number of choices can be confusing, a history seems in order. First released in 1982, locate
uses a database that can be read by any user. If regular expressions are not used, it displays every instance of the string entered on the system, which is inconvenient if the string is common. However, locate
's most serious limitation is that the database has to be updated manually. Largely because of these problems – especially the need for manual updates – locate
was succeeded briefly by slocate
(secure locate) [4] until replaced in 2006 by mlocate
(merging locate). Both slocate
and mlocate
are an improvement over locate
in that they contain the utility updatedb
[5] to update databases, speeding up the process by only searching for files and directories where the ctime
has changed. Also, both slocate
and mlocate
show only the files that the current user has access to, thereby improving security, and they allow regular expressions to be used without a specific option. Written by Miloslav Trmac while he participated in the Google Summer of Code, mlocate
became the preferred version until 2020, although it seems to have gone through periods of being unmaintained.
Named for the posting lists that inspired it, plocate
was written to be a drop-in replacement for mlocate
. While it can still use updatedb
to create its database, plocate
can also use the plocate-build
utility [6] to create an index when a root user is logged in. Unlike mlocate
, when multiple strings are searched, plocate
returns only the files that match all the search strings, rather than any file that matches even one string. Another difference from mlocate
: plocate
is compatible with systemd and SELinux. Instead of scanning every entry one at a time, plocate
scans trigrams (i.e., combinations of three bytes at a time) to increase search speeds. Although specifically designed for solid state drives (SSDs), on older hard drives, plocate
can gain further speed by using the io_uring
Asynchronous I/O (AIO) framework introduced in the Linux 5.1 kernel in March 2019 [7]. As its main limitation, plocate
's benefits may be lost when searching for strings shorter than three bytes, for non-UTF-8 file names, or for regular expressions with numerous hits. Usually, its enhancements mean that plocate
can find two files out of 27 million in .008 milliseconds, while mlocate
takes 20.118 milliseconds for the same operation [8]. Even though locate
and mlocate
are still available, in most circumstances, plocate
should be the preferred variant.
Setting Up plocate
You can find plocate
in the official repositories of Arch Linux, Manjaro, Debian (Buster backports and Bullseye releases), and Ubuntu. On other distributions, you can build plocate
with zstd and libtomic
using a C++ compiler. Whichever way you install it, before you use plocate
, you must run updatedb
to create at least one database at /var/lib/mlocate/mlocate.db
(Figure 1). This process may take a while to run the first time if the system is not on an SSD drive and does not run at least a Linux 5.1 kernel. Otherwise, on a 2TB system, the database is created in a matter of a few seconds. To specify a non-default location for the database, use: --database-root FILE
. You can add --database-root FILE
(-U FILE
) to specify a non-default location for the database, and --require-visibility 0
(-l 0
) to make the entire content of the database visible to ordinary users. Adding --verbose
(-v
) will display the files and directories being added to the database onscreen, which has the advantage of letting you know when the database creation is complete. Alternatively, you can create the database using plocate-build
, even creating a plain text database if you choose. Either way, you can check that plocate
is ready to use by doing a simple search (Figure 2).
Of note, mlocate
includes a few options that plocate
lacks. For example, mlocate
includes --statistics
(-S
) as well as --stdio
(-s
), a C function for interacting with different physical devices, for compatibility with the BSD and GNU versions of locate
. What remains in plocate
are a dozen basic functions, which are more than enough for almost all operations.
On networks, plocate
can be modified by adding multiple databases with --database PATH
(-d PATH
). Multiple databases can be specified one per option or in a single option with the names of databases separated by a colon (:). Note that versions of Ubuntu before 21.04 may not have these options and may not mention them in their man pages. Two other options signal that a search string should be treated as containing regular expressions: --regexp
(-r
) signals the presence of standard regular expressions and --regex
signals that extended regular expressions are used. Because regular expressions can return a large number of results, these options can noticeably slow a search. Without one of these options, common symbols in regular expressions such as *, ?, or ! are treated as regular text.
Other options modify a search's results. With --ignore-case
(-i
), plocate
's default case sensitivity is overwritten, so that lowercase and uppercase letters are treated as the same, except in the case of some Unicode case rules, such as a German ß being the same as ss. Moreover, like regular expressions, ignoring letter case can slow the search by producing a larger number of results, but search speed can be increased. For instance, on one hand --limit NUMBER
(-l NUMBER
) stops a search after the designated number of hits, and --count
(-c
) merely shows the number of instances. On the other hand, --basename
(-b
), which searches only for file names without their extensions, increases search speed only minimally at best by omitting directories from the search.
A Command for Modern Times
With the recent release of plocate
, the locate
family of commands seems set for the next few years. While plocate
has a few limitations, it uses the latest technology and is adopted to modern computing practices. For example, with the current hardware speeds, it no longer makes sense to set whether to follow symbolic links – following them by default can be done with no noticeable effects.
Even more important, the rise of Linux on the desktop has increased the demand for simplicity. In the past, the find
command, with its arcane distinctions, was sufficient because users could be assumed to have the expertise and the patience to wade through the obscurities of its man page. Today, though, with lessveteran users, simplicity and efficiency are expected as the norms. In the case of system searches, these expectations have resulted in plocate
, one of the increasing number of rewrites of classic Linux commands that users have lived with for so long.
Infos
- locate: https://www.man7.org/linux/man-pages/man1/locate.1.html
- mlocate: https://www.unix.com/man-page/Linux/1/mlocate/
- plocate: https://www.mankier.com/1/plocate
- slocate: https://linux.die.net/man/1/slocate
- updatedb: https://www.unix.com/man-page/linux/8/updatedb/
- plocate-build: https://plocate.sesse.net/plocate-build.8.html
- io-uring AIO framework: https://blogs.oracle.com/linux/post/an-introduction-to-the-io-uring-asynchronous-io-framework
- plocate's speed: https://www.linuxuprising.com/2021/09/plocate-is-much-faster-locate-drop-in.html
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.