码迷,mamicode.com
首页 > 其他好文 > 详细

history命令详解

时间:2017-11-14 11:18:46      阅读:536      评论:0      收藏:0      [点我收藏+]

标签:linux

时间:2017.11.13

作者:李强

参考:man,info,magedu讲义

声明:以下英文纯属个人翻译,英文B级,欢迎纠正,盗版不纠,希望不误人子弟。

1、使用目的与场景

    实现快速操作命令的一种方式

2、官方说明

    Display or manipulate the history list.

3、写在前面

    首先这里有两个概念history list和history file。下面会使用到,history list就是memory中的东西,history file就是disk上的东西。history list 针对当前会话生效,修改history file是针对所有用户生效。

当我们通过终端登陆系统后,就会自动将默认history file文件中的命令添加到history list中去,当然logout后history list中的命令就会自动写入到history file中去,就是存盘。流程图就不画了自己意会。

4、修改文件及涉及的环境变量

    文件:

     ~/.bash_history    :历史列表的默认保存位置

     ~/.bash_profile    :当前用户的环境变量的设置位置

     /etc/profile          : 系统环境变量的默认设置位置

    /etc/profile.d/env.sh(这个文件自定义): 手动添加的修改环境变量的设置位置

    系统是啥,系统就是"地主"想做啥做啥。

    变量:

     HISTSIZE:命令历史记录的条数 

    ps.设置的是history list的值,执行history我一次不想看太多命令。

     HISTFILE:指定历史文件,默认为~/.bash_history 

     HISTFILESIZE:命令历史文件记录历史的条数 

     HISTTIMEFORMAT=“%F %T “   显示时间

    ps.和date的format是一样的。 

     HISTIGNORE=“str1:str2*:… “ 忽略str1命令,str2开头的历史

    ps.可以设置忽略已空格开头的命令和下面的HISTCONTROL=ignorespace相同,在命令加空格就不会记录了;匹配是精确匹配,str1会忽略,但str1xx就会记录。

    

     控制命令历史的记录方式:

     环境变量:HISTCONTROL 

    ignoredups 默认,忽略重复的命令,连续且相同为“重复” 

    ignorespace 忽略所有以空白开头的命令

    ignoreboth 相当于ignoredups, ignorespace的组合 

    erasedups 删除重复命令 

    ps.可以用 : 隔开,配合设置,HISTCONTROL=‘ignorespace:ignoredups‘ 

     export 变量名="值“ 

     存放在 /etc/profile 或 ~/.bash_profile

5、用法(不按字母排序)

     history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]

     ps.官方,以下为个人解释。

    -c     清空当前history list缓存

    -d     删除list中的某个命令,后面跟的是number号,就是上面的offset

     n     显示最近n条命令

    -a     append 添加当前会话history list中的命令历史到文件中去

    -n     new   只添加history list中没有从history file中添加过的命令

    -r      read  读取history file中的文件到当前session的history file中

    -w     write  把当前history file中的命令覆盖到history文件中去。

            可以跟上另外的history file做备份,也可以配合 -c 先清空然后再默认不加[filename]

            清空~/.bash_history 文件。

    -p    执行但是不记录到history list中

    -s    会把参数添加到history list中去,但是并不执行


      以上是option选项,以下还有一些bash对history list的快捷操作 History expansions


