95

When I run the history command on my ubuntu server, I get output as follows:

   history
   ...
   25  cd ~
   26  ls -a
   27  vim /etc/gitconfig
   28  vim ~/.gitconfig

I want to view the datetime of a particular user. However when I assume them:

su otheruser
export HISTTIMEFORMAT='%F %T  '
history
...
25  cd ~
26  ls -a
27  vim /etc/gitconfig
28  vim ~/.gitconfig

It still doesn't show datetime. I am using zsh shell.

JohnMerlino
  • 6,161
  • 1
    I created a dedicated program for storing and viewing your history, maybe of interest: https://github.com/chrissound/MoscoviumOrange. – Chris Stryczynski Jun 11 '20 at 10:34
  • @ChrisStryczynski is your program haskel-based? I am on ubuntu, how can I install it and what advantages do I have with your program? – Timo Apr 28 '21 at 07:28
  • Probably best to create an issue on there if you need help with installation. Advantages are logging the path, better search functionality, color highlighting and not shell dependent. – Chris Stryczynski Apr 28 '21 at 11:54

3 Answers3

137

I believe the HISTTIMEFORMAT is for Bash shells. If you're using zsh then you could use these switches to the history command:

Examples

$ history -E
    1   2.12.2013 14:19  history -E

Alternatively: \history -E

$ history -i
    1  2013-12-02 14:19  history -E

Alternatively: \history -i

$ history -D
    1  0:00  history -E
    2  0:00  history -i

If you do a man zshoptions or man zshbuiltins you can find out more information about these switches as well as other info related to history.

excerpt from zshbuiltins man page

