SvennD
Creating your own search database, with mlocate.
January 13, 2016

Creating your own search database, with mlocate.

Posted on January 13, 2016  •  2 minutes  • 293 words  •  Suggest Changes

Creating your own search database, not with blackjack and most definitely not with hookers but with mlocateLocate is one of the possible search options in Linux/GNU. Locate is part of the mlocate package, this package contains locate binary. Locate is a database backed search, as such its faster then find if you search over a large amount of indexed directories. The disadvantage is that you have to first build the database in order to search it.

I believe this is done on some distro’s every night, (cron) if the machine is offline during that time the database does not get updated. The mlocate package has a tool _updatedb _that can be ran to update the ‘central’ database. Its however also possible to create a database for, an external drive, this could be handy if you wanne see if the file is there, while the drive itself is not mounted. (or not even in close proximity to a computer) For me, I wanted to make tapes searchable, even when they where off the machine, in a top-secret-black-site location. (aka a box behind the server)

This can be done easily with updatedb & locate.

# create the database
# the -l 0 sets the updatedb to ignore permissions 
updatedb -l 0 -U /media/external_drive -o external_drive.db

# search a file
locate -d /locate/external_drive.db IsMyImageHere

# limit the search to 10 items
locate -d /locate/external_drive.db -n 10 IsMyImageHere

# show stats on this database
locate -d /tape/external_drive.db -S
Database /tape/external_drive.db:
 92,709 directories
 7,440,981 files
 762,003,203 bytes in file names
 134,485,291 bytes used to store database

# speed showoff for database above :
time locate -d /tape/external_drive.db not_a_file

real 0m6.843s
user 0m6.782s
sys 0m0.061s

~7 seconds for a  92,709 directories, 7,440,981 files search … pretty nice 🙂

Support

If you enjoyed this website, consider buying me a Dr. Pepper

Buy me a Dr PepperBuy me a Dr Pepper