7

If I understand correctly, the database locate relies on is just for files on partitions of internal HDDs. I wonder if it is possible to use locate on external HDDs?

7

The locate database is generally configured to omit files on removable disks, since they can't be assumed to be there later. It can be configured through a file such as /etc/updatedb.conf (the location depends on which of the several locate programs you use and how it is configured by your distribution).

For a removable disk, it is probably better to keep the database in a separate file. Run

updatedb --localpaths=/media/my_removable_disk --output=/var/cache/locate/my_removable_disk.locatedb 

to update the database. Add /var/cache/locate/my_removable_disk.locatedb to the environment variable LOCATE_PATH; for reasonably recent versions of GNU locate, an empty path component stands for the default path, so you can use

export LOCATE_PATH=:/var/cache/locate/my_removable_disk.locatedb

If you want to keep the locate database on the removable disk, don't add the path to LOCATE_PATH, because locate stops looking if one of the database files is missing. A wrapper script would be better:

locates () {
  locate "$@"
  for d in /media/*; do
    locate -d "$d/.locatedb" "$@"
  done
}
2
  • 3
    Just a note for future reference, the command is a tad different for mlocate users: updatedb --database-root /media/my_removable_disk --output=/var/cache/locate/my_removable_disk.locatedb --prunepaths "" --prunefs "" – Ivan Apr 2 '13 at 10:54
  • see a lesmana's answer, it worked for me, in particular his point 2. I am on a Fedora 30. – mario Sep 28 '20 at 15:49
5

The config file is here: /etc/updatedb.conf, so if you didn't add anything, just mount your HDD, and do updatedb, then you would be able to search for files on external HDD partitions.

6
  • Thanks! If I disconnect my external HDD from my computer, the config file will be updated automatically later to remove info about the external HDD? – Tim Sep 19 '12 at 5:35
  • @Tim, no, that's not possible at this moment, infomration won't update unless you do updatedb again. – daisy Sep 19 '12 at 5:56
  • @Tim updatedb generate a static db file – daisy Sep 19 '12 at 5:56
  • But on my Ubuntu 12.04, the database is updated automatically and periodically. – Tim Sep 19 '12 at 6:00
  • @Tim yes, there's a cron job that does updatedb daily – daisy Sep 19 '12 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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