Simple index of external media on Linux

If you’re not the fan of any kind of web-based or GUI application to index your files on external media for you, there’s a way simpler solution for the command line afficiandos out there: use locate.

locate is usually known as the prepared man’s find as it offers a subset of the functionality (finding files by name) with the adventage of it being nearly instantaneous. It does this by calling updatedb to simply index your filesystem into a simple hashed database which locate uses.

Normally, this does fairly well for your usual administrative tasks like “Where the hell is this file?”.

But, being a nice tool, locate also allows you to generate custom databases. Which is pretty useful when handling external drives and having an easy index of them.

I recommend creating ~/.locatedbs and storing database files there kind of like this:

updatedb -U $mountpoint -o $HOME/.locatedbs/$label

This can be explicitly queried like this:

locate -d $HOME/.locatedbs/$label $pattern

This works pretty well with modern environments where the mountpoint includes the label of the device, as this is the only (easy) way to find out where the file you’re looking at:

$ locate -d ~/.locatedbs/imbrium.db win8-usb.img
/media/towo/imbrium/win8-usb.img

Of course, the usability here still sucks. Recent versions of locate support setting the environment variable LOCATE_PATH, which specifies (depending on the version: additional) databases to be searched. In case of Debian and Ubuntu, it’s an additional database path. Thus by inserting

export LOCATE_PATH=$(echo $HOME/.locatedbs/* | sed 's/ /:/g')

into your shell profile, any future logins will be able to simply use locate to search all indexed external drives.

To further increase usability, you’d ideally call an update script shortly before unmounting a drive instead of doing it manually, but I haven’t yet found a convenient way to do so neatly.