-------------------------------------------万能的分隔符------------------------------------------------

    The History library provides a history expansion feature that is similar to the history expansion provided by `csh‘.  This section describes the syntax used to manipulate the history information.

    History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.

    History expansion takes place in two parts.  The first is todetermine which line from the history list should be used during substitution.  The second is to select portions of that line for inclusion into the current one.  The line selected from the history iscalled the "event", and the portions of that line that are acted upon are called "words".  Various "modifiers" are available to manipulate the selected words.  The line is broken into words in the same fashion that Bash does, so that several words surrounded by quotes are considered one word.  

    History expansions are introduced by the appearance of the history expansion character, which is `!‘ by default

   以下为本人翻译,凑活着看:

    

    history调用将history list 列表中的单词引入输入流, 使重复命令变得容易, 将参数插入到当前输入行中的上一个命令, 或快速修复以前命令中的错误。

    history调用分成2个部分,第一我们要明确我们在置换的时候选择的是history list的哪一行,第二选择这行的哪个部分去加入到当前命令中。 行的选择我们称为”event“,选择这行的哪一部分称为‘’words‘’

    history的调用默认用!字符引入调用

    执行操作格式如下:

   [command]  !xx:yy   !xx,哪一行命令,中间":"分开,yy 第几个参数

   event Designators标识符,以下指定哪一行被调用。

    

    `!‘    : 开始history调用,除非后面跟spasce,tab,或者`=‘在后面,就不是这个意思了,

    ps.后面要直接加东西的意思,如下面所示必要手残多敲些东西的意思

    `!N‘    :执行第N行命令

    `!-N‘    :执行倒数第N行命令

    `!!‘        :执行刚才或者上一个执行的命令,相当于 `!-1‘  

    `!STRING‘    :从最后一个往前,执行最近的一个以STRING开头的命令

    `!?STRING[?]‘    :从最后一个往前,执行最近的一个包含STRING开头的命令

   # 这里有个快捷键实现上面2个方式,Ctrl +r  向上查找最近的一个以某个字符开头或包含某个字符的命令回车执行,Ctrl +g 取消搜索

    `^STRING`:替换上一条命令的第一个STRING

    `^STRING1^STRING2^‘:快速替换,将上条命令中的STRING1替换成STRING2

                                              相当于`!!:s/STRING1/STRING2/‘

    word Designators标识符

    !!:$‘    :标识前一个命令的最后一个参数可以缩写成 `!$‘

    `!fi:2‘    :表示最近一个以fi开始命令的第二个参数,表示不懂

   Here are the word designators:

    `0 (zero)‘    :第0个单词,对于大部分应用来说是命令本身

    ps. "!:0" 执行前一个命令但是不加参数。

    `N‘    :第N个单词

    `^‘    :  第一个参数,也是第一个单词

    `$‘    :最后一个参数

    `%‘

     The word matched by the most recent `?STRING?‘ search.

    `X-Y‘   一个单词的范围,`-Y‘ 是 `0-Y‘的缩写

    `*‘    :所有的单词,除了第0个也就是命令本身和`1-$‘是相同的意思

    `X*‘    : `X-$‘的缩写

    `X-

    modify

    After the optional word designator, you can add a sequence of one or

more of the following modifiers, each preceded by a `:‘.

    

    `h‘    :移除尾部的路径,保留路径的头

    `t‘    :移除路径的头部,保留尾,比如ls /etc/passwd /etc/motd  echo  !!:t 只有 motd

    `r‘    :移除`.SUFFIX‘后缀,保留文件名

    `e‘    :移除所有保留`.SUFFIX‘后缀

    `p‘    :打印命令但是不执行 ,比如  !4:p 打印第4条命令看下是什么,但是不执行

    `s/OLD/NEW/‘

     Substitute NEW for the first occurrence of OLD in the event line.

     Any delimiter may be used in place of `/‘.  The delimiter may be

     quoted in OLD and NEW with a single backslash.  If `&‘ appears in

     NEW, it is replaced by OLD.  A single backslash will quote the

     `&‘.  The final delimiter is optional if it is the last character

     on the input line.     

    `&‘

     Repeat the previous substitution.

    `g‘

   ps.虽然看不懂但是是这么用的 !:gs/string1/string2 将上一条命令中所有的string1都替换 为 string2

    `a‘

     Cause changes to be applied over the entire event line.  Used in

     conjunction with `s‘, as in `gs/OLD/NEW/‘, or with `&‘.

    `G‘

     Apply the following `s‘ modifier once to each word in the event.

example:

    command !^     : 利用上一个命令的第一个参数做cmd的参数 

    command !$      : 利用上一个命令的最后一个参数做cmd的参数 

    command !*      : 利用上一个命令的全部参数做cmd的参数 

    command !:n     : 利用上一个命令的第n个参数做cmd的参数

    ps. !^  可以理解为"!-1:^"或者"!!:^"的缩写

         !$! 可以理解为"!-1:$"或者"!!:$"的缩写

         !*! 可以理解为"!-1:*"  或者"!!:*"的缩写

        这里就可以理解了!!在加上:之后可以缩写为!,这就是之前的!:0代表上一个命令的意思。

        下面的就好理解了!xx:yy

    

    command !n:^      调用第n条命令的第一个参数 

    command !n:$      调用第n条命令的最后一个参数 

    command !n:n     调用第n条命令的第n个参数 

    command !n:*       调用第n条命令的所有参数

    

    command !string:^     从命令历史中搜索以 string 开头的命令 ,并获取它的第一个参数 

    command !string:$     从命令历史中搜索以 string 开头的命令 ,并获取它的最后一个参数

    command !string:n     从命令历史中搜索以 string 开头的命令 ,并获取它的第n个参数 

    command !string:*     从命令历史中搜索以 string 开头的命令 ,并获取它的所有参数

    

    

    坐等新版本博客2.0上线再上传测试结果,到时候还能复习下,有时间再优化优化排版排版,凑合看吧,顺便说下我不是喜欢绿色,我喜欢的是大自然的颜色技术分享

本文出自 “我是一只小小菜鸟” 博客,请务必保留此出处http://lajifeiwomoshu.blog.51cto.com/5110076/1981505

history命令详解

标签:linux

原文地址:http://lajifeiwomoshu.blog.51cto.com/5110076/1981505

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