我真的很喜欢使用control+r递归搜索我的命令历史记录。我发现了一些我喜欢与它一起使用的好选择:
# ignore duplicate commands, ignore commands starting with a space
export HISTCONTROL=erasedups:ignorespace
# keep the last 5000 entries
export HISTSIZE=5000
# append to the history instead of overwriting (good for multiple connections)
shopt -s histappend
Run Code Online (Sandbox Code Playgroud)
对我来说唯一的问题是erasedups只删除连续的重复项 - 所以使用这串命令:
ls
cd ~
ls
Run Code Online (Sandbox Code Playgroud)
该ls命令实际上将被记录两次。我想过定期使用 cron 运行:
cat .bash_history | sort | uniq > temp.txt
mv temp.txt .bash_history
Run Code Online (Sandbox Code Playgroud)
这将实现删除重复项,但不幸的是顺序不会被保留。如果我不先sort处理文件,我不相信uniq可以正常工作。
如何删除 .bash_history 中的重复项,保留顺序?
.bash_history通过脚本覆盖文件有什么问题吗?例如,如果您删除了一个 apache 日志文件,我认为您需要发送一个 nohup / reset 信号 …
我的 Bash 历史记录中有一些我想删除的命令。
我可以找到它们history | grep "command with password"然后删除它们history -d <line-number>
但是,当我尝试通过管道将它们批量删除时,xargs我收到一个错误:
history | grep "command with password" | awk '{print $1}' | sort -r | xargs history -d
xargs: history: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我认为这xargs会遍历行号列表并将其一一发送给history -d命令。
是什么导致了这个错误?
注意:我知道还有其他方法可以删除历史记录,问题纯粹是为了提高我对xargs工作原理以及导致错误的错误的理解。