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 directoryWhy is this happening? How can I find the directory?
33 Answers
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-repoIt'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) 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):
In case it is a new file or directory, If you have enough privilege you can force an update:
sudo updatedbthis will update all and only the files and directories present in allowed paths and not discarded (case 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 updatedbIndeed you can find the keys of the pruned files/directories in the configuration file
/etc/updatedb.conf. Search forPRUNENAMES,PRUNEPATHSorPRUNEFS, 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/. 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.
Run
sudo updatedbbefore locating. This will update the database.
2