Results 1 to 7 of 7
How exactly does 'locate' command work?
Why is it faster than 'find' command?...
- 02-24-2009 #1Just Joined!
- Join Date
- Oct 2008
- Location
- Bhubaneswar, Bangalore, INDIA
- Posts
- 22
locate command
How exactly does 'locate' command work?
Why is it faster than 'find' command?
- 02-24-2009 #2
Hi and Welcome !
This question poped up in my mind a few months ago and this blog answered it pretty well.
Locate vs Find is like Speed vs Agility in your filesystemIt is amazing what you can accomplish if you do not care who gets the credit.
New Users: Read This First
- 02-24-2009 #3
Hello and welcome to the forums.
Have a look at the man pages for info about the locate command.
I'm not too sure it's faster than find command, if you know how to use it correctly.
Just open a terminal window and type
Press Q to quitCode:man locate
And try the man pages for find too
Code:man find
I do not respond to private messages asking for Linux help, Please keep it on the forums only.
All new users please read this.** Forum FAQS. ** Adopt an unanswered post.
- 02-24-2009 #4Just Joined!
- Join Date
- Oct 2008
- Location
- Bhubaneswar, Bangalore, INDIA
- Posts
- 22
Thanks for the replies.
I have some more queries.
1) So, 'locate' command uses a database of all files in the system. everytime we use the command, it scourges through the DB. Am I right?
The above text file has a size of 8.0 MB ( for my comp).Code:$ locate * > good.txt
So I was expecting the DB to be of somewhat the same size.
Am I to expect that there is a file like the above 'good.txt' which contains all possible file paths and we are like using the "Find" button in GEDIT to match the string?
2) I have two more partitions mounted on /media/disk and /media/disk-1. (They are mounted during startup of comp itself)
But I can see none of the files in them using "locate" command. It simply ignores them.
What can be the reason?
- 02-25-2009 #5
Correct...sort of. The file is usually called locate.db, slocate.db or mlocate.db. You can find out by running:
However, that doesn't mean a flat text file and its "database" counterpart will have the same size. For example, my locate.db is ~4.2 MB but running the command you referred to produces a text file of 1.5 MB.Code:locate *locate.db
Either you're excluding the directory somehow in /etc/updatedb.conf or when the cron job that updates your locate.db runs (it's usually scheduled to run daily), those partitions are not mounted so they don't get indexed in the db. You can do a test by mounting those partitions and running updatedb as root. Then do another locate to see if it finds files in there. If it doesn't, do a man updatedb.conf to see if you can include them somehow.2) I have two more partitions mounted on /media/disk and /media/disk-1. (They are mounted during startup of comp itself)
But I can see none of the files in them using "locate" command. It simply ignores them.
What can be the reason?
- 02-26-2009 #6
Yes, you guys are correct. The locate command isn't searching your system in real time. Instead, it searches a database of filenames and directories. That is how the locate command is able to return the results of a search immediately. This database is automatically updated daily.
However, if you use locate to search for a file or directory that you just recently installed (say about 12 hours), it will not be found in your search because the file/directory has not been added to the database yet. If this ever happens to you, you can manually update the database using the updatedb command as root (sudo updatedb). Problem solved!
Important note for Debian-based distros:
When using locate you are actually using the slocate command ("secure locate"), you're just not aware of it. This command won't search files that the user running slocate doesn't have permission to view. To get around this, simply run locate as root (sudo locate "filename") to return more search results.
Yes, while I'm still a Linux rookie myself, I actually just finished learning about commands such as find, locate, and grep (regex) in a book I have which is why I, surprisingly, was able to be so informative. Yay me!
- 02-27-2009 #7Just Joined!
- Join Date
- Oct 2008
- Location
- Bhubaneswar, Bangalore, INDIA
- Posts
- 22
Yes...
After I excluded /media from PRUNEPATHS in /etc/updatedb.conf, I am able to locate files in there...


Reply With Quote
