How can I see all of the bash history?
Asked Answered
H

6

60

First let me show an example below.

In shell(1) I did the following command.

$ ping google.com
PING google.com (74.125.235.164) 56(84) bytes of data.
64 bytes from nrt19s12-in-f4.1e100.net (74.125.235.164): icmp_seq=1 ttl=54 time=2.85 ms
64 bytes from nrt19s12-in-f4.1e100.net (74.125.235.164): icmp_seq=2 ttl=54 time=3.42 ms

And after that, open another shell(2) and look at history.

$ history
 .
 .
 .
 8720  exit
 8721  clear
 8722  history

In this case, the shell can not see the history executed by shell(1), but I want to see all of the bash history in every shell.

So my question is how can I see all of the bash history? Does anybody know how to hack?

Thank you very much in advance!

Horse answered 27/2, 2013 at 16:10 Comment(0)
A
28

You should look into the histappend shell option and the -a flag to history:

histappend

If set, the history list is appended to the file named by the value of the HISTFILE variable when the shell exits, rather than overwriting the file.

history

-a Append the "new" history lines (history lines entered since the beginning of the current bash session) to the history file.

If you put history -a into your PROMPT_COMMAND, you'll get an always-up-to-date .bash_history file.

Africander answered 27/2, 2013 at 16:12 Comment(0)
D
69
cat ~/.bash_history

would also work, although I tend to just use

vim ~/.bash_history 

and then use /to search

Deonnadeonne answered 2/10, 2015 at 18:26 Comment(4)
How to do it when I am working in a virtual environment (venv)? ~/.bash_history shows only the commands outside of the virtual environment.Apprehensible
@Raif - you would need access to the terminal within the virtual environment as the "user" running the app commands (root or the equivalent).Deonnadeonne
could be ~/.zsh_history if you installed zshDosser
This is not the full bash history. These don't show any of the commands I used just minutes ago. They show commands from last night. Hundreds of commands are missing.Catto
A
28

You should look into the histappend shell option and the -a flag to history:

histappend

If set, the history list is appended to the file named by the value of the HISTFILE variable when the shell exits, rather than overwriting the file.

history

-a Append the "new" history lines (history lines entered since the beginning of the current bash session) to the history file.

If you put history -a into your PROMPT_COMMAND, you'll get an always-up-to-date .bash_history file.

Africander answered 27/2, 2013 at 16:12 Comment(0)
P
7

try this:

Edit your .bashrc and append this to it's end:

shopt -s histappend
PROMPT_COMMAND="history -n; history -a"
unset HISTFILESIZE
HISTSIZE=2000

source: http://subbass.blogspot.com.br/2009/10/howto-sync-bash-history-between.html

Pressman answered 27/2, 2013 at 16:36 Comment(0)
T
6

You can install something like Advanced Shell History, which will log each command to a sqlite3 database. It comes with a tool for querying the database from the command line. https://github.com/barabo/advanced-shell-history

With this setup, you will have a unified view of command history across all sessions. You also get things like command history for the current working directory (or subtree), command exit code, command duration, etc.

Full disclosure: I wrote and maintain the tool.

Tientiena answered 16/3, 2017 at 18:38 Comment(0)
B
4

As several have noted, you need to use shopt -s histappend. Check by running shopt and verifying that histappend is 'on'.

To ensure that each command (across multiple concurrent shells) appears in the history for each of those shells, add this at the end of your .bashrc file:

# Skip if not an interactive shell
if [ -z "${PS1}" ]; then return; fi
export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"

-a: appends the new history lines (history lines entered since the beginning of the current Bash session) to the history file.

-c: clears the history list.

-r: reads the current history file and append its contents to the history list.

Run source .bashrc or create new sessions and in several terminal windows enter the comment #Tn in each. Then on one terminal, enter history | tail -N to see the last N lines. You should see all of the comments entered on the different terminals.

It may be helpful to add the following to /etc/profile.d/bashrc.sh in order to get a timestamp on each line of the history:

if [ -z "${PS1}" ]; then return; fi
export HISTTIMEFORMAT='%F %T '

The result looks like this:

 [moi@laBoheme ~]$ history | tail -4
 3292  2019-01-22 12:41:25  # T1
 3293  2019-01-22 12:41:32  # T2
 3294  2019-01-22 12:41:44  # T3
 3295  2019-01-22 12:41:50  history | tail -4
Beane answered 22/1, 2019 at 21:31 Comment(0)
K
1

history 1 worked for me..

This will show the history starting from line 1.

Kristikristian answered 10/6 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.