如何按时间顺序定期将 tcsh 历史记录备份到单个文件?

发布于 2024-08-16 02:47:16 字数 402 浏览 4 评论 0原文

我在工作中使用 tcsh - 我广泛使用的功能之一是在 shell 提示符下完成命令行历史记录。目前,我已将历史文件的大小限制为 2000(因为我不想让 shell 速度减慢太多)。然而,有时我需要一个命令,我知道我已经使用了一两个月,但现在已被删除。所以我想要一个系统,其中:

  1. 我的历史缓冲区仅存储 2000 行

  2. 不应将旧命令删除,而是应将它们保存到“主”历史文件中,按时间顺序排序,即如果打开了两个 shell,则应根据日期戳对历史记录中输入的命令进行排序(而不是关闭 shell 的顺序)!

  3. 如果这个主历史文件可以自动备份,比如说每周,那就完美了。

我确信许多狂热的 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:

  1. My history buffer stores 2000 lines only

  2. 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)!

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2024-08-23 02:47:16

2000元已经很低了。您可以筹集相当多的资金,而不会遭受太多痛苦。

接下来,您可能希望存储注销时的历史记录,因为这是将新命令添加到 .history 文件中的时间。

在 $HOME 中创建一个名为 .logout 的文件(对于 bash 用户,此文件为 .bash_logout)。在此,将历史记录的内容复制到永久存储中。例如:

cat $HOME/.history >> $HOME/.ancient_history

这会将历史记录附加到文件“.ancient_history”中。对于 bash 用户,要复制的文件称为 .bash_history。

然后创建一个 cron 作业,时不时地创建一个备份。对于初学者来说,这里是将文件移动到带有每天午夜过后 5 分钟的日期戳的文件名。

 5 0 * * *       mv $HOME/.ancient_history $HOME/.ancient_history_`date +%s`

您可能还可以用它做更多的事情,但这足以开始。这是一个非常好的主意,我以前也没有想过这样做:-)

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:

cat $HOME/.history >> $HOME/.ancient_history

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.

 5 0 * * *       mv $HOME/.ancient_history $HOME/.ancient_history_`date +%s`

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 :-)

昔日梦未散 2024-08-23 02:47:16

从来没有想过这样做,但最简单的方法是编写一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文