04与history命令有关的一些总结

与history命令有关的一些总结

  1. 有关.bash_history文件

    • 文件位置: ~/.bash_history

    • bash中默认命令记忆可达1000个。这些命令保存在主文件夹内的.bash_history中。

    • 记录的是上一次登录所执行过的命令。至于本次登录的命令暂时存储在内存中,注销成功后会写入文件中。

      也就是最近执行的命令可以使用history打印出来,但是并没有保存在.bash_history中。

  2. history 命令

    • history [#] : 列出最近的#条命令,不带#时,为列出全部

      # 列出最近的三条命令
        qwe@ubuntu:~$ history 3
           81  sudo apt install vim
           82  vim ~/.hash_history
           83  history 3
      
    • history -c

      # 将目前shell中的所有history命令消除。对命令历史文件没有影响
        qwe@ubuntu:~$ history -c
        qwe@ubuntu:~$ # 执行history -c后无法再按 ↑ 得到之前输入的命令
      
    • history -w

      # 将本次登录的命令写入命令历史文件中, 默认写入~/.bash_history
        qwe@ubuntu:~$ history -c
      
    • history -r

      # 将命令历史文件中的内容读入到目前shell的history记忆中
        qwe@ubuntu:~$ history -r
      
  3. 使用! 执行历史命令

    执行以下例子前,执行history所得结果如下:

    qwe@ubuntu:/home$ history
        1  history
        2  ls
        3  pwd
        4  cd qwe/
        5  cd ..
        6  pwd
        7  history
    
    
    • !number

      # !number 执行第几条命令(注意!和number之间没有空格)
      qwe@ubuntu:/home$ !3
      pwd
      /home
      #执行第序号为3的命令(也就是pwd)
      
    • ! command

      # 从最近的命令查到以command开头的命令执行,此例为 l 开头的第一个也就是 ls 
      qwe@ubuntu:/home$ !l
      ls
      qwe
      
      
    • !!

      # 执行上一条命令
      qwe@ubuntu:/home$ !!
      ls
      qwe
      
      
posted @ 2020-11-02 09:43  TR_Goldfish  阅读(117)  评论(0编辑  收藏  举报