查看历史命令及时间

通常,当我们在 Linux 中尝试查看命令的历史记录时,我们只会看到所有使用过的命令,而看不到它们使用的时间。
在这里,将介绍如何查看使用命令的时间和日期。

# history | less
1 service network restart
2 ping google.com
3 vi /etc/resolv.conf
4 ping google.com
5 vi /etc/sysconfig/network-scripts/ifcfg-eth0
6 service network restart
7 ping google.com
8 yum -y install samba
9 yum -y install quota
10 service iptables stop

在上面你可以看到你只能查看运行的命令

现在查看历史命令以及时间和日期

# export HISTTIMEFORMAT="%d:%m:%Y %T "

# history | less
1 08:10:2012 11:31:31 service network restart
2 08:10:2012 11:31:31 ping google.com
3 08:10:2012 11:31:31 vi /etc/resolv.conf
4 08:10:2012 11:31:31 ping google.com
5 08:10:2012 11:31:31 vi /etc/sysconfig/network-scripts/ifcfg-eth0
6 08:10:2012 11:31:31 service network restart
7 08:10:2012 11:31:31 ping google.com
8 08:10:2012 11:31:31 yum -y install samba
9 08:10:2012 11:31:31 yum -y install quota
10 08:10:2012 11:31:31 service iptables stop

但是导出命令将依赖于会话,因此一旦您更改会话或者重新启动,更改将不起作用。
因此,在您的 .bash_profile 文件中添加导出行以进行永久更改。

# vi /root/.bash_profile
export HISTTIMEFORMAT="%d:%m:%Y %T "
日期:2020-06-02 22:17:19 来源:oir作者:oir