Category: Perl

Linux: Read / dump contents of DMB file created with httxt2dbm

Berkley DB (DBM) files seem easy to create, but hard to read/manage. The most common task I can think of wanting to do on a database file is to dump or display it’s contents. And there’s no easy or straight forward way to do this (at least not that I found).

We were creating DBM files (from text files) for use by Apache/mod_rewrite using the httxt2dbm utility. If you create “file.dbm” it actually creates “file.dbm.pag” and “file.dbm.dir”.

To output the contents of the DBM file(s), Perl comes to the rescue. A colleague of mine wrote this after we searched the net for a working script and failed to find one. I’m sharing it in hopes someone will find it useful.

# /path/to/ /path/to/file.dbm
use SDBM_File;
use strict;

my ($name) = @ARGV;
die "Missing DBM name.\n" unless $name;
my %map;
tie( %map, 'SDBM_File', $name, 'O_RDONLY', 0644 ) || die "Can't open DBM file\n"; for my $k (sort(keys %map)) {
  # value is terminated by a null character, but the hash value contains cruft after that
  # so cut off the value at the first \0
  my @v = split("\0",$map{$k});
  print $k, "\t", $v[0], "\n";
untie %map;