Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2339965
  • 博文数量: 2110
  • 博客积分: 18861
  • 博客等级: 上将
  • 技术积分: 24420
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-05 18:23
文章分类

全部博文(2110)

文章存档

2011年(139)

2010年(1971)

我的朋友

分类: LINUX

2010-10-27 10:52:47

 

  使用 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

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

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

  # history -c

  命令替换

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

  # ls anaconda-ks.cfg

  anaconda-ks.cfg

  # vi !!:$

  vi anaconda-ks.cfg

  补充:使用 !$ 可以达到同样的效果,而且更简单。[感谢 wanzigunzi 读者补充]

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

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

  anaconda-ks.cfg

  # vi -5 !^

  vi anaconda-ks.cfg

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

  在下面的例子,!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

  使用 HISTSIZE 禁用 history

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

  # export HISTSIZE=0

  # history

  # [Note that history did not display anything]

  使用 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]

  bash的设置

  运行 set|grep HISTFILE,默认的历史操作记录文件是 .bash_history

  在.bash_profile 添加

  HISTFILE=/root/test

  export   HISTFILE

  重新登录,会发现已经把记录写道/root/test 了。

  其他设置都写在.bashrc可实现:

  # 忽略重复的命令

  export HISTCONTROL=ignoredups

  # 忽略由冒号分割的这些命令

  export HISTIGNORE="[   ]*:&:bg:fg:exit"

  # 设置保存历史命令的文件大小

  export HISTFILESIZE=1000000000

  # 保存历史命令条数

  export HISTSIZE=1000000

  由于bash的history文件默认是覆盖,如果存在多个终端,最后退出的会覆盖以前历史记录,改为追加形式:

  shopt -s histappend

  实时写入,而不是退出shell才写入的方法:

  PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

阅读(241) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~