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?

2 Answers
2

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:

Remove /media from PRUNEPATHS.

Mount the external drive unter /mnt.

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

I do not recommend removing /media from PRUNEPATHS. 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.

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.

I will now explain how to 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.