19

The locate command sounds intuitive. I would expect it to locate files on the filesystem. However, it rarely finds files on the system that are indeed present:

$ locate ipsec.secrets

This gives no results, even when run from filesystem root directory /. The file is indeed present:

$ whereis ipsec.secrets
ipsec: /usr/sbin/ipsec /etc/ipsec.secrets /etc/ipsec.d /etc/ipsec.conf /usr/lib/ipsec /usr/share/man/man8/ipsec.8.gz

Why was locate unable to find this file?

JohnMerlino
  • 7,309
  • Run updatedb and try again – Jan Sep 06 '14 at 21:03
  • After running that command with elevated privilege, then yes the file showed up. Apparently, the updatedb is updated only once a day and for new programs installed, their files will not show up yet. – JohnMerlino Sep 06 '14 at 21:07

4 Answers4

25

updatedb only runs once a day, you need to run it with root privileges to find recent files.

Jan
  • 12,291
  • 3
  • 32
  • 38
10

If you are not able to locate a file that obviously exists I see only two possibilities:

  1. The file (directory) was created after the last time that the database of locate was updated. By default it is updated once in a day (/etc/cron.daily/mlocate).
    With enough privilege you can fix it forcing an update with

    sudo updatedb
    
  2. The file (directory) was created under a path not scanned by updatedb (case more rare): you can find the keys of the pruned files in the configuration file /etc/updatedb.conf. Search for PRUNENAMES, PRUNEPATHS or PRUNEFS and modify consequently, then update again the database.

    sudo pico /etc/updatedb.conf
    sudo updatedb
    

Of course even without privilege you can still search for the file, for example scanning all the directories and subdirectories starting from a position with something like

find . -name '*ipsec.secrets*'  # to start from the current directory
find / -name '*ipsec.secrets*'  # to start from the root # long long 

Note that whereis has a hard-coded path (where to search for), so may not always find what you're looking for.

Hastur
  • 3,950
2

Being somewhat lazy and not wanting to run sudo updatedb and, since I have a computer that can work for me, it runs sudo updatedb every 15 minutes so I don't have to.

Use sudo crontab -e and find this line:

# m h  dom mon dow   command

insert below it:

*/15 *  *   *   *     /usr/bin/updatedb

Then press Ctrl+O to save the file (write it Out) and then Ctrl+X to eXit.

If you've just created the files in the last 15 minutes though you will still need to run:

sudo updatedb

... to manually update the indices used by locate command.

0

My solution was super fast (but maybe not optimal since your sudo updatedb command will take more time). However, you won't miss any file anymore, even if you have multiple partitions (this is handy for me).

I opened the configuration file of updated (you need sudo privileges to save the following file):

nano /etc/updatedb.conf

I then commented (adding a # at the beginning of the line) all the lines starting with: PRUNENAMES PRUNEPATHS PRUNEFS

Save the file (CTRL+O, ENTER, CTRL+X).

I hope this helps someone else.

desmond13
  • 734
  • 1
  • 8
  • 16