In this article, we’ll explain how to Install the Locate command to find files on ubuntu 20. In Linux, most of us use the Linux “find” command to search any files but have you ever tried the locate command for searching files in the filesystem.
As you already know what does find command do, In this article we’ll show you how to find files in Ubuntu 20
The difference between Find and Locate command.
Prerequisite Install the Locate command to find files on Ubuntu 20
- Installed Ubuntu 20 Operating System on Server/VM.
- Required Sudo or root access for installing locate command
- SSH port open on the server and SSH package pre-installed and enable it (or) Login to server with GUI and follow below steps
Command to verify locate command installed or not:
root@Ubuntu-20:~# which locate root@Ubuntu-20:~#
So, the Locate command is not installed on ubuntu. Follow the below command to install locate command.
Command to install Locate command to find files on Ubuntu 20
$ sudo apt install mlocate
Demo Output:
root@Ubuntu-20:~# sudo apt install mlocate Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: nocache The following NEW packages will be installed: mlocate 0 upgraded, 1 newly installed, 0 to remove and 32 not upgraded. Need to get 50.1 kB of archives. After this operation, 258 kB of additional disk space will be used. Get:1 http://us.archive.ubuntu.com/ubuntu focal/main amd64 mlocate amd64 0.26-3ubuntu3 [50.1 kB] Fetched 50.1 kB in 5s (9,872 B/s) Selecting previously unselected package mlocate. (Reading database ... 142489 files and directories currently installed.) Preparing to unpack .../mlocate_0.26-3ubuntu3_amd64.deb ... Unpacking mlocate (0.26-3ubuntu3) ... Setting up mlocate (0.26-3ubuntu3) ... update-alternatives: using /usr/bin/mlocate to provide /usr/bin/locate (locate) in auto mode Adding group `mlocate' (GID 134) ... Done. Initializing mlocate database; this may take some time... done Processing triggers for man-db (2.9.1-1) ... root@Ubuntu-20:~#
Verify the locate command:
root@Ubuntu-20:~# which locate /usr/bin/locate root@Ubuntu-20:~#
Locate command is successfully installed on Ubuntu 20.
Locate Command Options are mentioned below:
$ locate --h
Demo Output
root@Ubuntu-20:~# locate --h Usage: locate [OPTION]... [PATTERN]... Search for entries in a mlocate database. -A, --all only print entries that match all patterns -b, --basename match only the base name of path names -c, --count only print number of found entries -d, --database DBPATH use DBPATH instead of default database (which is /var/lib/mlocate/mlocate.db) -e, --existing only print entries for currently existing files -L, --follow follow trailing symbolic links when checking file existence (default) -h, --help print this help -i, --ignore-case ignore case distinctions when matching patterns -p, --ignore-spaces ignore punctuation and spaces when matching patterns -t, --transliterate ignore accents using iconv transliteration when matching patterns -l, --limit, -n LIMIT limit output (or counting) to LIMIT entries -m, --mmap ignored, for backward compatibility -P, --nofollow, -H don't follow trailing symbolic links when checking file existence -0, --null separate entries with NUL on output -S, --statistics don't search for entries, print statistics about each used database -q, --quiet report no error messages about reading databases -r, --regexp REGEXP search for basic regexp REGEXP instead of patterns --regex patterns are extended regexps -s, --stdio ignored, for backward compatibility -V, --version print version information -w, --wholename match whole path name (default) Report bugs to https://pagure.io/mlocate. root@Ubuntu-20:~#
How to use Locate command to find files in Ubuntu
Locate command is easy to use with a simple command. Let’s see the syntax to search/find files from filesystems.
$ locate Name_of_your_file or $ locate Name_of_your_folder
Re-built Locate database:
Update Locate DB as it won’t know about recently added/deleted files until databases are rebuilt. To re-built the databases run command “updatedb” follow the below steps.
$ updatedb
Eg: Demo Output
root@Ubuntu-20:~# locate my-first-file.txt root@Ubuntu-20:~# root@Ubuntu-20:~# updatedb root@Ubuntu-20:~# locate my-first-file.txt /home/my-first-file.txt root@Ubuntu-20:~#
Eg: Assume you want to search a file which has a name “my-first-file.txt” then follow below command:
$ locate my-first-file.txt
Suppose you want to search and display all the file which has the same name:
$ locate -n 5 test-file.txt -5 : Number of files that want to show in the output.
Command to remove Locate command from Ubuntu:
$ sudo apt remove mlocate
Demo Output:
root@Ubuntu-20:~#sudo apt remove mlocate Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: mlocate 0 upgraded, 0 newly installed, 1 to remove and 32 not upgraded. After this operation, 258 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 142507 files and directories currently installed.) Removing mlocate (0.26-3ubuntu3) ... Processing triggers for man-db (2.9.1-1) ... root@Ubuntu-20:~# which mlocate root@Ubuntu-20:~#
Note: Both Linux command “Locate” and “find” are almost similar and some unique in their functions.
End of the article, how to install the locate command to find files on ubuntu 20. Ubuntu download page.
Thanks for reading this article, If you have any queries, kindly comment us below.