Unable to read the timestamp of Zsh history
Question
Problem: to understand the following timestamp
1241036430
at ~/.history
: 1241036336:0;vim ~/.zshrc
: 1241036379:0;vim ~/bin/HideTopBar
: 1241036421:0;ls
: 1241036430:0;cat ~/.history
when I have
setopt EXTENDED_HISTORY
HISTFILE=~/.history
in .zshrc.
How can you read the timestamp?
Solution
This simple util, called localtime
is gold for reading files with timestamps:
#!/usr/bin/perl
# http://perl.plover.com/classes/mybin/samples/source/localtime
if ($ARGV[0] eq '-f') {
*show_localtime = \&show_localtime_list;
shift;
}
if (@ARGV) {
for (@ARGV) {
print show_localtime($_), "\n";
}
} else {
while (<>) {
s/^(\d+)/show_localtime($1)/e;
print;
}
}
sub show_localtime {
my $t = shift;
scalar localtime $t;
}
sub show_localtime_list {
my $t = shift;
my @a = localtime $t;
"@a\n"
}
- (There is also a little presentation :))
It handles lots of cases, and seem to understand both timestamps in seconds and mini-seconds, etc.
$ localtime < ~/.histfile
<snip>
: Sat Sep 17 05:55:17 2016:0;cat localtime
OTHER TIPS
Try history -d
. Or just type history -
and press control-D to get all the various options:
% history -
-D -- print elapsed times
-E -- dd.mm.yyyy format time-stamps
-d -- print time-stamps
-f -- mm/dd/yyyy format time-stamps
-i -- yyyy-mm-dd format time-stamps
-m -- treat first argument as a pattern
-n -- suppress line numbers
-r -- reverse order of the commands
You can display the whole history with human-readable timestamps using this one-liner taken from an answer on the zsh mailing list:
perl -lne 'm#: (\d+):\d+;(.+)# && printf "%s :: %s\n",scalar localtime $1,$2' $HISTFILE
I would recommend piping the output to a pager (less
for example) to make it more readable.
Adendum: You can use the history
command itself to translate timestamps found in saved history files as well:
The options to the history
command as explained by Nicholas Riley apply just as well to saved history files, so history -d < historyfile
(or any of the other options) translates the timestamps just fine.
This comes in handy if you're using more than just one history file - I've setup zsh
to keep one history file per pty to avoid mixing up histories from shells running in parallel on the same system (since usually each window/screen/... is particular to a certain task, and so the histories emerging from normal use end up sort of themed).
: 1241036430:0;cat ~/.history
‘: <beginning time>:<elapsed seconds>;<command>’.
extendedhistory - saves the time in seconds. The beginning time is since the epoch.
source: http://zsh.sourceforge.net/Doc/Release/Options.html#index-EXTENDEDHISTORY