dmesg – Custom Time Format

datedmesg

dmesg has the option -T to print human-readable timestamps, and new versions have the option --time-format, where the time format can be changed, for instance:

--time-format iso

Unfortunately, the iso time format is not much readable either. Can I define my own time format using the standard date syntax? ie:

date +%F-%T

Best Answer

Yes, you can, e.g. by re-formatting the time with gawk (GNU awk) like this:

dmesg --time-format iso | gawk '{ printf (strftime("+%F-%T",mktime(gensub("[-+T:,]"," ","g",$1)))) ; $1 = ""; print $0}'

dmesg prints iso dates here like this:

2019-09-02T06:10:30,708864+02:00 this is an important message

We strip the first string of special characters then use the mktime function in (g)awk to parse the string into a date, then strftime to format - use your favourite parameters.

This gives something like

+2019-09-02-06:10:30 this is an important message

Small notes:

  • We ignore the time zone here assuming this is obvious since we are running the command locally.

  • Please note that the time given by dmesg can be inaccurate (in fact, sometimes way off) if you suspend and then wake the computer due to the way timestamps are stored, see dmesg(1):

    man 1 dmesg
    
Related Question