CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / File & Directory Management / locate

locate

Find files instantly by name from an index

Beginnersearchindexupdatedb

locate answers 'where is the file called X?' in milliseconds by searching a database of every path on the system, rebuilt daily by updatedb. It's fast because it never touches the disk, and stale for the same reason: files created since the last update are invisible.

Syntaxlocate [OPTION]... PATTERN... sudo updatedb

Watch it run

You remember a config file's name but not where the package put it.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
locate sshd_config

The pattern matches anywhere in the full path, so you also get a directory and other files containing the text. The answer comes back in a few milliseconds.

Anatomy of a command

$
commandlocate

On Ubuntu 24.04 this is plocate, a fast replacement for mlocate with the same options.

How it works

locate never walks the filesystem. A separate program, updatedb, does that once a day (on Ubuntu 24.04 the plocate-updatedb.timer systemd unit; older systems used /etc/cron.daily). It crawls every directory, skipping what /etc/updatedb.conf prunes (/tmp, /proc, network filesystems like NFS), and writes all path names into a compressed index, /var/lib/plocate/plocate.db. plocate builds a trigram index, so a query reads only the few blocks that can contain the pattern instead of scanning every path. A plain pattern matches anywhere in the full path (as if wrapped in *...*); with several patterns plocate requires all of them to match. The database is readable only by the plocate group, and the locate binary is setgid plocate: it can read the whole index, but before printing a result it checks that you could reach that path yourself, so it doesn't leak names from directories you can't enter.

A daily job indexes every path; locate only reads that index, which makes it instant and possibly out of date.

daily runwalk all dirswrite indexquerylookupfiltersystemd timerdailyupdatedbplocateFilesystemlive treeNew fileafter the scanplocate.dbtrigram indexlocatesetgid plocateYoulocate app.yamlAccess checkcan you reach it?
1/7
Once a day, updatedb runs

plocate-updatedb.timer starts updatedb, usually in the night with a random delay, at idle I/O priority.

← → to step

Options you'll actually use

-iCase-insensitive match
-l N / -n NStop after N results
-cPrint only the number of matches
-bMatch the base name only (with '\name' for an exact name)
-r REGEX / --regexBasic / extended regular expression (slower)
-eOnly print files that still exist right now
-0NUL-separated output for xargs -0
-d DBPATHSearch a different database
sudo updatedbRebuild the index now

Common mistakes

Using locate in a script to check whether a file exists

The index can be up to a day old, so files created since then are missed and deleted ones still appear.

test -e /path/file, or find for searches
Concluding a file doesn't exist because locate found nothing

It may be newer than the index, or in a pruned path (/tmp, /media, NFS), or in a directory you can't access.

sudo updatedb && locate name, or sudo find / -xdev -name name
locate nginx.conf expecting exactly one config file

A plain pattern matches anywhere in the full path, so you also get nginx.conf.default, nginx.conf.dpkg-old and every path under a directory containing that text.

locate -b '\nginx.conf' for an exact base name; with wildcards (locate '*/nginx.conf') the glob must match the whole path

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Use locate to find where app.conf lives on the sandbox.

  2. 2

    Create ~/findme-test.txt, try to locate it, then make locate find it.

  3. 3

    Count how many paths on the system contain 'python3', case-insensitively.

Related commands

findSearch the filesystem by name, size, age, owner and morewhichFind which executable runs for a command namesystemctlStart, stop, enable and inspect systemd servicescrontabSchedule recurring jobs with cron
PreviousfindNexttree
Linux / File & Directory Management / locate

locate

Find files instantly by name from an index

Beginnersearchindexupdatedb

locate answers 'where is the file called X?' in milliseconds by searching a database of every path on the system, rebuilt daily by updatedb. It's fast because it never touches the disk, and stale for the same reason: files created since the last update are invisible.

Syntaxlocate [OPTION]... PATTERN... sudo updatedb

Watch it run

You remember a config file's name but not where the package put it.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
locate sshd_config

The pattern matches anywhere in the full path, so you also get a directory and other files containing the text. The answer comes back in a few milliseconds.

Anatomy of a command

$
commandlocate

On Ubuntu 24.04 this is plocate, a fast replacement for mlocate with the same options.

How it works

locate never walks the filesystem. A separate program, updatedb, does that once a day (on Ubuntu 24.04 the plocate-updatedb.timer systemd unit; older systems used /etc/cron.daily). It crawls every directory, skipping what /etc/updatedb.conf prunes (/tmp, /proc, network filesystems like NFS), and writes all path names into a compressed index, /var/lib/plocate/plocate.db. plocate builds a trigram index, so a query reads only the few blocks that can contain the pattern instead of scanning every path. A plain pattern matches anywhere in the full path (as if wrapped in *...*); with several patterns plocate requires all of them to match. The database is readable only by the plocate group, and the locate binary is setgid plocate: it can read the whole index, but before printing a result it checks that you could reach that path yourself, so it doesn't leak names from directories you can't enter.

A daily job indexes every path; locate only reads that index, which makes it instant and possibly out of date.

daily runwalk all dirswrite indexquerylookupfiltersystemd timerdailyupdatedbplocateFilesystemlive treeNew fileafter the scanplocate.dbtrigram indexlocatesetgid plocateYoulocate app.yamlAccess checkcan you reach it?
1/7
Once a day, updatedb runs

plocate-updatedb.timer starts updatedb, usually in the night with a random delay, at idle I/O priority.

← → to step

Options you'll actually use

-iCase-insensitive match
-l N / -n NStop after N results
-cPrint only the number of matches
-bMatch the base name only (with '\name' for an exact name)
-r REGEX / --regexBasic / extended regular expression (slower)
-eOnly print files that still exist right now
-0NUL-separated output for xargs -0
-d DBPATHSearch a different database
sudo updatedbRebuild the index now

Common mistakes

Using locate in a script to check whether a file exists

The index can be up to a day old, so files created since then are missed and deleted ones still appear.

test -e /path/file, or find for searches
Concluding a file doesn't exist because locate found nothing

It may be newer than the index, or in a pruned path (/tmp, /media, NFS), or in a directory you can't access.

sudo updatedb && locate name, or sudo find / -xdev -name name
locate nginx.conf expecting exactly one config file

A plain pattern matches anywhere in the full path, so you also get nginx.conf.default, nginx.conf.dpkg-old and every path under a directory containing that text.

locate -b '\nginx.conf' for an exact base name; with wildcards (locate '*/nginx.conf') the glob must match the whole path

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Use locate to find where app.conf lives on the sandbox.

  2. 2

    Create ~/findme-test.txt, try to locate it, then make locate find it.

  3. 3

    Count how many paths on the system contain 'python3', case-insensitively.

Related commands

findSearch the filesystem by name, size, age, owner and morewhichFind which executable runs for a command namesystemctlStart, stop, enable and inspect systemd servicescrontabSchedule recurring jobs with cron
PreviousfindNexttree