Find out the locate command in Linux
One of the most common activities when working on Linux is finding files and folders. On Linux systems, there are several commands that allow you to search for files, in which find
and locate
are the most used commands.
locate
command is the fastest and easiest way to search for files, as well as folders by name.
In this tutorial, TipsMake.com will explain how to use the locate
command in Linux.
How to use locate command in Linux
- Locate settings (locate command not found)
- Install locate on Ubuntu and Debian
- Install locate on CentOS and Fedora
- How to locate command
- How to use the locate command
Locate settings (locate command not found)
Depending on the distribution and how the system is provided, locate
packages can be preinstalled on Linux systems.
To check if the locate
utility is installed on your system, open the terminal, type 'locate' and press Enter
. If the package is installed, the system will display the message " locate: no pattern to search for specified ", otherwise you will see something like ' locate command not found '.
If the locate
not installed, you can easily install it with the distribution package manager.
Install locate on Ubuntu and Debian
$ sudo apt update $ sudo apt install mlocate
Install locate on CentOS and Fedora
$ sudo yum install mlocate
How to locate command
locate
command searches for a certain pattern through the database file created by the updatedb
command. The results are displayed on the screen, each result on one line.
During the installation of the mlocate
package, a cron job is created to run the updatedb
command every 24 hours. This ensures the database is updated regularly. For more information about cron jobs, check the file /etc/cron.daily/mlocate.
The database can be manually updated by running updatedb
with root or user with sudo privileges:
$ sudo updatedb
The update process will take some time, depending on the number of files and folders, as well as your system speed.
Files created after updating the database will not be displayed in the locate
command results.
Compared to the more powerful find
command when searching for locate
files and command systems that operate faster but lack many features and can only be searched by file name.
How to use the locate command
The syntax of the locate command is as follows:
locate [OPTION] PATTERN.
In the most basic form, when used without any options, the locate
command will print the absolute path of all files and folders that match the search pattern and the user has the right to read the file in the link. search results.
For example, to search for a file called .bashrc, enter:
$ locate .bashrc
The output will include the names of all files containing the .bashrc string in their name:
/etc/bash.bashrc /etc/skel/.bashrc /home/linuxize/.bashrc /usr/share/base-files/dot.bashrc /usr/share/doc/adduser/examples/adduser.local.conf.examples/bash.bashrc /usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bashrc
The /root/.bashrc file will not be displayed, because the example has run the command as a normal user, without access to the / root directory .
If the result list is long, for easier reading, you can move the output to less
:
$ locate .bashrc | less
The locate
command also accepts patterns that contain wildcards such as * . When patterns do not contain these characters, the command will search for * PATTERN * , which is why in the previous example, all files containing the search pattern in their names are displayed.
Wildcards are symbols used to represent zeros, one or more characters. For example, to search all .md files on the system you will use:
$ locate *.md
To limit search results, use the -n option followed by the number of results you want displayed. For example, the following command will search all .py files and display only 10 results:
$ locate -n 10 *.py
By default, locate
commands perform a case-sensitive search. The -i (--ignore-case) option tells the locate
command to run a search that is not case sensitive.
$ locate -i readme.md
Output is as follows:
/home/linuxize/p1/readme.md /home/linuxize/p2/README.md /home/linuxize/p3/ReadMe.md
To display the number of all matching items, use the -c (--count) option. The following command will return the number of all .bashrc files in their name:
$ locate -c .bashrc
Output:
6
By default, locate
commands do not check if the files found still exist on the file system. If you deleted a file after updating the latest database and the file matches the search pattern, it will be included in the results.
To show only the names of files that still exist at the time the locate
command is run, use the -e (--existing) option. For example, the following command will only return the existing .json files:
$ locate -e *.json
If you need to run a more complex search, you can use the -r (--regrec) option, which allows you to search using regular expressions instead of patterns. This option can be specified multiple times.
For example, to search all .mp4 and .avi files on your system, not case sensitive, you will run:
$ locate --regex -i "(.mp4|.avi)"
Locate command searches the file system for files and directories whose names match a given pattern. The command syntax is easy to remember and the results are displayed almost immediately.
For more information about all available options for the locate
command, type man locate
in the terminal.
If you have any questions, please leave a comment in the comment section below!
You should read it
May be interested
- Basic Linux commands everyone needs to knowwhen using linux, you should also learn basic linux commands to make operations quicker and easier. here are some basic commands on linux you should know.
- 18 Interesting Linux Commands in Terminalterminal is a very powerful tool, but it can be made 'interesting' through a few bash commands that tipsmake.com will introduce to you below. let's follow and learn because some commands are quite useful.
- How to find, set and change IP addresses on Linuxip address is like a computer phone number. computers use it to communicate with other devices and vice versa. here are some simple ways to manage ip addresses on linux.
- The Cat command in Linuxthe cat command (short for 'concatenate') is one of the most frequently used commands in operating systems like linux / unix. the cat command allows users to create one or more files, view file contents, join files, and redirect output in a terminal or file.
- How to use the last command in Linuxwant to know who, what time and which device to access your linux computer? please read the following article.
- How to use ss command on Linuxthe ss command is a new alternative to the classic netstat. you can use it on linux to get data about network connections. here's how to work with this helpful tool.
- Some tricks to locate Pokemon GOif you're wondering how to locate pokemon go or how to find pikachu, scyther, electabuzz and some other rare pokemon. please refer to the following article of network administrator.
- 11 uses of ps command in Linuxfor system administrators, ps is a frequently used tool. the ps command is used to list the processes currently running on the system, with many available filtering and display modes via flags and arguments.
- How to find a specific word in a file on Linuxthe famous gnu search program, grep can search inside the file with the correct flag. this article will show you how to find specific words in a file on linux
- Instructions for using zforce command on Linuxthe gzip command is a popular tool used to compress / decompress files in linux. tipsmake.com presents the basics of this tool in the article: some basic terminal commands in linux mint 11.