如何查看所有 bash 历史记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15116806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I see all of the bash history?
提问by gipcompany
First let me show an example below.
首先让我在下面展示一个例子。
In shell(1) I did the following command.
在 shell(1) 中,我执行了以下命令。
$ 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.
之后,打开另一个 shell(2) 并查看历史记录。
$ 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.
在这种情况下,shell 看不到 shell(1) 执行的历史记录,但我想查看每个 shell 中的所有 bash 历史记录。
So my question is how can I see all of the bash history? Does anybody know how to hack?
所以我的问题是如何查看所有 bash 历史记录?有人知道怎么破解吗?
Thank you very much in advance!
非常感谢您提前!
采纳答案by Carl Norum
You should look into the histappend
shell option and the -a
flag to history
:
您应该查看histappend
shell 选项和以下-a
标志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.
histappend
如果设置,则历史列表会
HISTFILE
在 shell 退出时附加到由变量值命名的文件中,而不是覆盖该文件。
history
-a
将“新”历史行(自当前 bash 会话开始以来输入的历史行)附加到历史文件。
If you put history -a
into your PROMPT_COMMAND
, you'll get an always-up-to-date .bash_history
file.
如果你放入history -a
你的PROMPT_COMMAND
,你会得到一个总是最新的.bash_history
文件。
回答by etusm
cat ~/.bash_history
would also work, although I tend to just use
也可以,虽然我倾向于只使用
vim ~/.bash_history
and then use /
to search
然后用于/
搜索
回答by jfelipesp
try this:
尝试这个:
Edit your .bashrc and append this to it's end:
编辑您的 .bashrc 并将其附加到它的末尾:
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
来源:http: //subbass.blogspot.com.br/2009/10/howto-sync-bash-history-between.html
回答by carl.anderson
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
您可以安装诸如 Advanced Shell History 之类的东西,它将每个命令记录到 sqlite3 数据库中。它带有一个从命令行查询数据库的工具。 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.
完全披露:我编写并维护了该工具。
回答by DeanM
As several have noted, you need to use shopt -s histappend
. Check by running shopt
and verifying that histappend is 'on'.
正如一些人所指出的,您需要使用shopt -s histappend
. 通过运行shopt
并验证 histappend 为“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:
要确保每个命令(跨多个并发 shell)出现在每个 shell 的历史记录中,请在 .bashrc 文件末尾添加以下内容:
# 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.
-a:将新的历史行(自当前 Bash 会话开始以来输入的历史行)追加到历史文件中。
-c: clears the history list.
-c:清除历史列表。
-r: reads the current history fileand append its contents to the history list.
-r:读取当前历史文件并将其内容附加到历史列表中。
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.
运行source .bashrc
或创建新会话并在多个终端窗口中输入注释#Tn
。然后在一个终端上,输入history | tail -N
查看最后N行。您应该会看到在不同终端上输入的所有注释。
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:
将以下内容添加到 /etc/profile.d/bashrc.sh 可能会有所帮助,以便在历史记录的每一行上获取时间戳:
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:27 # T1
3293 2019-01-22 12:41:34 # T2
3294 2019-01-22 12:41:42 # T3
3295 2019-01-22 12:41:50 history | tail -4