• Linux命令(94)之history


    linux命令之history

    1.history介绍

    linux命令history会记录并显示用户所执行过的所有命令,也可以对其命令进行修改和删除操作。

    2.history用法

    history [参数]

    history参数
    参数说明
    -a将当前会话的历史信息追加到历史文件(.bash_history)中
    -c删除所有条目从而清空历史列表
    -d从指定位置删除历史列表
    -r读取历史文件(.bash_history)到缓冲区
    -s将指定的命令添加到缓冲区
    -w将缓冲区信息写入到历史文件(.bash_history)

    3.实例

    3.1.将当前会话的历史信息追加到历史文件(.bash_history)中

    命令:

    history -a

    1. [root@centos79-3 ~]# ping 1.1.1.1
    2. PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
    3. 64 bytes from 1.1.1.1: icmp_seq=1 ttl=128 time=247 ms
    4. 64 bytes from 1.1.1.1: icmp_seq=2 ttl=128 time=361 ms
    5. ^C
    6. --- 1.1.1.1 ping statistics ---
    7. 2 packets transmitted, 2 received, 0% packet loss, time 1014ms
    8. rtt min/avg/max/mdev = 247.939/304.742/361.545/56.803 ms
    9. [root@centos79-3 ~]# ping 2.2.2.2
    10. PING 2.2.2.2 (2.2.2.2) 56(84) bytes of data.
    11. ^C
    12. --- 2.2.2.2 ping statistics ---
    13. 3 packets transmitted, 0 received, 100% packet loss, time 2007ms
    14. [root@centos79-3 ~]# history -a
    15. [root@centos79-3 ~]# tail -f .bash_history
    16. #1695879396
    17. cat .bash_history
    18. #1695879406
    19. clear
    20. #1695879409
    21. ping 1.1.1.1
    22. #1695879414
    23. ping 2.2.2.2
    24. #1695879420
    25. history -a
    26. [root@centos79-3 ~]#

    3.2.从指定位置删除历史列表

    命令:

    history -d 3

    1. [root@centos79-3 ~]# history | grep 1289
    2. 1289 2023-09-28 13:54:07 root set +o history
    3. 1313 2023-09-28 14:05:36 root history | grep 1289
    4. [root@centos79-3 ~]# history -d 1289
    5. [root@centos79-3 ~]# history | grep 1289
    6. 1289 2023-09-28 13:55:16 root history
    7. 1312 2023-09-28 14:05:36 root history | grep 1289
    8. 1313 2023-09-28 14:05:46 root history -d 1289
    9. 1314 2023-09-28 14:05:50 root history | grep 1289
    10. [root@centos79-3 ~]#

    3.3.删除所有条目从而清空历史列表

    命令:

    history -c

    1. [root@centos79-3 ~]# history -c
    2. [root@centos79-3 ~]#

    3.4.读取历史文件(.bash_history)到缓冲区

    命令:

    history -r

    1. [root@centos79-3 ~]# history -r
    2. [root@centos79-3 ~]# tail -n 4 .bash_history
    3. #1695879420
    4. history -a
    5. #1695879044
    6. ifconfig
    7. [root@centos79-3 ~]#

    3.5.将缓冲区信息写入到历史文件(.bash_history)

    命令:

    history -w

    1. [root@centos79-3 ~]# history -w
    2. [root@centos79-3 ~]# tail -n 4 .bash_history
    3. #1695880068
    4. clear
    5. #1695880071
    6. history -w
    7. [root@centos79-3 ~]#

    3.6.不记录已执行的历史命令

    命令:

    set +o history

    1. [root@centos79-3 ~]# set +o history
    2. [root@centos79-3 ~]# ping 111.111.111.111
    3. PING 111.111.111.111 (111.111.111.111) 56(84) bytes of data.
    4. ^C
    5. --- 111.111.111.111 ping statistics ---
    6. 3 packets transmitted, 0 received, 100% packet loss, time 2021ms
    7. [root@centos79-3 ~]# tail -n 1 .bash_history
    8. set +o history
    9. [root@centos79-3 ~]#

    3.7.取消不记录已执行的历史命令

    命令:

    set -o history

    1. [root@centos79-3 ~]# history | tail -n 6
    2. 1302 2023-09-28 13:55:10 root ping 16.16.16.16
    3. 1303 2023-09-28 13:55:16 root history
    4. 1304 2023-09-28 13:55:28 root cat .bash_history
    5. 1305 2023-09-28 13:55:42 root history
    6. 1306 2023-09-28 13:55:51 root history | tail -n 4
    7. 1307 2023-09-28 13:55:58 root history | tail -n 6
    8. [root@centos79-3 ~]#

    另外:

    再次执行历史记录命令的三种方式:

    第一个方式是:!数字

    第二个方式是: ctrl+r 快捷键从小往上搜索,右方向键确定

    第三个方式是: !字符串

  • 相关阅读:
    这些年写过的花式sql 第2句 统计用户返佣金排名
    视频孪生智慧水利的十大场景应用案例
    TP-Link家用路由器上网与防蹭网
    Element UI定义方法校验邮箱格式
    临沂ISO食品安全管理体系认证
    如何分析这个电路的逻辑功能
    数据结构系列-堆排序
    Web3中文|元宇宙在商业中的最佳应用
    umich cv-5-2 神经网络训练2
    深度学习中的子空间、线性变换和矩阵概念应用
  • 原文地址:https://blog.csdn.net/z19861216/article/details/133378177