Learn › Linux Foundations › Search
locate - a hands-on Linux lab on a real virtual machine.
Read a prebuilt file list in an instant with locate: match a pattern with a wildcard, match a bare word as a substring anywhere in the path, count with -c, ignore case with -i, and learn why a silent empty result means no match or a stale snapshot. All on the ~/archive fixture, right in the practice terminal.
Somewhere in your home directory sits a folder called archive: ~/archive, a mixed pile of reports, logs, images, config files, and scripts nested a few levels deep. You need one file out of it, and you know a piece of its name. You could walk the whole tree looking, but on a real machine with hundreds of thousands of files that walk takes time.
Here is the shortcut. A companion tool has already scanned this archive once and written every path it holds into a plain database, a saved list of names sitting in a file. There is a command built to read that list back in an instant, without touching the disk at all. It is called locate, and this whole lesson is about driving it well.
The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. locate comes from a package that is not part of a bare Linux install, so the practice terminals just replay its real output here. On the real machine at the end of the module, the mission installs it and builds the list for you.
The list for ~/archive has already been built into a private file, ~/archive.db. Because it is your own file rather than the system-wide one, you point locate at it every time with -d (short for database). The shape is always locate -d ~/archive.db followed by what you are looking for. Building that file is a different command's job, updatedb, which you meet in its own lesson; here the file already exists and you just read it.
Ask the list for something. Hand locate a pattern with a * in it and it returns every stored path that matches. The * is a wildcard: it stands for any run of characters, so '*.log' means "any path ending in .log." Wrap the pattern in single quotes so the shell hands the * to locate untouched, rather than trying to expand it itself.
locate prints its hits in the order they sit in the database, which is not alphabetical. To get a stable, readable list, pipe the output into sort. The | symbol, called a pipe, feeds one command's output straight into the next; sort puts the lines in order. Type this exact command and press Enter:
locate -d ~/archive.db '*.log' | sort
prompt: student@linuxcamp:~$ answer: locate -d ~/archive.db '*.log' | sort output: /home/student/archive/logs/access.log /home/student/archive/logs/auth.log /home/student/archive/logs/big.log /home/student/archive/logs/system.log hint: Type locate -d ~/archive.db, then the quoted pattern '*.log', then a space, a padded pipe, and sort
Four log files, instantly. locate never walked ~/archive; it read the saved list and printed the lines that matched '*.log'. The * matched everything before .log, so every path ending in .log came back, no matter how deep it lives. The | sort on the end took whatever order the database handed over and put it in alphabetical order, so this output is the same every time you run it.
Here is the move that makes locate fast to type. You do not always need a wildcard. Hand locate a plain word with no * and it treats it as a substring: it returns every path that contains that word anywhere, folder names included, not just at the end.
Ask for everything with reports somewhere in its path:
locate -d ~/archive.db reports | sort
prompt: student@linuxcamp:~$ answer: locate -d ~/archive.db reports | sort output: /home/student/archive/reports /home/student/archive/reports/2024 /home/student/archive/reports/2024/annual.txt /home/student/archive/reports/2024/q1-summary.txt /home/student/archive/reports/2024/q2-summary.txt /home/student/archive/reports/2025 /home/student/archive/reports/2025/data.csv /home/student/archive/reports/2025/metrics.csv /home/student/archive/reports/2025/q1-summary.txt hint: Type the bare word reports after the database, with no quotes and no star: locate -d ~/archive.db reports | sort
Nine lines, because the word reports sits in every one of those paths, the reports directory itself and both year folders included. That is the difference between a bare word and a pattern: a bare word matches a substring anywhere in the line, while '*.log' anchors to the end. Reach for a bare word when you remember part of a name but not exactly where it sits or how it ends.
One narrower substring proves the point. locate -d ~/archive.db annual returns just the single path /home/student/archive/reports/2024/annual.txt, because annual appears in only one line of the list.
Now a test of what you have learned. Your archive holds a hidden file, .secret.conf, whose name starts with a dot. A plain shell wildcard typed at the prompt, like *.conf, would skip it, because the shell hides dot-names by default. Before you run this, decide: will a locate pattern skip the hidden file the way the shell does, or show it?
Ask the list for every .conf file and find out:
locate -d ~/archive.db '*.conf' | sort
prompt: student@linuxcamp:~$ answer: locate -d ~/archive.db '*.conf' | sort output: /home/student/archive/config/.secret.conf /home/student/archive/config/app.conf /home/student/archive/config/logging.conf hint: Same shape as the log lookup, new ending: locate -d ~/archive.db '*.conf' then a padded pipe and sort
Three config files, and the first one is the hidden .secret.conf. The dot at the front of its name did nothing to hide it from locate. When the list was built, that path was recorded like any other, and locate matched your pattern against that recorded text rather than asking the shell to expand a wildcard against the live folder, so the dot is just another character to it. That is a real, practical reason to reach for locate: it surfaces dot-files that a bare *.conf at the prompt would silently pass over.
Sometimes you do not want the paths at all, just the number of them. The -c flag (short for count) prints how many matches there are instead of printing the matches themselves. Because it prints a single number, there is nothing to sort.
Count the .txt files the list knows about:
locate -d ~/archive.db -c '*.txt'
prompt: student@linuxcamp:~$ answer: locate -d ~/archive.db -c '*.txt' output: 4 hint: Add -c after the database path, before the pattern: locate -d ~/archive.db -c '*.txt'
Just 4. With -c, locate did the counting for you and printed the total instead of the four paths. There are exactly four .txt files across the archive. When the number is all you want, -c is the fast way to confirm it without a wall of paths in the way.
By default locate is case-sensitive: PNG and png are different letters to it, so a capitalized pattern would miss lowercase names. The -i flag (short for ignore case) turns that rule off, so upper and lower case match each other.
Your images are all lowercase .png, but suppose you typed the pattern in capitals. Add -i and it still matches:
locate -d ~/archive.db -i '*.PNG' | sort
prompt: student@linuxcamp:~$ answer: locate -d ~/archive.db -i '*.PNG' | sort output: /home/student/archive/images/banner.png /home/student/archive/images/logo.png hint: Add -i after the database path: locate -d ~/archive.db -i '*.PNG' then a padded pipe and sort
An uppercase pattern, lowercase results. -i told locate to treat .PNG and .png as the same, so it matched the two lowercase .png names. Note that only banner.png and logo.png came back, not the .jpg images: -i changed the case rule, not the pattern itself. Without -i, that same '*.PNG' would have matched nothing at all, because the list holds no uppercase .PNG.
One behavior is worth seeing on purpose: what locate does when the pattern matches nothing. Before you run this, decide: will it print an error message, a line that says "not found," or something else entirely?
Ask for a name that is not anywhere in the archive and watch what comes back:
locate -d ~/archive.db nonexistent-xyz
prompt: student@linuxcamp:~$ answer: locate -d ~/archive.db nonexistent-xyz output: hint: Type locate -d ~/archive.db nonexistent-xyz exactly as shown and press Enter
Silence. No error message, no "not found" line: no match means no output, and nothing else. Behind the scenes locate also exits with a status of 1 to signal "found nothing," which scripts can check, but on screen you just see an empty line and your prompt again. So an empty result from locate is not a bug; it is the honest answer, that pattern is not in the list.
There is a second reason locate can come back empty, and it is the one trait to always keep in mind. The list is a snapshot, frozen the moment it was last built. A file created after that is not in the list, so locate will not find it until the list is rebuilt (that rebuild is the updatedb command's job, covered in its own lesson). That is the trade you accept for the speed: locate reads a saved list, so it is instant, but a result is only ever as fresh as the last time the list was written. When you need the truth of this exact moment, find walks the live disk instead.
Scaffolding off. From here to the end, no command is shown. You have every piece you need.
The archive holds some .csv spreadsheet files, and you want all of them, sorted. That ending means a wildcard pattern, quoted so the shell leaves the * alone, and a pipe to keep the order stable.
prompt: student@linuxcamp:~$ answer: locate -d ~/archive.db '*.csv' | sort output: /home/student/archive/reports/2025/data.csv /home/student/archive/reports/2025/metrics.csv hint: Same shape as your very first lookup, the .log one, with a different ending in the quotes
Two spreadsheets, both from 2025, and you built the whole query from memory: database with -d, quoted pattern with a *, padded pipe into sort. That is the workhorse form of locate, and it is yours now.
You want the single path in the list that has the word annual in it, and nothing else. It is one file, so there is no need for a wildcard or a sort. Pick the form of query that matches a word anywhere in a path.
prompt: student@linuxcamp:~$ answer: locate -d ~/archive.db annual output: /home/student/archive/reports/2024/annual.txt hint: A bare word is a substring match, so no star and no quotes are needed after the database
One path, exactly the file you were after. You reached for a bare word because you knew a piece of the name but not where it lived. You pointed locate at the private database with -d, and read the answer straight out of the list, all from memory. That is the same move the real machine will ask of you: know what you want, pick the form of query, type it without a prompt.
Last one. A teammate asks how many .txt files the archive holds, and they want the number, not a wall of paths. One flag you learned in this lesson does exactly that.
prompt: student@linuxcamp:~$ answer: locate -d ~/archive.db -c '*.txt' output: 4 hint: The counting flag goes between the database path and the quoted pattern, and there is nothing to sort
4, straight from the list, no paths in the way. Three challenges, three different forms: a pattern, a bare word, a count. You picked the right one for each job without a command in front of you, which is the entire skill.
You earned this cheat sheet. Every row is a locate query you just ran against the ~/archive list:
The one idea that ties it together: locate is fast because it reads a saved list instead of the disk, and a list only knows what existed when it was last written. A pattern with * anchors to the end of a name; a bare word matches anywhere; -c counts and -i ignores case; and a silent, empty result means either no match or a list that has not been rebuilt yet.
When locate cannot find a file you are sure exists, a stale list is the usual reason: the file was created after the list was last built. Rebuilding the list is the updatedb command's job, which you meet in its own lesson. And when you need an answer that is guaranteed current, reach for find, which walks the live disk every time.
The practice terminal has shown you the shape of every locate query: a pattern with *, a bare-word substring, -c to count, -i to ignore case, and the silent empty result. The Search module ends with one real Linux machine, and that is where you run locate for real.
In the Search capstone mission, a Linux VM boots just for you on this same ~/archive filesystem. The mission installs locate and builds the list, then hands you objectives that use exactly what you just practiced. You read the private database with -d, match with a pattern or a bare word, and sharpen the query with -c and -i. One difference from here: the lab shows no commands. You read the objective, recall the flag, and type it. That recall is what makes it stick.
Finish the other Search lessons, then go read the list for real.
Practice locate in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.