Overview
The locate command is used to search for files and directories across the entire Linux system using keywords. While the find command scans the disk in real-time, locate refers to a pre-built “filename database.” This makes its search speed significantly faster. However, it cannot find new files created after the last database update.
Specifications (Arguments and Options)
Syntax
locate [options] pattern
Main Arguments and Options
| Option | Description |
-i | Perform a case-insensitive search (ignore-case). |
-c | Display only the number of matches instead of file paths (count). |
-r | Use basic regular expressions for the search pattern (regexp). |
--regex | Use extended regular expressions for the search pattern. |
-0 | Output results separated by a null character (\0) for paths with spaces. |
-d <DBpath> | Search using a specific database file other than the default. |
Basic Usage
You can search by providing a part of a filename. Any path containing that string will be displayed.
Command
# Search for all paths containing "nginx.conf"
locate nginx.conf
Execution Result
/etc/nginx/nginx.conf
/usr/share/doc/nginx/nginx.conf.example
/var/lib/docker/overlay2/.../etc/nginx/nginx.conf
Practical Commands
Case-Insensitive Search
This is useful when you are unsure if a file is named “ReadMe” or “README”.
# Search for "readme.md" regardless of capitalization
locate -i readme.md
/home/user/project/README.md
/usr/share/doc/git/README.md.gz
Narrow Down Results with Regular Expressions
Standard locate uses partial matches. By using regular expressions, you can set strict conditions, such as finding only files that end in “.log”.
# Search only for files ending in ".log" under /var/log/
locate -r "^/var/log/.*\.log$"
/var/log/auth.log
/var/log/kern.log
/var/log/syslog
Count the Number of Matches
Use this when you only need to know “how many” files match rather than seeing the full list of paths.
# Count the total number of JPEG files in the system
locate -c -i .jpg
1452
Customization Points
- Running updatedb: If the results seem outdated, manually update the database by running
sudo updatedb. - AND Search: While
locatedoes not have a built-in AND function, you can combine it withgrep(e.g.,locate nginx | grep conf).
Important Notes
- Not Real-Time: The search targets files as they existed when the database was last built. Files created recently will not appear until the database is updated (usually via a daily cron job). Use
sudo updatedbfor an immediate update. - Deleted Files: Deleted files may still appear in results until the database is refreshed.
- Permissions: Depending on the environment, files in directories that a regular user cannot view may be excluded from the search results for security reasons.
Applications
Manually Updating and Controlling the Database
The updatedb command runs behind the scenes of locate. You can use specific options or configuration files to exclude certain directories from the database.
Manual Update
# Update the database immediately to reflect recent changes
sudo updatedb
Exclude Specific Paths (Temporary)
You can shorten database creation time and reduce noise by excluding large areas like backup directories.
# Update the database while excluding /mnt/backup
sudo updatedb --prunepaths='/mnt/backup'
To exclude a path permanently, edit the PRUNEPATHS section in /etc/updatedb.conf.
Summary
The locate command provides an overwhelmingly faster response than find when performing broad file searches across the entire system. It is ideal for situations where you don’t know where a configuration file is located or want to check if a specific library is installed. However, because accuracy depends on the database’s freshness, it is important to use find for recent files and locate for existing system files.
