Add timestamp to terminal in Linux

I came across an issue few weeks back at work when I did some troubleshooting and ran some commands. When I was documenting it, I just couldn’t remember the exact time I started troubleshooting and issued those commands; I mean I even had putty logs saved for 9 hours of troubleshooting data. At that point I really felt the need to add timestamp to terminal so that I would know what I did and when.

This is our usual Linux terminal or bash shell prompt:

root@kali:~#

Added this line in .bashrc file.

export PROMPT_COMMAND="echo -n \[\$(date +%F-%T)\]\ "

and my Terminal prompt now looks like this:

[2016-10-08-00:47:25] root@kali:~$

Add timestamp to terminal in Linux - blackMORE Ops - 2So next time, I save the output of my putty or of my terminal session, I would know exactly what I did and when I did.

Pretty neat way and saves a lot of headaches if you ask me. It’s not something new and if you’re like Jeff, then you would know it already… but I am not Jeff and not many people are, so just added this to my list of collection. Enjoy.

One comment

  1. just for hint, you can include directly in your $PS variable
    like this:
    export PS1=”[\d \t] ”
    [Thu Oct 20 11:33:39]
    or this:
    export PS1=”[\$(date +%k:%M:%S)] \u@\h\\$ ”
    [11:09:56] root@pc$

    cheers

Leave a Reply

Your email address will not be published. Required fields are marked *