my @strings = qw( /home/usernames/doejohnwilson /home/usernames/doejanemary ); my $common = $strings[0] ^ $strings[1]; # every \x00 means the chars are identical my $common_length = 0; if ($common =~ /^(\x00+)/) { $common_length = length $1; }; print substr $strings[0], 0, $common_length;