如何在Linux中的两个文件中拥有历史记录
除了作为 HISTFILE 提到的默认文件之外,是否可以将特定用户的历史记录保存在另一个文件中? 如果主文件被删除,我希望有一个备份文件,并让它像该文件的备份一样。
问候, 斯里哈莎·卡鲁鲁。
Is is possible to have the history of a specific user in one more file other than the default file mentioned as HISTFILE?
I would like to have as backup file if main file was removed and let it be like a backup for that one.
Regards,
Sriharsha Kalluru.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以创建到该文件的硬链接。
当他家中的原始文件被删除时,该文件仍然存在,并且可以防止内容丢失。
You can create a hardlink to the file
When the original file in his home is removed the file is still there and preserves the content from being lost.
很多时候,我发现自己在 Bash 中使用 Ctrl-R 来获取四倍于终端宽度的旧命令,只是为了发现已经过去太多天并且它不再存在于 .bash_history 文件中。这里有两行将跟踪您在 bash 提示符下键入的每个命令行,并且根本不使用外部进程,只是简单的 bash。
我解决这个问题的第一个方法是将历史记录中的最大行数增加到一个非常大的数量。但无论它有多大,总有那么一刻我需要几个月前输入的长命令,而它已经离开了历史。当我了解 PROMPT_COMMAND 变量(bash 在显示每个提示之前执行的命令)时,我想到了当前的解决方案。以下是两行内容:
我给自己设定的一个目标是在不使用任何外部进程的情况下实现它,这样 bash 就不必在每次按提示符下的 ENTER 后派生一个新进程。第一行设置历史行的格式以将日期包含为 Unix 时间戳,因此您现在可以在键入每个命令时进行操作。第二行是解决方案的核心,首先确保如果设置了先前的 PROMPT_COMMAND,它会在我们的内容之前执行,然后将一行格式附加
到当前用户主目录中名为 .bash_eternal_history 的文件中。
添加用户名(一开始似乎没有必要)后来变得有用,可以区分“sudo -s”会话和保留“~/”相同值的普通会话,因此将行附加到同一个 .bash_eternal_history 文件中。
我希望你们中的一些人像我一样发现这两行有用。 :-)
Many times I've found myself using Ctrl-R in Bash to get a old command four times the terminal width, just to find out that too many days have passed and it's no longer in the .bash_history file. Here are two lines that will keep track of every command line you type at the bash prompt and use no external processes at all, just plain bash.
My first approach to this problem was increasing the maximum number of lines in the history to a very large quantity. But no matter how large it was, there was always a moment when I needed a long command I typed many months ago and it had already left the history. The current solution came to my mind when I learned about the PROMPT_COMMAND variable, a command that bash executes before showing each prompt. Here are the two lines:
One goal I set to myself was to achieve it without using any external process, so bash wouldn't have to fork a new process after every ENTER pressed at its prompt. The first line sets the format of history lines to include the date as a Unix timestamp, so you can now when you typed every command. The second line, which is the core of the solution, first ensures that, if a previous PROMPT_COMMAND was set, it gets executed before our stuff and then appends a line of the format:
to a file called .bash_eternal_history in the current user home.
Adding the username, which at first seemed unnecesary, became useful later to distiguish between "sudo -s" sessions and normal sessions which retain the same value for "~/", and so append lines to the same .bash_eternal_history file.
I hope some of you find these two lines as useful as I do. :-)
硬链接能解决您的问题吗?
您还可以在此处阅读手册页。
Would hard links solve your problem?
Also you can read the man page here.
我会编写一个 cronjob,每分钟左右将原始的 histfiel 复制到备份位置。您不必担心定义第二个 histfile。
否则,您可以将用户输入的每个命令写入备用文件
对于这种方法,请看这里:
bash,在从控制台输入的每个命令之前或之后运行一些命令
i would write a cronjob that copies the original histfiel to a backuplocation every minute or so. the you don't have to worry about defining a second histfile.
otherwise you could write every command the user enters to a alternate file
for this approach take a look here:
bash, run some command before or after every command entered from console