Find the answer to your Linux question:
Results 1 to 7 of 7
How exactly does 'locate' command work? Why is it faster than 'find' command?...
  1. #1
    Just 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?

  2. #2
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    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 filesystem
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  3. #3
    Super Moderator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,144
    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
    Code:
    man locate
    Press Q to quit
    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.

  4. #4
    Just 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?

    Code:
    $ locate * > good.txt
    The above text file has a size of 8.0 MB ( for my comp).

    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?

  5. #5
    Linux Engineer Thrillhouse's Avatar
    Join Date
    Jun 2006
    Location
    Arlington, VA, USA
    Posts
    1,377
    Quote Originally Posted by baltana View Post
    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?

    Code:
    $ locate * > good.txt
    The above text file has a size of 8.0 MB ( for my comp).

    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?
    Correct...sort of. The file is usually called locate.db, slocate.db or mlocate.db. You can find out by running:
    Code:
    locate *locate.db
    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.
    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?
    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.

  6. #6
    Just Joined! KPolulak's Avatar
    Join Date
    Feb 2009
    Location
    New Jersey, USA
    Posts
    42
    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!

  7. #7
    Just 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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...