Can't locate a directory using "locate"? [duplicate]

Solution 1:

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)

Solution 2:

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.

Solution 3:

Run

sudo updatedb

before locating. This will update the database.