Also when listing,
  -d     prints timestamps for each command
  -f     prints full time-date stamps in the US `MM/DD/YY hh:mm' format
  -E     prints full time-date stamps in the European `dd.mm.yyyy hh:mm' format
  -i     prints full time-date stamps in ISO8601 `yyyy-mm-dd hh:mm' format
  -t fmt prints time and date stamps in the given format; fmt is formatted 
         with the strftime function with the  zsh extensions described for 
         the %D{string} prompt format in the section EXPANSION OF PROMPT 
         SEQUENCES in zshmisc(1).  The resulting formatted string must be no 
         more than 256 characters or will not be printed.
  -D     prints elapsed times; may be combined with one of the options above.

Debugging invocation

You can use the following 2 methods to debug zsh when you invoke it.

Method #1

$ zsh -xv

Method #2

$ zsh
$ setopt XTRACE VERBOSE

In either case you should see something like this when it starts up:

$ zsh -xv
#
# /etc/zshenv is sourced on all invocations of the
# shell, unless the -f option is set.  It should
# contain commands to set the command search path,
# plus other important environment variables.
# .zshenv should not contain commands that produce
# output or assume the shell is attached to a tty.
#

/etc/zshrc is sourced in interactive shells. It

should contain commands to set up aliases, functions,

options, key bindings, etc.

shell functions

... ... unset -f pathmunge _src_etc_profile_d +/etc/zshrc:49> unset -f pathmunge _src_etc_profile_d

Created by newuser for 4.3.10

slm
  • 369,824
  • 7
    "event not found: -i " "event not found: -E". Do I need to load something in a config file before running these switches? – JohnMerlino Dec 02 '13 at 19:41
  • Are you sure you're in a zsh? I'm using a vanilla zsh that I run and it has them. – slm Dec 02 '13 at 19:43
  • ps -p $$ PID TTY TIME CMD 23771 pts/4 00:00:00 zsh – JohnMerlino Dec 02 '13 at 19:46
  • 1
    What version of zsh? zsh --version. I just confirmed on Ubuntu 12.10 that the commands I gave you worked just fine. – slm Dec 02 '13 at 19:52
  • zsh 4.3.17 (x86_64-unknown-linux-gnu) – JohnMerlino Dec 02 '13 at 23:30
  • you need the extendedhistory option enabled for timestamps of commands to be saved in your HISTFILE. – llua Dec 03 '13 at 04:09
  • 17
    @JohnMerlino I had zsh 4.3.10 (x86_64-unknown-linux-gnu) on a server for which I wanted to view history with timestamps. After looking through the zshbuiltins man page, I discovered that I needed to use fc. What finally worked for me was fc -li. You can pass command numbers to fc, too, so fc -li -100 lists the last 100 commands in your history. – Thomas Upton Jul 23 '14 at 13:11
  • 23
    I have to use \history -E, I use oh-my-zsh – sites Sep 25 '14 at 22:30
  • you can set it permanently in oh-my-zsh by uncommenting this line in your personal .zshrc: HIST_STAMPS="mm/dd/yyyy" – Jeff Clayton Mar 02 '18 at 14:27
  • @juanpastas, that works.. any idea why? – alpha_989 Apr 07 '18 at 16:33
  • 2
    It's a long-standing (6+ years) bug with oh-my-zsh, see this issue on github: https://github.com/robbyrussell/oh-my-zsh/issues/739 – rococo Jun 24 '18 at 20:28
  • 1
    @juanpastas fix is not needed anymore for newer oh-my-zsh versions. Check which history, it should return aliased to omz_history. – normanius Jan 05 '20 at 16:48
  • @sites I also use omz, but no \ needed. Maybe they changed this. – Timo Nov 21 '20 at 18:53
  • In man history I did not find these flags E, i and D. In the Bash History Manual neither.Thanks for the info with zshoptions, what is the D Flag for? – Timo Nov 21 '20 at 18:58
25

history -E or history -i or whatever DO NOT work for me.

zsh --version shows that zsh 4.3.6 (x86_64-suse-linux-gnu).

Then fc -li 100 works! It shows the recent 100 commands with timestamp :)

  • Thanks, this worked for me as well, contrary to the top answer. My version is: zsh 5.1.1 (x86_64-ubuntu-linux-gnu). And while I'm here: do you know how to get the help of the command fc? Neither man fc nor fc --help work – exhuma Jun 13 '17 at 06:49
  • @exhuma why not google it :) – Gab是好人 Jun 14 '17 at 08:57
  • @Gab是好人 do you have history aliased? if you are using oh-my-zsh the history command is add the -l flag in one of the options. see https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/history.zsh#L10-L15 – Mike D Jun 19 '17 at 12:51
  • 1
    fc -lf will show a full timestamp. Note that this works only on zsh, not on bash. – dr_ Jul 27 '17 at 07:49
  • @exhuma In zsh, you can use run-help fc. – xuhdev Oct 28 '17 at 20:37
  • fc's documentation is available in zsh's info manual. The info manual can be accessed via info zsh. The specific heading to look under is Shell Builtin Commands. – GDP2 Mar 30 '18 at 21:16
  • @exhuma On my system I can see the man page for the zsh commands with man zshbuiltins. – Mark Meuer May 11 '20 at 14:27
11

If you are using oh-my-zsh addon in zsh, history -E or history -i wont work (because it's aliased to fc -l 1).

As @juanpastas pointed out, try

\history -E

or

\history -i

or

fc -li 100

alpha_989
  • 443
  • 2
    I just added: 'alias history="fc -li 1"' to redefine the history alias into my ~/.zshrc file and it's now working as desired! – user886869 Jun 08 '19 at 23:14
  • or even better: 'alias history="history -i". That way you can run the same command in the alias, and if you want to add more keys/options it is not that confusing. – DrBeco Oct 07 '19 at 20:36
  • history in omz is aliased to omz_history which uses fc (-l) $@, here and not fc -l 1 but maybe 1 means show all entries. – Timo Apr 28 '21 at 14:48
  • Seems that fc -li 99 means "show history from entry 99" and without a numeric arg, it show only the last 16(?) or so. Try fc -li 1 | wc -l to see how many are available. – Mark Hudson Sep 22 '22 at 22:54