LinuxCommandLibrary

slocate

Locate files in a secure database

TLDR

Enable quiet mode to suppress error messages

$ slocate -q
copy

Limit the number of results shown
$ slocate -n [number]
copy

Build an slocate database starting at path /
$ slocate -u
copy

Build an slocate database starting at a given directory
$ slocate -U [path/to/directory]
copy

Update an slocate database using the default /etc/updatedb.conf configuration
$ slocate -c
copy

Set the security level of slocate, with 0 being disabled, and 1 being secure
$ slocate -l [0|1]
copy

Specify the database that slocate should search in
$ slocate [[-d|--database]] [path/to/directory]
copy

Search the slocate database using a specific regex string
$ slocate [[-r|--regexp]] [regex]
copy

SYNOPSIS

slocate [OPTION]... PATTERN...

PARAMETERS

-b, --basename
    Match only the base name of pathnames. Do not match directories.

-c, --count
    Instead of printing filenames, print only the number of matching entries.

-d PATH, --database=PATH
    Specify a different database to search. Multiple databases can be specified, separated by :. If none is specified, slocate uses the default database (e.g., /var/lib/mlocate/mlocate.db).

-e, --existing
    Only print entries that existed at the time slocate was run. This performs a file system check on the results from the database, which can slow down the command.

-h, --help
    Display a help message and exit.

-i, --ignore-case
    Ignore case distinctions in patterns.

-q, --quiet
    Do not print error messages about inaccessible files or database issues.

-r, --regex
    Interpret patterns as extended regular expressions (ERE).

-V, --version
    Display version information and exit.

-w, --wholename
    Match the whole pathname, not just the base name. This is the default behavior.

DESCRIPTION

slocate (secure locate) is a command-line utility used to quickly find files on the filesystem by searching a pre-built database. Unlike the original locate command, slocate was designed with security in mind, ensuring that search results only include files that the executing user has permission to see. This prevents information leakage, as users cannot discover the existence of files they are not authorized to access.

The command does not scan the filesystem in real-time. Instead, it queries a database (typically updated daily by the updatedb command) containing a snapshot of the filesystem's contents. This approach makes slocate exceptionally fast for file searches, as it avoids the overhead of traversing the entire directory tree. However, a trade-off is that recently created or deleted files will not appear in search results until the database has been updated.

CAVEATS

The primary limitation of slocate is its reliance on a pre-built database. This means that search results are not real-time; any files created, deleted, or moved since the last updatedb run will not be accurately reflected. Additionally, while slocate enforces user permissions during queries, the updatedb utility (which builds the database) typically runs as root and indexes all files, including those inaccessible to regular users. Modern Linux distributions often use mlocate, which is a more advanced replacement for both the original locate and slocate, incorporating security features and improved performance.

DATABASE LOCATION

The slocate command typically queries a database file, most commonly found at /var/lib/mlocate/mlocate.db or /var/lib/locate/locate.db, depending on the system's specific setup.

DATABASE UPDATE

The database used by slocate is generated and updated by the updatedb command. This command is usually run automatically at regular intervals (e.g., daily) via a cron job, often as a root user, to ensure the database remains reasonably current. Users typically do not run updatedb manually.

HISTORY

The locate utility has a long history in Unix-like systems, providing fast file searches using an indexed database. The original locate program, however, presented a security concern: it would show the existence of files even if the user executing the command did not have permission to view or access them, potentially leaking sensitive information. To address this, slocate (secure locate) was developed, introducing access checks at query time to ensure only permissible files were displayed.

Over time, the need for a more robust and efficient solution led to the development of mlocate (merging locate), which effectively combined the speed of locate with the security features of slocate, alongside performance improvements for database updates and searches. As a result, mlocate has largely superseded slocate and is the standard locate implementation on most modern Linux distributions, often symlinked as locate.

SEE ALSO

locate(1), mlocate(1), updatedb(8), find(1), which(1), whereis(1)

Copied to clipboard