updatedb & locate command problem - Files from external hard drive are no longer indexed after rebooting
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.
In updatedb.conf
you have /media
in PRUNEPATHS
. You should remove it, since your external drives seem to be mounted there.
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'