3

Linux 中的 history 命令中如何启用时间戳

 1 year ago
source link: https://www.51cto.com/article/743163.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Linux 中的 history 命令中如何启用时间戳

作者:TIAP 2022-12-26 17:23:06
今天这篇文章,我们介绍一下如何在 history 命令中启用时间戳。
36d768b745f4ff270357287df2854dea3cb773.jpg

我们大家应该都比较熟悉 history 命令,其用于查看先前都运行过哪些命令。不过有一个问题,默认情况下,history 命令是不显示命令的执行时间(带日期和时间)的,那么我们就不知道某个命令是具体什么时候执行的。

今天这篇文章,我们介绍一下如何在 history 命令中启用时间戳。

在 history 命令中启用时间戳

要在 history 命令中启用时间戳,需要修改 HISTTIMEFORMAT 环境变量。可使用 export 命令,如下所示:

export HISTTIMEFORMAT="%F %T "
  • %F 将以 YYYY-MM-DD 格式显示日期;
  • %T 以 HH:MM:SS 格式显示时间。

设置完成后,再次使用 history 命令,可看到在命令前方显示时间戳,如下:

图片

以上修改,只是在当前会话有效,系统重启后,上述设置就会失效。

永久启用时间戳

要在 history 命令中永久启用时间戳,需要在 .bashrc 文件中进行更改。

如同其他设置一样,如果想要永久的修改一个环境变量,需要在 bashrc 中进行修改。

使用文本编辑器打开文件 .bashrc,这里我们使用 nano:

nano

按键 Alt + / 跳转到文件末尾(vim 使用快捷键 G),添加如下内容:

export HISTTIMEFORMAT="%F %T "
图片

然后保存,并退出编辑器。

使用 source 命令使修改生效:

source

此时,无论什么时候使用 history 命令,都会显示每个命令执行时的时间戳。

以上就是本次分享全部内容,欢迎讨论。

责任编辑:庞桂玉 来源: TIAP

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK