8

I added time to the ~/.bashrc file, so now it shows me time when I execute history command like this:

  376  04/10/19 20:39:52 sudo cat ~/.bash_history
  377  04/10/19 20:40:04 date
  378  07/10/19 10:13:29 echo 'HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc
  379  07/10/19 10:13:38 source ~/.bashrc
  380  07/10/19 10:13:54 history
  381  07/10/19 10:20:08 sudo vim /home/subir/.bash_history 
  382  07/10/19 10:20:29 sudo nano /home/subir/.bash_history 
  383  07/10/19 11:34:20 cd ..
  384  07/10/19 11:34:34 find | grep .bash_history
  385  07/10/19 11:34:48 cat ./subir/.bash_history 
  386  07/10/19 11:37:32 history

But when this session's data gets appended to ~/.bash_history file then it shows something like this :

#1570201804
date
#1570423409
echo 'HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc
#1570423418
source ~/.bashrc
#1570423434
history
#1570423808
sudo vim /home/subir/.bash_history 
#1570423829
sudo nano /home/subir/.bash_history 

What are the values after # ?. Can anyone help me deduce it's meaning or tell me a way to get timestamp in ~/.bash_history file.

2
  • thats epoch you can convert epoch to other format with date ex. date -d @1570201804 "+%Y-%m-%d %H:%M:%S" – bac0n Oct 7 '19 at 8:16
  • @bac0n thanks for replying . Thank you for the help.Got it!! – Prototype Oct 7 '19 at 11:30
9

That's Unix Timestamp / Epoch time - number of seconds passed from January 1st, 1970.

If you convert it to datetime, you will get:

$ date -d @1570201804
Fri Oct  4 18:10:04 EAT 2019

In UTC it is 10/04/2019 @ 3:10pm (UTC) but since I am in East Africa, we get +3 time difference coming to 10/04/2019 @ 6:10pm (UTC)

I guess your timezone is +5:30

0
1

Parse .bash_history containing timestamp (awk).

first I cut the field at newline -F \\n and check for lines beginning with #epoch $0 ~ /^#[0-9]+/. Next I trim off first char substr($1,2) before formatting the epoch string strftime("%d/%m/%y %T"). Last I print the first part without newline printf "%5d %s " so I can fetch the next line getline that contains the commands and print them on the same line.

awk -F \\n '{ if ($0 ~ /^#[0-9]+/) {printf "%5d  %s ", ++i, strftime("%d/%m/%y %T", substr($1,2)); getline; print $0 }}' ~/.bash_history

Output:

    1  07/10/19 14:46:09 echo a
    2  07/10/19 14:46:28 echo b
    3  07/10/19 14:46:39 echo c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.