Categories
Linux Tutorial Series

Linux Tutorial Series – 71 – The locate command

Here is the video version, if you prefer it:

Today we are going to talk about locating files. This is very useful and I use it relatively frequently myself.

The locate command is used to locate files. (Shotts, 2019)⁠ It is used as such:

locate string

where string is a part of the pathname you want to locate. To remind ourselves, a pathname is a location on your disk, such as /home/mislav/Linux_folder, so you are asking your computer “give me all the pathnames that contain string within themselves” (string being an array of characters).

Here is an example:

mislav@mislavovo-racunalo:~/Linux_folder$ locate Linux_folder

/home/mislav/Linux_folder

/home/mislav/Linux_folder/1-4.txt~

/home/mislav/Linux_folder/1264.txt

/home/mislav/Linux_folder/2345.txt

/home/mislav/Linux_folder/a.txt

/home/mislav/Linux_folder/a.txt~

/home/mislav/Linux_folder/ab.txt

/home/mislav/Linux_folder/ab.txt~

/home/mislav/Linux_folder/ab2.txt

/home/mislav/Linux_folder/aba.txt

/home/mislav/Linux_folder/aba.txt~

/home/mislav/Linux_folder/cb.txt

/home/mislav/Linux_folder/cb.txt~

/home/mislav/Linux_folder/file.txt

Here, all paths that contain Linux_folder within them are located.

Note: You may not have locate installed as a command (I know I haven’t). You can install the locate command by following an answer provided here: (“How to install the locate command?,” n.d.)⁠

locate uses a database to find files. What that means is that pathnames are stored in a database and then that database is searched for pathnames matching string.

There also exists a command called find, which you can also use to find files, but we will talk about find in another article.

Thank you for reading!

References 

How to install the locate command? (n.d.). Retrieved February 6, 2020, from https://askubuntu.com/questions/215503/how-to-install-the-locate-command

Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 241-243