Skip to content

locate command in MacOS

The MacOS locate command is a powerful tool for quickly locating files on your system. It searches a pre-built database of file names and displays the paths of any matches found. This can be a faster alternative to using the find command, especially for searching large directory structures. By default, the locate command searches the entire system, but you can narrow down the search scope by specifying the starting directory. Keep in mind that the locate database is updated periodically, so newly created files may not immediately show up in the search results.

locate Syntax:

Terminal window
locate [option] [pattern]

MacOS locate Options:

OptionDescription
-dSpecify the database to search
-eSearch for only exact matches
-iPerform case-insensitive search
-rInterpret the pattern as a regular expression
-cShow count of found entries

locate Parameters:

ParameterDescription
patternPattern to search for in the database

How to use locate command:

Search for a file by name

Terminal window
locate example.txt

Searches for a file named “example.txt” in the system.

Update the locate database

Terminal window
sudo /usr/libexec/locate.updatedb

Updates the locate database to ensure the search results are current.

Terminal window
locate -i example

Performs a case-insensitive search for files containing “example”.

Limit search to a specific directory

Terminal window
locate -d /path/to/directory example

Restricts the search to the specified directory “/path/to/directory”.

Display the number of matches only

Terminal window
locate -c example

Returns the count of matches found for the search term “example”.

Terminal window
locate -b "example"

Performs a real-time search that is faster but may not be up-to-date.

Search for files modified within the last 7 days

Terminal window
locate -A7 example

Finds files modified within the last 7 days containing the term “example”.

Terminal window
locate -v example

Provides a detailed output of the search process for better understanding.

How do I use locate in MacOS?

To use the locate command in MacOS, execute the following command:

Terminal window
locate file.txt

How can I find a file with a specific extension using locate in MacOS?

To locate a file with a specific extension, use the following command:

Terminal window
locate *.pdf

How do I make the locate command case-insensitive in MacOS?

You can make the locate command case-insensitive by using the -i flag. Here is an example:

Terminal window
locate -i file.txt

How can I display the number of matching entries found with locate in MacOS?

To display the number of matching entries found, you can use the -c flag with the locate command like this:

Terminal window
locate -c file.txt

Is there a way to limit the search results in locate on MacOS?

Yes, you can limit the number of search results using the -l flag followed by the number of results you want to display. Here’s an example:

Terminal window
locate -l 5 file.txt

How can I exclude certain directories from the locate search in MacOS?

To exclude specific directories from the search, you can use the -d flag followed by the directories you want to exclude. Here is an example:

Terminal window
locate -d /usr/local file.txt

How do I force the updating of the locate database in MacOS?

You can force the updating of the locate database by using the -u flag. Here’s how you can do it:

Terminal window
sudo /usr/libexec/locate.updatedb

Can I use wildcards with the locate command in MacOS?

Yes, you can use wildcards with the locate command to search for files with specific patterns. Here’s an example:

Terminal window
locate *file*.txt

Applications of the locate command

  1. Finding files or directories on a macOS system
  2. Quickly locating specific files or directories by name
  3. Searching for system files or settings
  4. Automating file search tasks
  5. Assistance in troubleshooting issues with missing files