Linux 系统 history 命令详解
2017-11-01 00:06
225 查看
转自 http://blog.csdn.net/ichuzhen/article/details/30479435
history 命令在 Linux 系统中可以说是为我们提供了很大的便利,详细了解其用法可以有效提高工作效率。
在默认环境下,系统(以Centos 6.2为例)中默认 history 命令执行的效果是:
[plain] view
plain copy
983 ls
984 cat iptables
985 ifconfig
986 mysql -u root -p
987 ssh root@192.168.0.51
988 df -h
989 history
以上是截取的部分结果,下面来详细说说其用法:
1.命令的用法及参数usage: history [-c] [-d offset]
or history -anrw [filename] or history -ps arg [arg...]
参数:
n :数字,要列出最近的若干命令列表
-c :将目前的 shell 中的所有 history 内容全部消除
-a :将目前新增的 history 指令新增入 histfiles 中,若没有加 histfiles ,则预设写入 ~/.bash_history
-r :将 histfiles 的内容读到目前这个 shell 的 history 记忆中
-w :将目前的 history 记忆内容写入 histfiles
重复以前的指令:
[plain] view
plain copy
[root@keystone ~]# history 10 # 列出最后10条执行的命令
[root@keystone ~]# !988 # 执行第988条指令
[root@keystone ~]# !! # 执行最后一条指令
[root@keystone ~]# history -c # 清除所有的指令记录
history 命令的用途确实很大,但需要小心安全的问题,尤其是 root 的历史纪录档案,因为这将很多的重要资料在执行的过程中会被纪录在 ~/.bash_history 当中,需要使用过程中恰当的管理。
2.使用 HISTTIMEFORMAT 显示时间戳在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTTIMEFORMAT='%F %T '
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` " # 这将显示执行命令的用户
这个功能只能用在当 HISTTIMEFORMAT 这个环境变量被设置之后,之后的那些新执行的 bash 命令才会被打上正确的时间戳。在此之前的所有命令,都将会显示成设置 HISTTIMEFORMAT 变量的时间。
3.使用 HISTSIZE 控制历史命令记录的总行数
在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTSIZE=100 # history 命令的记录数量。The number of commands to remember in the command history. The default value is 500.
export HISTFILESIZE=100 # 记录文件的行数。The maximum number of lines contained in the history file. When this variable is assigned a value, the his-tory file is truncated, if necessary, by removing the oldest entries, to contain no more than that number of lines. The default value is 500. The history file is also truncated to this size after writing it when an interactive shell exits.
如果你想禁用 history,可以将 HISTSIZE 设置为 0。
4.使用 HISTFILE 更改历史文件名称
默认情况下,命令历史存储在 ~/.bash_history 文件中。
在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTFILE=/var/log/command_history
5.使用 HISTCONTROL 从命令历史中剔除连续重复的条目
在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTCONTROL=ignoredups # ignoredups 只能剔除连续的重复条目。要清除整个命令历史中的重复条目,可以将 HISTCONTROL 设置成 erasedups
export HISTCONTROL=erasedups
6.使用 HISTCONTROL 强制 history 不记住特定的命令
在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTIGNORE='ls' # A colon-separated list of patterns used to decide which command lines should be saved on the history list.Each pattern is anchored at the beginning of the line and must match the complete line
history 命令在 Linux 系统中可以说是为我们提供了很大的便利,详细了解其用法可以有效提高工作效率。
在默认环境下,系统(以Centos 6.2为例)中默认 history 命令执行的效果是:
[plain] view
plain copy
983 ls
984 cat iptables
985 ifconfig
986 mysql -u root -p
987 ssh root@192.168.0.51
988 df -h
989 history
以上是截取的部分结果,下面来详细说说其用法:
1.命令的用法及参数usage: history [-c] [-d offset]
or history -anrw [filename] or history -ps arg [arg...]
参数:
n :数字,要列出最近的若干命令列表
-c :将目前的 shell 中的所有 history 内容全部消除
-a :将目前新增的 history 指令新增入 histfiles 中,若没有加 histfiles ,则预设写入 ~/.bash_history
-r :将 histfiles 的内容读到目前这个 shell 的 history 记忆中
-w :将目前的 history 记忆内容写入 histfiles
重复以前的指令:
[plain] view
plain copy
[root@keystone ~]# history 10 # 列出最后10条执行的命令
[root@keystone ~]# !988 # 执行第988条指令
[root@keystone ~]# !! # 执行最后一条指令
[root@keystone ~]# history -c # 清除所有的指令记录
history 命令的用途确实很大,但需要小心安全的问题,尤其是 root 的历史纪录档案,因为这将很多的重要资料在执行的过程中会被纪录在 ~/.bash_history 当中,需要使用过程中恰当的管理。
2.使用 HISTTIMEFORMAT 显示时间戳在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTTIMEFORMAT='%F %T '
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` " # 这将显示执行命令的用户
这个功能只能用在当 HISTTIMEFORMAT 这个环境变量被设置之后,之后的那些新执行的 bash 命令才会被打上正确的时间戳。在此之前的所有命令,都将会显示成设置 HISTTIMEFORMAT 变量的时间。
3.使用 HISTSIZE 控制历史命令记录的总行数
在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTSIZE=100 # history 命令的记录数量。The number of commands to remember in the command history. The default value is 500.
export HISTFILESIZE=100 # 记录文件的行数。The maximum number of lines contained in the history file. When this variable is assigned a value, the his-tory file is truncated, if necessary, by removing the oldest entries, to contain no more than that number of lines. The default value is 500. The history file is also truncated to this size after writing it when an interactive shell exits.
如果你想禁用 history,可以将 HISTSIZE 设置为 0。
4.使用 HISTFILE 更改历史文件名称
默认情况下,命令历史存储在 ~/.bash_history 文件中。
在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTFILE=/var/log/command_history
5.使用 HISTCONTROL 从命令历史中剔除连续重复的条目
在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTCONTROL=ignoredups # ignoredups 只能剔除连续的重复条目。要清除整个命令历史中的重复条目,可以将 HISTCONTROL 设置成 erasedups
export HISTCONTROL=erasedups
6.使用 HISTCONTROL 强制 history 不记住特定的命令
在 /etc/bashrc 中编辑:
[plain] view
plain copy
export HISTIGNORE='ls' # A colon-separated list of patterns used to decide which command lines should be saved on the history list.Each pattern is anchored at the beginning of the line and must match the complete line
相关文章推荐
- Linux 系统 history 命令详解
- linux系统wc命令详解
- Linux - nmon - IBM系统监控工具nmon命令详解(2)
- linux uname命令参数及用法详解(linux查看系统信息命令)
- Linux 系统命令及其使用详解(大全)
- Linux chkconfig 命令参数及用法详解 | linux系统服务设置命令
- Linux 系统命令及其使用详解(大全)
- Linux 系统命令及其使用详解(大全)
- e2label命令_Linux e2label 命令用法详解:设置第二扩展文件系统的卷标
- umount命令_Linux umount 命令用法详解:用于卸载已经加载的文件系统
- env命令_Linux env 命令用法详解:显示系统中已存在的环境变量
- lastlog命令_Linux lastlog 命令用法详解:显示系统中所有用户最近一次登录信息
- 详解Linux系统中at与atq命令的用法
- linux系统中top命令输出详解
- apt-key命令_Linux apt-key 命令用法详解:管理Debian Linux系统中的软件包密钥
- Ubuntu Linux系统下Apt-get命令参数详解
- linux下history(历史)命令用法详解
- Linux 系统命令及其使用详解
- linux系统vi命令详解
- Ubuntu Linux系统下apt-get命令详解