我正在研究运行fedora 9的x86目标。
每当我重新启动它时,我的历史记录都会恢复到某种状态,而且我没有在重启之前在会话中执行的命令。
我需要更改以更新重启前的历史记录?
我正在研究运行fedora 9的x86目标。
每当我重新启动它时,我的历史记录都会恢复到某种状态,而且我没有在重启之前在会话中执行的命令。
我需要更改以更新重启前的历史记录?
Answers:
哪个历史? bash的历史?如果您丢失了bash历史记录并且一次有多个会话,那是因为每个会话都会覆盖其他会话的历史记录。
你可能想告诉bash不要每次都覆盖历史记录,而是要附加到它上面。您可以通过修改.bashrc来运行 shopt -s histappend
。
您还可以通过将HISTSIZE导出为大数字来增加历史文件的大小(以字节为单位,因此100000应该足够多)。
HISTSIZE
是要记住的命令数,而不是字节数。
我遇到了同样的问题 - 但我的 .bashrc
文件已经有了 shopt -s histappend
并纠正 HISTFILE
, HISTSIZE
, HISTFILESIZE
。
对我来说问题是我的 .bash_history
文件归 根 而不是我的用户名,所以我的用户永远不会在退出时保存到该文件。
sudo chmod your_user_name .bash_history
做到了!谢谢。
sudo chown user_name:user_name ~.bash_history
解决了!
查找环境变量HISTFILE,HISTSIZE,HISTFILESIZE。
我编写了一个脚本,用于根据以下内容为每个会话或任务设置历史文件。
# write existing history to the old file
history -a
# set new historyfile
export HISTFILE="$1"
export HISET=$1
# touch the new file to make sure it exists
touch $HISTFILE
# load new history file
history -r $HISTFILE
它没有必要保存每个历史命令,但它保存了我关心的那些,并且更容易检索它们然后通过每个命令。我的版本还列出了所有历史文件,并提供了搜索所有历史文件的功能。
完整来源: https://github.com/simotek/scripts-config/blob/master/hiset.sh
我在.bashrc中写了一些行来完成以下操作: 将每个命令后的每个会话保存到文件中。您将创建与终端一样多的历史文件。从最近的历史文件开始启动新终端时,将先前会话中的所有历史文件加载到历史缓冲区,直到达到行计数阈值。
HISTCONTROL=''
HISTFOLDER=~/.bash_histories
HISTFILEEXT=history # only files in $HISTFOLDER with this extension will be read
shopt -s histappend # append when closing session
mkdir -p $HISTFOLDER
HISTFILE=$HISTFOLDER/$(date +%Y-%m-%d_%H-%M-%S_%N).$HISTFILEEXT # create unique file name for this session. Nanoseconds seems to be unique enough, try: for ((i=0; i<=10; i++)); do date +%Y-%m-%d_%H-%M-%S_%N; done
# if HISTFILE unset, history is not saved on exit -> not really necessary if we save after each command, but its a double net safety
HISTSIZE=-1 # maximum number of commands to hold inside bash history buffer
HISTFILESIZE=-1 # maximum number of lines in history file
# history -a $HISTFILE # bash saves the total history commands entered since startup or since the last save and saves that amount of commands to the file. This means reading a history file after typing commands will trip up bash, but this won't be a problem if the history file is only loaded in the beginning. This means that only new commands are saved not all the old loaded commands, thereby we can load as many history files into the buffer as we want and still only save newly thereafter typed commands
PROMPT_COMMAND="history -a $HISTFILE; $PROMPT_COMMAND" # This command is executed after very typed command -> save history after each command instead after only closing the session
# Load old histories from last 5 files/sessions
HISTLINESTOLOAD=2000
# --reverse lists newest files at first
names=($(ls --reverse $HISTFOLDER/*.$HISTFILEEXT 2>/dev/null))
toload=()
linecount=0
# Check if is really file and count lines and only append to $toload if linecount under $HISTLINESTOLOAD
for fname in ${names[*]}; do
if test -f $fname; then
linecount=$((linecount+$(wc -l < $fname) ))
if test $linecount -ge $HISTLINESTOLOAD; then
break
fi
toload+=($fname)
fi
done
# Beginning with the oldest load files in $toload into bash history buffer
for (( i=${#toload[*]}-1; i>=0; i-- )); do
history -r ${toload[$i]}
done