系统之家 - 系统光盘下载网站!

当前位置:系统之家 > 系统教程 > Linux中history命令实例

Linux中history命令的操作实例汇总(2)

时间:2014-12-26 11:28:57 作者:qipeng 来源:系统之家 1. 扫描二维码随时看资讯 2. 请使用手机浏览器访问: https://m.xitongzhijia.net/xtjc/20141226/33495.html 手机查看 评论

  8.使用 HISTCONTROL 从命令历史中剔除连续重复的条目

  在下面的例子中,pwd 命令被连续执行了三次。执行 history 后你会看到三条重复的条目。要剔除这些重复的条目,你可以将 HISTCONTROL 设置为 ignoredups:

  代码如下:

  # pwd

  # pwd

  # pwd

  # history | tail -4

  44 pwd

  45 pwd

  46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]

  47 history | tail -4

  # export HISTCONTROL=ignoredups

  # pwd

  # pwd

  # pwd

  # history | tail -3

  56 export HISTCONTROL=ignoredups

  57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above]

  58 history | tail -4

  9.使用 HISTCONTROL 清除整个命令历史中的重复条目

  上例中的 ignoredups 只能剔除连续的重复条目。要清除整个命令历史中的重复条目,可以将 HISTCONTROL 设置成 erasedups:

  代码如下:

  # export HISTCONTROL=erasedups

  # pwd

  # service httpd stop

  # history | tail -3

  38 pwd

  39 service httpd stop

  40 history | tail -3

  # ls -ltr

  # service httpd stop

  # history | tail -6

  35 export HISTCONTROL=erasedups

  36 pwd

  37 history | tail -3

  38 ls -ltr

  39 service httpd stop

  [Note that the previous service httpd stop after pwd got erased]

  40 history | tail -6

  10.使用 HISTCONTROL 强制 history 不记住特定的命令

  将 HISTCONTROL 设置为 ignorespace,并在不想被记住的命令前面输入一个空格:

  代码如下:

  # export HISTCONTROL=ignorespace

  # ls -ltr

  # pwd

  # service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history]

  # history | tail -3

  67 ls -ltr

  68 pwd

  69 history | tail -3

  11.使用 -c 选项清除所有的命令历史

  如果你想清除所有的命令历史,可以执行:

  代码如下:

  # history -c

  12.命令替换

  在下面的例子里,!!:$ 将为当前的命令获得上一条命令的参数:

  代码如下:

  # ls anaconda-ks.cfg

  anaconda-ks.cfg

  # vi !!:$

  vi anaconda-ks.cfg

  补充:使用 !$ 可以达到同样的效果,而且更简单。

  下例中,!^ 从上一条命令获得第一项参数:

  代码如下:

  # cp anaconda-ks.cfg anaconda-ks.cfg.bak

  anaconda-ks.cfg

  # vi -5 !^

  vi anaconda-ks.cfg

  13.为特定的命令替换指定的参数

  在下面的例子,!cp:2 从命令历史中搜索以 cp 开头的命令,并获取它的第二项参数:

  代码如下:

  # cp ~/longname.txt /really/a/very/long/path/long-filename.txt

  # ls -l !cp:2

  ls -l /really/a/very/long/path/long-filename.txt

  下例里,!cp:$ 获取 cp 命令的最后一项参数:

  代码如下:

  # ls -l !cp:$

  ls -l /really/a/very/long/path/long-filename.txt

  14.使用 HISTSIZE 禁用 history

  如果你想禁用 history,可以将 HISTSIZE 设置为 0:

  代码如下:

  # export HISTSIZE=0

  # history

  # [Note that history did not display anything]

  15.使用 HISTIGNORE 忽略历史中的特定命令

  下面的例子,将忽略 pwd、ls、ls -ltr 等命令:

  代码如下:

  # export HISTIGNORE=”pwd:ls:ls -ltr:”

  # pwd

  # ls

  # ls -ltr

  # service httpd stop

  # history | tail -3

  79 export HISTIGNORE=”pwd:ls:ls -ltr:”

  80 service httpd stop

  81 history

  [Note that history did not record pwd, ls and ls -ltr]

  上面就是Linux下history命令的例子详解了,从这15个例子中你能更深入的了解history命令的实际应用,如果你经常使用命令,相信history命令是你的好帮手。

发表评论

0

没有更多评论了

评论就这些咯,让大家也知道你的独特见解

立即评论

以上留言仅代表用户个人观点,不代表系统之家立场

其他版本软件

热门教程

人气教程排行

Linux系统推荐

官方交流群 软件收录