0

I'm trying to put a timestamp to history command

export HISTTIMEFORMAT='%F %T '

, but commands (both old and new) recorded in history are not showing timestamps.

Output of

history | tail
  445  exoirt HISTTIMEFORMAT='F% T%'
  446  export HISTTIMEFORMAT='F% T%'
  447  history
  448  history | tail
  449  ls
  450  cd
  451  ls
  452  cd
  453  ls
  454  cd

I expected some datetime information between the number and the column. like here (visible in the below snipped too)

       1  2008-08-05 19:02:39 service network restart
       2  2008-08-05 19:02:39 exit
       3  2008-08-05 19:02:39 id
       4  2008-08-05 19:02:39 cat /etc/redhat-release

Ouput of

type history

is

history is an alias for history 0

I tried to insert the variable in ~/.bashrc file, but again it did not work.

Edit: I am running Kali linux. I tried rebooting my system and resetting my history size and data. Edit #2: Output of

echo $ZSH_VERSION 
5.8

Output of

echo $SHELL
/usr/bin/zsh
9
  • it's true that older entries won't take timestamp, but for new entries, AFAIK you need to flush out your .bash_history content first to get ride of old entries – αғsнιη Apr 27 at 14:52
  • How are you checking this? If your history is aliased to history 0, then it shouldn't even have any output. – terdon Apr 27 at 14:57
  • I cleared bash history file and its still not working. Both 'type' and 'which' against history is showing its aliased that way. – stubborn Apr 27 at 14:59
  • Please edit your question and explain what you mean by "not working". Show us the output of history | tail and explain how it is not what you are expecting. The time format will not be visible in the history file. – terdon Apr 27 at 17:02
  • Done. By not working I mean its not showing date. – stubborn Apr 27 at 17:23
1

You're on Kali Linux. The Kali Linux distribution is using zsh as its default login shell, not bash.

This means that any change you make to ~/.bashrc would not be seen by the shell you use. The zsh shell is instead using the ~/.zshrc file for initializations of interactive shell sessions.

Furthermore, the zsh shell has no shell variable called HISTTIMEFORMAT that does anything in particular. (If you had been using bash, you would not have had to export this variable, by the way.) Instead you will have to use the history builtin command to view the history in different ways.

However, first you will have to get the shell to store the history information in its extended format, which include timestamps. You do this with

setopt EXTENDED_HISTORY

(You can do this in ~/.zshrc if you want it in all shell sessions that you start). This shell option is documented in the zshoptions manual.

Then, you will have to get rid of the rather unhelpful alias for history that the default ~/.zshrc on Kali Linux sets up, since it stops us from using the history command properly.

Either comment out the line

alias history="history 0"

or add

unalias history

at the end of the file.

Then, you should be able to see timestamps using

history -i

See also the documentation for fc (history is actually the same as fc -l) in the zshbuiltins manual for how to format the output of history in the format you want. For example

history -t '%F %T'

(This happens to be almost exactly the same output format as from history -i, but with seconds added.)

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.