Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash history between two points in time

I would like to do something like

history 2960-2966

with the expectation of this kind of output:

2960  2013-12-09 20:59:35 bundle update
2961  2013-12-09 21:00:08 git st
2962  2013-12-09 21:00:12 git add .
2963  2013-12-09 21:01:19 git st
2964  2013-12-09 21:01:43 git log
2965  2013-12-09 21:02:45 git st
2966  2013-12-09 21:02:57 git reset HEAD Gemfile.lock

Is this possible in the bash shell?

UPDATE: My question is about seeing those seven history rows, not the dates, per se; it just happens that I have a HISTTIMEFORMAT directive in my .bash_profile to give the date and time stamp. If I were to remove it then I would wish history 2960-2966, or something, to generate this output:

2960  bundle update
2961  git st
2962  git add .
2963  git st
2964  git log
2965  git st
2966  git reset HEAD Gemfile.lock

I want the desired history rows to be displayed, preferably with any customization I've specified in .bash_profile.

Why would I want this? A very simple use case is: I do something like history | grep 'bundle update' get a decent result, then want to see some history from that point plus a few lines farther along, or maybe the history bracketing that point.

like image 536
Purplejacket Avatar asked Dec 12 '13 21:12

Purplejacket


2 Answers

You could use sed. Say:

history | sed -n '2960,2966p'

to print line numbers 2960 to 2966 from the output of history command.

Quoting help history:

If the $HISTTIMEFORMAT variable is set and not null, its value is used
as a format string for strftime(3) to print the time stamp associated
with each displayed history entry.  No time stamps are printed otherwise.

You could set the format to get the timestamp in the desired format by saying:

export HISTTIMEFORMAT="%F %T "

Since the history file is written by default only upon session close, you'd need to say:

history -w

in order to update the history file in the current session.

like image 61
devnull Avatar answered Sep 27 '22 17:09

devnull


fc -l 2960 2966

This works on OS 10.6.X. It gives exactly what you need. Hope this helps!

like image 44
Devesh Avatar answered Sep 27 '22 17:09

Devesh