码迷,mamicode.com
首页 > 系统相关 > 详细

每天学习Linux命令——history

时间:2014-10-28 17:35:08      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   color   os   使用   for   sp   文件   log   代码   

  history命令的功能是显示使用过的命令,并为其编号。下面几条命令是history的不同操作:

history n 显示最近使用过的n条命令。

history -c 将当前shell中历史清空。

history -d 801 删除编号为801的命令。

history -a 追加最新一条命令到历史文件中。

history -n 显示还没有从历史文件中读取的历史记录。

history -r 将历史文件中的记录作为当前shell的历史记录。

history -w 将当前记录写入历史文件中,覆盖原内容。

系统默认的history命令难以满足我们的需求,我们可以通过修改/etc/profile文件来对hostory命令的

显示效果进行修改:

(1)显示命令执行的时间

在/etc/profile文件的末尾添加:export HISTTIMEFORMAT="%F %T `whoami` :"(注意:这里whoami两边符号不是单引号,而是Tab键上面那个键的符号,就是~下面的那个符号。)

(2)显示执行命令的用户的源IP

首先通过命令获取登录源IP:USER_IP=`who -u am i 2>/dev/

null | awk ‘ {print &NF}‘ | sed -e ‘ s/[()]//g‘`

if ["&USER_IP" = ""]

then

USER_IP=`hostname`

然后再export HISTTIMEFORMAT时,将USER_IP作为参量写入HISTTIMEFORMAT,

export HISTTIMEFORMAT="%F %T &USER_IP: `whoami` :"

综合以上技巧,我们可以得到以下脚本:

USER_IP=`who -u am i 2>/dev/null | awk ‘{print &NF}‘ |sed -e ‘s/[()]//g‘`

if ["&USER_IP"=""]

then

USER_IP=`hostname`

fi

if [! -d /opt/history]

then

mkdir /opt/history

chmod 777 /opt/history

fi

if [! -d /opt/history/&{LOGNAME}]

then

mkdir /opt/history/&{LOGNAME}

chmod 300 /opt/history/&{LOGNAME}

fi

export HISTSIZE=4096

export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S]"

export HISTFILE=" /opt/history/${LOGNAME}.history"

chmod 600 /opt/history/*history* 2>/dev/null

 

将以上代码追加到/etc/profile文件末尾即可,然后重新登录,会发现history的显示格式和存储文件都发生率变化。

 

每天学习Linux命令——history

标签:style   color   os   使用   for   sp   文件   log   代码   

原文地址:http://www.cnblogs.com/tangxiaolang/p/4056965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!