Linux Check out History record timestamping tips

  • 2021-01-06 00:52:14
  • OfStack

Linux Check out History record timestamp tips

Anyone familiar with bash will know that using history you can print historical commands that you have previously typed, for example


[root@servyou_web ~]# history | more 
  6 ./test.sh  
  7 vim test.sh  
  8 ./test.sh 

But here only the command is shown, not the execution time, because the history of the command is saved ~/.bash_history It doesn't have a shelf time.

By setting environment variables export HISTTIMEFORMAT="%F %T `whoami` " Time stamp history


[root@servyou_web ~]# export HISTTIMEFORMAT="%F %T `whoami` " 
[root@servyou_web ~]# history | tail 
 1014 2011-06-22 19:17:29 root  15 2011-06-22 19:13:02 root ./test.sh  
 1015 2011-06-22 19:17:29 root  16 2011-06-22 19:13:02 root vim test.sh  
 1016 2011-06-22 19:17:29 root  17 2011-06-22 19:13:02 root ./test.sh  
 1017 2011-06-22 19:17:29 root  18 2011-06-22 19:13:02 root vim test.sh  
 1018 2011-06-22 19:17:29 root  19 2011-06-22 19:13:02 root ./test.sh  
 1019 2011-06-22 19:17:29 root  20 2011-06-22 19:13:02 root vim test.sh  
 1020 2011-06-22 19:17:29 root  21 2011-06-22 19:13:02 root ./test.sh  
 1021 2011-06-22 19:17:29 root  22 2011-06-22 19:13:02 root vim test.sh  
 1022 2011-06-22 19:25:22 root  22 2011-06-22 19:13:02 root vim test.sh  
 1023 2011-06-22 19:25:28 root history | tail

As you can see, the timestamp of the history command has been added, but it is not added in.bash_history. In fact, this time record is stored in the memory of the current shell process. If you log back in to logout, you will find that the timestamp of the commands you executed last time you logged in is the same value, that is, the time at the time of logout.

However, for bash plus screen, this timestamp is still valid for a long time. After all, screen does not exit until server is restarted, so these times can be retained for a long time. You can use it, too echo 'export HISTTIMEFORMAT="%F %T `whoami` "' > > /etc/profile then source 1 the OK

conclusion


Related articles: