Maybe I'm overlooking something but is there a way to get your current bash history for the current session you are using like
if i run
ssh host
$ pwd
$ ls
$ cd /tmp
I just want to see those 3 commands and nothing else
Maybe I'm overlooking something but is there a way to get your current bash history for the current session you are using like
if i run
ssh host
$ pwd
$ ls
$ cd /tmp
I just want to see those 3 commands and nothing else
A slightly roundabout way:
history -a ~/current_history
This will save the current session's unsaved bash history to ~/current_history
, which you can then view.
history -a
it throws it along with the previous history into ~/.bash_history ... which could be a problem if your history is longer than the cutoff, which is a 1000 on my centos7 and fedora 24 systems.
– Ray Foss
Jun 5 '17 at 20:53
I had the problem that I wanted to write the current history
to a file but still wanted the entries to be recorded in the main bash history
I solved this by just attaching the file with cat
:
history -a current-history
cat current-history >> .bash_history
Use comp
to compare the entire history (incl. current Bash session) with the already persisted history in .bash_history
and only print those lines that are unique to the current session -- which should show only those commands that were executed since starting the current Bash shell
comm -23 <( history | cut -c 8- ) ~/.bash_history
Edit: as @Wildcard pointed out this command does not work for all distributions of comm
. I tested this on Mac OS.
A variation of the same idea using diff
:
diff <( history | cut -c 8- ) ~/.bash_history | sed -n 's/^< //pg'
comm
assumes sorted input. But, welcome to Stack Exchange! :)
– Wildcard
Feb 1 '17 at 4:12
comm
are sorted inherently in my example above. Did you run it? And it did not produce your current Bash session history?
– Christian Kadner
Feb 1 '17 at 4:21
~/.bash_history
is not sorted; and nor is the output of history
sorted at all once leading line numbers are removed. (Besides which, comm
expects lexicographically sorted input, not numerically sorted input.)
– Wildcard
Feb 1 '17 at 4:29
comm
command in my example are in the same order, not that they are sorted lexicographical. The FreeBSD version of comm
does not require lexicographically sorted input
– Christian Kadner
Feb 1 '17 at 4:41
history
command?? – Hackaholic Dec 3 '14 at 21:52history
but that doesn't give just my session – Mike Dec 3 '14 at 23:21HISTSIZE
andHISTFILESIZE
. – Wildcard Feb 1 '17 at 7:54