Linux确定命令的执行时长,Linux 历史命令显示执行时间

命令行历史

history

登录shell时,会读取命令历史文件中记录下的命令:~/.bash_history

登录进shell后新执行的命令只会记录在缓存中;这些命令会用户退出时“追加”至命令历史文件中;

history:

​ -a: 追加本次会话新执行的命令历史列表至历史文件中;

-d: 删除历史中指定的命令;

​ -c: 清空命令历史;

快捷操作:

​ !#: 调用历史中第#条命令;

​ !string:调用历史中最近一个以string开头的命令;

​ !!: 上一条命令

让history记录显示时间

效果:

[root@localhost ~]# history 5

365 [2020-01-27 20:50:11]vim .bash_profile

366 [2020-01-27 20:50:50]vim /etc/profile

367 [2020-01-27 20:51:34]source /etc/profile

368 [2020-01-27 20:51:41]history

369 [2020-01-27 20:57:37]history 5

配置:

在全局配置文件/etc/profile或者用户家目录.bash_profile的文件末尾添加一行配置,并使配置生效

# vim /etc/profile或.bash_profile

export HISTTIMEFORMAT="[%F %T]"

####################################

使配置立即生效

[root@localhost ~]# source [/etc/profile|.bash_profile]

还可以显示执行的用户及其IP

# vim /etc/profile

#在文件最后加上如下内容

USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'` #获取用户登录IP

if [ "$USER_IP" = "" ]

then

USER_IP=`hostname`

fi

export HISTTIMEFORMAT="[%F %T][`whoami`][${USER_IP}] " #定义历史命令显示格式

####################################

[root@localhost ~]# source /etc/profile

查看历史命令,已生效

[root@localhost ~]# history 5

433 [2020-01-27 21:39:40][root][192.168.0.106]source /etc/profile

434 [2020-01-27 21:39:42][root][192.168.0.106]history 5

435 [2020-01-27 21:39:49][root][192.168.0.106]reboor

436 [2020-01-27 21:39:51][root][192.168.0.106]reboot

437 [2020-01-27 21:43:13][root][192.168.0.106]history 5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值