如何按时间顺序定期将 tcsh 历史记录备份到单个文件?
我在工作中使用 tcsh - 我广泛使用的功能之一是在 shell 提示符下完成命令行历史记录。目前,我已将历史文件的大小限制为 2000(因为我不想让 shell 速度减慢太多)。然而,有时我需要一个命令,我知道我已经使用了一两个月,但现在已被删除。所以我想要一个系统,其中:
我的历史缓冲区仅存储 2000 行
不应将旧命令删除,而是应将它们保存到“主”历史文件中,按时间顺序排序,即如果打开了两个 shell,则应根据日期戳对历史记录中输入的命令进行排序(而不是关闭 shell 的顺序)!
如果这个主历史文件可以自动备份,比如说每周,那就完美了。
我确信许多狂热的 shell 用户都遇到过这样的情况 - 我希望能从其中一位用户那里得到答案!
I use tcsh at work - one of the features I use extensively is command-line history completion at the shell prompt. Currently, I've limited the size of my history file to 2000 (as I don't want to slow down the shell too much). However at times I need a command I know I've used a month or two back , but by now has been erased. So I want a system wherein:
My history buffer stores 2000 lines only
Instead of older commands getting erased , they should be saved into a "master" history file, ordered chronologically i.e if two shells were opened , then the commands entered in the history should be sorted as per the datestamp (not the order in which the shells were closed)!
It would be perfect , if this master history file could be auto-backed up, say per week basis.
I'm sure many of avid shell users have faced a situation like this - I'm hoping to get the answer from one of such users !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2000元已经很低了。您可以筹集相当多的资金,而不会遭受太多痛苦。
接下来,您可能希望存储注销时的历史记录,因为这是将新命令添加到 .history 文件中的时间。
在 $HOME 中创建一个名为 .logout 的文件(对于 bash 用户,此文件为 .bash_logout)。在此,将历史记录的内容复制到永久存储中。例如:
这会将历史记录附加到文件“.ancient_history”中。对于 bash 用户,要复制的文件称为 .bash_history。
然后创建一个 cron 作业,时不时地创建一个备份。对于初学者来说,这里是将文件移动到带有每天午夜过后 5 分钟的日期戳的文件名。
您可能还可以用它做更多的事情,但这足以开始。这是一个非常好的主意,我以前也没有想过这样做:-)
2000 is pretty low. You could raise that a fair amount without suffering too much.
Next you probably want to store the history on logout, since this is when new commands are added to the .history file.
Create a file called .logout in your $HOME (for bash users, this file is .bash_logout). In this, copy the contents of the history to a permanent store. For example:
This will append the history to a file ".ancient_history". For bash users, the file to copy is called .bash_history.
Then create a cron job that creates a back up of this every now and again. For starters here is one that moves the file to a filename with a date stamp at 5 minutes past midnight every day.
There are probably more things you could do with this, but this is enough to get started. It's a pretty good idea that I hadn't thought of doing before either :-)
从来没有想过这样做,但最简单的方法是编写一个 cron 作业,将历史文件附加到另一个文件。这样做的问题是,除非您在转储后编写 cron 来清除历史文件,否则您会得到重复项。
历史记录仅按行号存储(据我所知),因此每个转储的数字都会重复。但你冷酷地添加了一条带有转储日期的标记线。
never quite thought of doing this but the simplest way would be to write a cron job that appended the history file to another file. The problem with this would be that you would get duplicates unless you wrote the cron to clear the history file after it did the dump.
history is stored (as far as i am aware) by line number only so the numbers would repeat for each dump. but you cold add a marker line with the date of the dump.