18

I want to locate a directory in my system using "locate" command. I know there is a directory named "bench-repo". when I'm giving the following command it shows me the following error :

locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory

Why is this happening? How can I find the directory?

3
  • 6
    If the directory is new, you have to update the database: sudo updatedb and then run your locate command again – Zanna Jul 19 '16 at 6:37
  • @Zanna wow!!! It just worked !!! Thanks a lot, buddy :) – tuxtu Jul 19 '16 at 6:39
  • 2
    @Zanna Can you please convert your comment into an answer? – edwinksl Jul 19 '16 at 6:51
24

The binary database used by locate (/var/lib/mlocate/mlocate.db) is updated once daily by cron, so locate will not find new files.

You can fix this by first running sudo updatedb

sudo updatedb && locate -e bench-repo

It's a good idea to use the -e flag so you only find files that still exist.

Oh and here's a bonus tip - you can get locate to give you a detailed listing by passing to ls -l

ls -l $(locate -e bench-repo)
5

Two causes two actions

In general when you are not able to locate a file or it is because recently created (after the last database update) or because it is not in the paths where updatedb is going to search its entries or matches some pruning rules(see below):

  1. In case it is a new file or directory, If you have enough privilege you can force an update:

    sudo updatedb 
    

    this will update all and only the files and directories present in allowed paths and not discarded (case 2).

  2. In case it the file was out of the paths scanned by updatedb, or was matching some exclusions rules you can modify the configuration file and update the database:

    sudo pico /etc/updatedb.conf  # manual update
    sudo updatedb
    

    Indeed you can find the keys of the pruned files/directories in the configuration file /etc/updatedb.conf. Search for PRUNENAMES, PRUNEPATHS or PRUNEFS, modify consequently, then update again the database.

Some words more about locate and updatedb

To be able to locate a file or a directory, it has to be actually included in your mlocate database, usually stored in /var/lib/mlocate/mlocate.db.

This database is updated periodically. By default it is updated daily and you can see its cron file in /etc/cron.daily/mlocate. If not present there you can search for it with locate mlocate | grep cron and see where it is and how often it is updated.

Use man locate and man updatedb for further readings.

2
  • 3
    Good tip about the PRUNE* variables. – Arronical Jul 19 '16 at 8:52
  • It happens often on multiuser machine with an area for temporary data or for production data (you know were it is, if it is temporary it doesn't matter, if permanent it is with fixed names...). – Hastur Jul 19 '16 at 8:56
3

Run

sudo updatedb

before locating. This will update the database.

2
  • Hi PeeKay, I'm afraid that if you are not root you will not be able to run so updatedb (that was the reason of sudo). Moreover if the file is out of the paths scanned by updatedb (the excluded keys are specified in the PRUNE* variable of the configuration file /etc/updatedb.conf) you will not find it however. – Hastur Jul 19 '16 at 10:28
  • @Hastur thats true. I thought he would have an idea of how updating a database works – PeeKay Jul 19 '16 at 12:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.