Installing and using the locate command in GNU/Linux

In GNU/Linux env, the locate command is a very good tool for locating folders/directories and files. It uses an updated system database, unlike the find command, which searches without indexes, making the locate command much faster and more efficient, especially if we have a system with few resources and/or high demand. The only drawback of the locate command is if its database does not exist, is outdated, or is corrupted.

Install

For install* in Debian (Kubuntu, Ubuntu) distros:

1
[user@host ~]$ sudo apt-get install mlocate

*We need root permission

For install* in RHEL (RedHat, Fedora, Centos, CentOS Stream, AlmaLinux, Rocky) distros:

1
[user@host ~]$ sudo yum install mlocate -y

*We need root permission

If wish search a folder or file:

  • For search a file “file.txt

1
[user@host ~]$ locate file.txt

  • For search any file or directory that contains in the name “report” as word:

1
[user@host ~]$ locate report

  • For search any file or directory that contains in the name “report” as word case-insensitive:

1
[user@host ~]$ locate -i rEpOrT

If is the first time in use the command locate get the error:

1
[user@host ~]$ locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory

We can fix with the command:

1
[user@host ~]$ sudo updatedb && locate -e bench-repo

In this time you can search any wish.

Additional parameters

  • -c: Displays the number of files found instead of listing them.
  • -r: Allows you to use regular expressions for searching.
  • -b: Searches only the file’s base name (without the path).

Resume

The locate command is a great, very fast tool compared to the find command. However, the locate command searches a database, so the results may be unexpected or inaccurate (it may be outdated, corrupted, or empty).

Another drawback is that the locate command will only show you files that your user has access to. The locate command is good for searching directories or files, but it can’t be better than the find command.

Ads Blocker Image Powered by Code Help Pro

Se detectó bloqueador de anuncios | Ad blocker detected

Hemos detectado que estás usando extensiones para bloquear anuncios. Por favor, apóyanos desactivando este bloqueador de anuncios.

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock