locate Command in Linux with Examples

The locate command in Linux retrieves file names from databases generated by updatedb(8), matching specified patterns.

When patterns lack globbing characters, locate assumes an asterisk before the pattern. By default, it doesn’t verify file existence but mandates parent directories in the database. It cannot locate files created after the database’s last update. If –regex isn’t used, globbing characters are accepted in patterns.

The syntax for the locate command in Linux is:

locate [OPTION]... PATTERN...


  • Option refers to various command options that modify the behavior of the locate command.
  • Pattern specifies the search pattern(s) used to find files or directories.

Options :

  • -b, –basename: Match only the filename against the specified patterns. This is the opposite of –wholename.
  • -c, –count: Instead of listing file names, print the number of matching entries.
  • -d, –database DBPATH: Replace the default database with DBPATH. DBPATH can be a single database file path or a colon-separated list of paths. An empty path signifies the default database. A path of – indicates the standard input. Reading from the standard input is allowed only once.
  • -e, –existing: Display only entries corresponding to files that exist at the time locate is executed.
  • -h, –help: Display a summary of available options and exit successfully.
  • -i, –ignore-case: Ignore case distinctions when matching patterns.
  • -l, –limit, -n LIMIT: Exit successfully after finding LIMIT entries. If the –count option is specified, the resulting count is also limited to LIMIT.
  • -m, –mmap: Ignored for compatibility with BSD and GNU locate.
  • -P, –nofollow, -H: When checking for file existence (if the –existing option is specified), do not follow trailing symbolic links. This causes broken symbolic links to be reported as regular files. This is the opposite of –follow.
  • -S, –statistics: Display statistics about each read database instead of searching for files and exit successfully.
  • -q, –quiet: Do not display error messages encountered while reading and processing databases.
  • -r, –regexp REGEXP: Search for a basic regular expression REGEXP. No PATTERNs are allowed if this option is used, but this option can be specified multiple times.
  • -w, –wholename: Match the entire path name against the specified patterns. This is the default behavior. The opposite can be specified using –basename.

Examples :


Example 1: Search a File using locate

$ locate httpd.conf

The locate command searches for files named httpd.conf using the mlocate database. This command is faster than find because it uses a pre-compiled database of file names and locations.


Example 2: Counting File Occurrences

$ locate -c httpd.conf

The locate -c httpd.conf command counts the number of occurrences of files named httpd.conf using the mlocate.db database.


Example 3: Change mlocate Database Location

locate -d /path/to/your/database httpd.conf

The locate -d /path/to/your/database httpd.conf command temporarily overrides the default mlocate.db database with the specified database path, /path/to/your/database, and searches for files named httpd.conf.

Example 4: Ignore Case in Locate Output

The locate command by default is configured to accept the file name in a case sensitive manner. In order to make the results case insensitive, we can use the -i option:

In the following example, we created two files with both lowercase and uppercase.

# cd /tmp
# touch new.txt NEW.txt
# updatedb

If you use the locate command only with the lowercase, it will find only the lowercase file.

# locate new.txt

Use locate -i, which will ignore case, and look for both lowercase and uppercase file.

$ locate -i new.txt

Example 5: Restrict the Locate Output

$ locate -l 5 passwd

The locate -l 5 passwd command restricts the output to display only the first five occurrences of files matching the pattern “passwd”. This is useful for quickly identifying a few specific instances of a file without having to scroll through a long list of results.


Example 6: Displaying Database Statistics

The locate -S command displays statistics about the mlocate.db database, providing insights into its contents and usage.

$ locate -S


Database /var/lib/mlocate/mlocate.db:
	15,076 directories
	172,533 files
	8,942,105 bytes in file names
	3,976,548 bytes used to store database

Example 7: Check File Existence

The locate -e command displays only files that actually exist on the system at the time the command is executed. This ensures that even if a file is listed in the mlocate.db database, it is physically present on the system before being displayed.

# locate -e sysctl.conf



Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.