In this blog, we will show you the steps to install locate command to find files on Amazon Linux or CentOS. Searching files in the filesystem, mostly we use the “find command”, there is one more command in Linux that we can use for finding files i.e., the “locate command”
Amazon Linux is a free, open-source operating system popular among developers and system administrators. It is customized for Amazon Web Services (AWS) and is intended to provide a reliable, secure, and high-performance environment for executing cloud applications. Amazon Linux has a variety of programs that make it easy to manage and administer your system, such as the “locate” command, which allows you to quickly identify files on your system.
Find command:
- Searches a directory structure for requested files
- the print is the default action and displays match
- ls displays full details on matches
- exec allows a command to be run against each
- ok can be used when a confirmation prompt is desired
Locate command:
- High-speed and low-impact searching because it doesn’t actually search the file system it searches the database that was built
- Searches DB index instead of the actual file system
- Index is updated each night by default, and you can manually update by command “updatedb”
- Locate won’t know about recently added/deleted files until databases are rebuilt
Steps to install Locate command to find files on Amazon Linux or CentOS using YUM:
Step 1: Connect to Your Amazon Linux Instance
The first step in installing the locate command on Amazon Linux is to connect to your instance using SSH. To do this, you will need to have an SSH client installed on your local computer. If you are using a Windows computer, you can use the PuTTY SSH client. If you are using a Mac or Linux computer, you can use the built-in Terminal application.
To connect to your Amazon Linux instance, you will need to know the public IP address or hostname of your instance, as well as the username and password that you used when you created the instance. Once you have this information, open your SSH client and enter the following command:
ssh [username]@[public IP address or hostname]
Replace [username] with your username and [public IP address or hostname] with the public IP address or hostname of your instance. If this is the first time you are connecting to your instance, you may see a warning message asking you to confirm the authenticity of the host. Type “yes” to continue.
Step 2: Update Your System
Before installing the locate command, it is recommended to update your system to ensure that you have the latest security patches and software updates. To do this, run the following command:
sudo yum update
This command will download and install any available updates for your system. Depending on the number and size of the updates, this may take several minutes to complete.
Verify locate command installed or not:
[root@AmazonLinux ec2]# which locate /usr/bin/which: no locate in (/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin) [root@AmazonLinux ec2]# rpm -qa | grep locate [root@AmazonLinux ec2]#
As per the above output locate command is not installed.
Step 3: Install the mlocate Package
The locate command is part of the mlocate package, which is not installed by default on Amazon Linux. To install the mlocate package, run the following command:
sudo yum install mlocate
This command will download and install the mlocate package, which includes the locate command and its supporting files. Depending on the speed of your internet connection, this may take a few minutes to complete.
Sample Output:
[root@AmazonLinux ec2]# yum install mlocate Loaded plugins: priorities, update-motd, upgrade-helper Resolving Dependencies --> Running transaction check ---> Package mlocate.x86_64 0:0.2.0.amzn1 will be installed --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================================================================== Package Arch Version Repository Size ==================================================================================================================================================== Installing: mlocate x86_64 0.2.0.amzn1 amzn-main 97 k Transaction Summary ==================================================================================================================================================== Install 1 Package Total download size: 97 k Installed size: 279 k Is this ok [y/d/N]: y Downloading packages: mlocate-0.22.2-4.10.amzn1.x86_64.rpm | 97 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mlocate-0.2.0.amzn1.x86_64 1/1 Verifying : mlocate-0.2.0.amzn1.x86_64 1/1 Installed: mlocate.x86_64 0:0.2.0.amzn1 Complete! [root@AmazonLinux ec2]#
Step 4: Update the Locate Database
After installing the mlocate package, you will need to update the locate database before you can use the locate command. The locate database is a pre-built index of all the files on your system, which allows the locate command to quickly find files based on their name or content.
To update the locate database, run the following command:
sudo updatedb
This command will scan your entire system and create a new index of all the files on your system. Depending on the size of your system and the number of files it contains, this may take several minutes to complete.
Sample Output
[root@AmazonLinux ec2]# which locate /usr/bin/locate [root@AmazonLinux ec2]# updatedb [root@AmazonLinux ec2]#
Default database storage location “mlocate.db”, updatdb is the command to rebuild the database which is recently added/deleted files.
example :
[root@AmazonLinux ec2]# locate mlocate.db /usr/share/man/man5/mlocate.db.5.gz /var/lib/mlocate/mlocate.db.dd1i5P [root@AmazonLinux ec2]#
Step 5: Use the Locate Command to Find Files
Once you have installed and updated the locate command, you can use it to quickly find files on your system. The basic syntax of the locate command is as follows:
locate [filename]
Replace [filename] with the name or partial name of the file you are looking for. For example, to find all files on your system with the word ‘example’ in their name, you can run the following command:
locate example
This command will search the locate database and return a list of all files on your system that contain the word ‘example’ in their name, along with their full path.
If you want to search for files that match a specific pattern, you can use wildcard characters. For example, to find all files with the extension ‘.txt’ in your home directory, you can run the following command:
locate ~/ *.txt
This command will search for all files with the extension ‘.txt’ in your home directory (~), including any subdirectories.
Note: When using the locate command, it is important to be aware of the fact that it searches only the files that have been indexed in the locate database. If you have recently created or modified a file on your system and it does not appear in the search results, you may need to update the locate database again using the ‘updatedb’ command.
How to check to locate command installed on Amazon Linux
[root@AmazonLinux ec2]# which locate /usr/bin/locate [root@AmazonLinux ec2]# rpm -qa | grep locate mlocate-0.2.0.amzn1.x86_64 [root@AmazonLinux ec2]#
As per output locate is installed.
Steps to remove locate command from Amazon Linux server:
[root@AmazonLinux ec2]# yum remove mlocate Loaded plugins: priorities, update-motd, upgrade-helper Resolving Dependencies --> Running transaction check ---> Package mlocate.x86_64 0:0.2.0.amzn1 will be erased --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================================================================== Package Arch Version Repository Size ==================================================================================================================================================== Removing: mlocate x86_64 0.2.0.amzn1 @amzn-main 279 k Transaction Summary ==================================================================================================================================================== Remove 1 Package Installed size: 279 k Is this ok [y/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Erasing : mlocate-0.2.0.amzn1.x86_64 1/1 Verifying : mlocate-0.2.0.amzn1.x86_64 1/1 Removed: mlocate.x86_64 0:0.2.0.amzn1 Complete! [root@AmazonLinux ec2]#
Conclusion:
The locate command is a useful utility for quickly finding files on your Amazon Linux instance. By following the steps outlined in this blog post, you can easily install and use the locate command to search for files on your system. However, it is important to remember that the locate command searches only the files that have been indexed in the locate database, so if you have recently created or modified a file on your system, you may need to update the database again using the ‘updatedb’ command. With these tips in mind, you can effectively use the locate command to manage your files and applications on Amazon Linux.
End of Article: These are the steps to install Locate command to find files on Amazon Linux or CentOS.