updatedb & locate command problem - Files from external hard drive are no longer indexed after rebooting

Files from my external hard drive are no longer indexed after rebooting. I have to remount and then run

# updatedb

after each reboot. The problem is updatedb takes a few minutes for my external hard drives. Is there any way I can retain indexing for my externals after I reboot so that the locate command can search through my externals?

EDIT: Per Request here are my specs:

$ cat /etc/updatedb.conf
PRUNE_BIND_MOUNTS="yes"
# PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf fuse.glusterfs fuse.sshfs ecryptfs fusesmb devtmpfs"

# mount
/dev/sda5 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
none on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
none on /dev type devtmpfs (rw,mode=0755)
none on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
none on /dev/shm type tmpfs (rw,nosuid,nodev)
none on /var/run type tmpfs (rw,nosuid,mode=0755)
none on /var/lock type tmpfs (rw,noexec,nosuid,nodev)
none on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
gvfs-fuse-daemon on /home/me/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=me)
/dev/sdb1 on /media/me type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096,default_permissions)
/dev/sdd1 on /media/Little Boy type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096,default_permissions)
/dev/sde1 on /media/Fat Man type fuseblk (rw,nosuid,nodev,allow_other,blksize=4096,default_permissions)

# on_ac_power; echo $?
255

Solution 1:

Files from your external harddisk are not indexed because updatedb is configured to not index anything under /media, which is where external storage is usually mounted. This configuration is set in the file /etc/updatedb.conf. Specifically the entry /media in the PRUNEPATHS line.

Some ideas how you can make updatedb to index your external drive:

1. Mount the external drive unter /mnt.

If your external harddisk is supposed to be mounted permanently then I suggest to configure it so that it is mounted under /mnt instead of under /media. That can usually be done by editing /etc/fstab. Search for "fstab" to learn about that. You will need sudo rights to edit /etc/fstab.

2. Create a dedicated database for your external harddisk and query that on demand.

The following command will create a dedicated database:

updatedb -l 0 -o ~/.externalharddisk.db -U /media/externalharddisk

This will create the database in the hidden file .externalharddisk.db in your home. You do not need sudo for that command. Execute the same command again to keep the database updated. Carefull: if you run that command while the external harddisk is not mounted then updatedb will think the files are deleted and will empty the database.

You can set up a script to automate that task. Search for "cronjob" to learn how to do that. Note: you can set up a user cronjob as user. You do not need sudo rights to set up a user cronjob.

The following command will query the database:

locate -d ~/.externalharddisk.db searchterm

You can also query the dedicated database and the default database at the same time:

locate -d ~/.externalharddisk.db: searchterm

The colon at the end followed by nothing means to also search in the default database.

You can make an alias for easier use. Put the following line in your .bashrc:

alias locate-external='locate -d ~/.externalharddisk.db:'

Now you can use locate to search only the default database and locate-external to also search in your external harddisk.

3. Remove /media from PRUNEPATHS

Note: I do not recommend this! This is because of the way updatedb works. If updatedb runs while the external harddisk is removed then all entries pointing to the external harddisk will be removed from the database. If updatedb runs while the external harddisk is connected then entries pointing to the external harddisk will be added again. Since updatedb is running regulary in the background you can never be sure whether the files from the external harddisk are currently indexed or not.

Furthermore: if you remove /media from PRUNEPATHS, this behaviour will also apply to any other external storage you happen to have mounted while updatedb is updating the database.

Solution 2:

In updatedb.conf you have /media in PRUNEPATHS. You should remove it, since your external drives seem to be mounted there.

Solution 3:

These are further embellishments to be added to Lesmana's answer

[1] slight embellishment: may i suggest adding the -i to make sure case of the query searched is no longer an issue: so to add to .bashrc >>

alias locate-external='locate -d ~/.externalharddisk.db: -i'

[2] As regards the updating of both databases; the one in the main HD and the new one in the external; it is probably worth adding one more entry to the .bashrc

alias updateALL='sudo updatedb && updatedb -l 0 -o ~/.externalharddisk.db -U /media/externalharddisk'

and then to simply run updateALL in terminal to update both databases at once

PS:of course do not forget to run . ~/.bashrc to firm it all up once a new line is saved in the .bashrc
PS²: and of course too externalharddisk in all the lines above is replaced by the name of your external HD name ie /media/YOURHDNAME

*PS³ and you can of course conflate both update and locate in your .bashrc

alias LO='sudo updatedb && updatedb -l 0 -o ~/.externalharddisk.db -U /media/externalharddisk ; locate -d ~/.externalharddisk.db: -i'

so now all you need to update and search is to enter LO 'followed by the terms you seek'