DMesg with human readable timestamps (BASH)

By default, dmesg uses seconds of uptime for it's timestamps, which in less than useful if you're trying to quickly check the time an entry was logged at. Where available /var/log/kern.log can be used to get timestamped entries, but where not available the function below gives a dmesg analog with human readable timestamps

Details

  • Language: BASH

Snippet

dmesg_human() {
    $(type -P dmesg) "$@" | perl -w -e 'use strict;
        my ($uptime) = do { local @ARGV="/proc/uptime";<>}; ($uptime) = ($uptime =~ /^(\d+)\./);
        foreach my $line (<>) {
            printf( ($line=~/^\[\s*(\d+)\.\d+\](.+)/) ? ( "[%s]%s\n", scalar localtime(time - $uptime + $1), $2 ) : $line )
        }'
}

Usage Example

dmesg_human