
The locate command in Linux is an alternative to the find command. Unlike find, the locate command looks for files in a regularly updated database in the system instead of searching the entire file system. This approach makes searching faster and convenient, especially when working with many files.
This article explains how to use the locate command to effectively search for files and directories in Linux.
locate Over find CommandWhile both find and locate commands have their use cases, locate operates much faster. locate uses a database that Linux builds automatically in the background to allow for faster results. The results from locate may not always reflect the state of the system if the database is out of date. However, you can regularly update the database manually or schedule a cron job.
Alternatively, the find command in Linux provides more precise results by searching in real-time, making it ideal for locating files based on specific criteria.
locate DatabaseThe locate command relies on a database that indexes file locations across your file system. By default, Linux updates this database periodically via a background job. However, it's important to update the database manually in case you make changes to the file system.
You can manually trigger the system to update the locate database using the following command.
The above command ensures the system indexes the most recent files and directories.
locate DatabaseBy default, you'll find the locate database in the following location on most Linux systems.
However, you can specify a different database file when searching by passing the -d option:
The above command allows you to specify custom databases, especially when working on multiple mounted filesystems or network shares.
locate CommandMost Linux systems pre-install the locate command by default. However, if you don't find the command, follow the steps below to install it.
Check whether the locate command is installed.
If locate is not installed, the command prints the following output.
locate on Ubuntu and DebianFollow the steps below to install locate on Ubuntu and Debian.
Update your system's package information index.
Install the locate package.
The above command installs the plocate package, which provides the locate command on Ubuntu and Debian systems.
locate on CentOS and FedoraFollow the steps below to install locate on CentOS and Fedora.
Update your system's package information index.
Install the locate package.
This command installs the mlocate package, which provides the locate command on CentOS and Fedora systems.
To securely transfer files between local and remote systems, explore the scp command in Linux for efficient file management.
locate Command SyntaxThe following is a basic locate command syntax:
In the above locate command:
[options]: Defines optional flags that modify the command's behavior.pattern: Specifies the file or directory name to search for.Follow the steps below to create sample files and directories to test the locate command.
Create three empty .txt files.
Create two empty .exe files.
Create a dir directory and a subdirectory. Replace /path/to/parent/ with your actual path.
The above command creates the dir directory and sub_dir subdirectory under /path/to/parent/dir1/. The -p option creates parent directories if they don't exist.
Find your active working directory.
This command displays the full path of your working directory.
Create a file like logfile.txt and write some sample data with a text editor such as Nano.
Add the following contents to the logfile.txt file.
Save and close the file.
locate OptionsHere are the most common locate command options in Linux.
locate Command in Linux ExamplesRun the following samples to learn how to use the linux locate command.
Update the locate database to index any new files and directories.
Search for a specific file named vultr.txt.
This command lists all paths that match the vultr.txt filename and displays all instances from the database.
Output:
Perform a case-insensitive search for all files named vultr.txt.
This command performs a case-insensitive search, so it will return results for vultr.txt, Vultr.txt, and other case variations.
Output.
Count the number of files that match example.txt.
This command returns the total count of files that match example.txt, without listing them.
Output.
Limit the search results to 10.
This command limits the output to the first 10 matches for example.txt.
Output.
Search for files that end with .exe using a regular expression.
Output.
locate Command ExamplesFollow the steps below to run advanced locate command searches.
Search for a directory named dir.
This command uses a regular expression to match directories matching dir at the end of the path.
Output.
Search for all .pdf extension files.
This command finds all files with a .pdf extension at the end using a regular expression.
Output.
Combine options to search for .log files, ignore cases, and limit the results to 5.
This command performs a case-insensitive search for files that end with .log extension and limits the results to 5.
Output.
Combine locate with awk to extract specific fields from the search results.
This command finds the file path using locate, searches for error in that file with grep, and uses awk to remove the date, time, and log level from the output, displaying only the error messages.
Output.
Combine locate with sort and uniq to count unique matches and sort them.
This command finds the file path using locate and displays its contents with cat. The command then searches for lines containing error using grep and sorts these lines alphabetically while counting the occurrences of each unique error message with uniq -c. The command then sorts the counts in descending order showing the most frequent errors at the top.
Output.
Combine locate with sed to perform inline editing on the search results.
This command finds the file path using locate, displays its contents with cat, and replaces all occurrences of error with ERROR using sed. The command transforms the case of the error term throughout the file content.
Output.
Combine locate with tee to save and display output simultaneously.
This command finds the file path using locate, displays its contents with cat, searches for lines containing the term ERROR using grep, and writes the matching lines to the terminal and the output.txt file using tee.
Output.
Search files containing the word log and filter the results using the grep command.
The above command first finds all file names containing log and then pipes the output to grep to filter the results, showing only the files in the /var/log directory.
Output.
Delete all files that match vultr.txt by combining locate with xargs.
This command finds all files named vultr.txt and pipes the list to xargs, which then deletes the file using the rm -f command.
Output.
Use locate to search for files and run advanced actions using `find.
The above command finds all files named Vultr.txt and passes the list to find which executes ls -l on each file to display detailed information.
Output.
locate Command Environment VariablesThe following Linux environment variables affect the locate command.
LOCATE_PATH: Specifies the path to the locate database.LOCATE_CONFIG: Specifies the path to the locate configuration file.LOCATE_PATH VariableThis environment variable lets you define a custom path for the locate database. By default, locate searches through its pre-configured database, which is usually located in /var/lib/mlocate/mlocate.db.
You can set a different locate database path by running the following command. Then run locate to search for example.txt in that database.
LOCATE_CONFIG VariableThis environment variable specifies a custom configuration file path that determines how locate behaves. The default configuration file controls important parameters, such as which directories to exclude from the database or which files to ignore during the search. By customizing this file, you can fine-tune the locate command behavior to meet your specific needs.
While it's less commonly used compared to LOCATE_PATH, the LOCATE_CONFIG variable is useful when working in environments where the default configuration does not fit your requirements. You can point the variable to a custom configuration file that defines how the database is updated, which paths are indexed, and more.
You can set the LOCATE_CONFIG variable by running the following command.
locate with mlocate and slocate Commandsmlocate and slocate commands are variants of the locate command but they offer additional features and security. These commands are often used interchangeably with locate.
mlocate (Merging Locate) Commandmlocate is a variant of locate that only updates the parts of the database that have changed. This makes it faster and more efficient when updating the database, especially on systems with many files. mlocate sophisticated privacy features ensure users can only locate files they have permission to see.
Example of mlocate command.
The first command above updates the mlocate database and the subsequent locate command searches for example.txt in the database.
slocate (Secure Locate) Commandslocate is another variant of locate that emphasizes on security. The command ensures that users can only see results for files they have the proper permissions to access. This is especially useful in multi-user environments where users shouldn't be able to locate files they don't have access to. slocate includes more advanced file permission checks when creating the database and searching files.
Example of slocate command.
Just like in mlocate, the updatedb.slocate commad updates the database and then uses the locate command to perform your search. The key difference lies in the way the results are filtered based on user permissions, which may yield fewer results compared to a standard locate.
In this article, you've installed the locate command in Linux, covered the command's syntax, command options, and run practical examples. With the locate command, you can quickly and efficiently search for files and directories on your system. By utilizing the different locate command advanced use cases, you can enhance your search and streamline your workflow in Linux.
0 Comments
Be the first to comment and share your perspective with the community.