Linux命令之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

[root@centos79-3 ~]# ping 1.1.1.1
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=128 time=247 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=128 time=361 ms
^C
--- 1.1.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1014ms
rtt min/avg/max/mdev = 247.939/304.742/361.545/56.803 ms
[root@centos79-3 ~]# ping 2.2.2.2
PING 2.2.2.2 (2.2.2.2) 56(84) bytes of data.
^C
--- 2.2.2.2 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2007ms

[root@centos79-3 ~]# history -a
[root@centos79-3 ~]# tail -f .bash_history

1695879396

cat .bash_history

1695879406

clear

1695879409

ping 1.1.1.1

1695879414

ping 2.2.2.2

1695879420

history -a
[root@centos79-3 ~]#

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

history -d 3

[root@centos79-3 ~]# history | grep 1289
1289 2023-09-28 13:54:07 root set +o history
1313 2023-09-28 14:05:36 root history | grep 1289
[root@centos79-3 ~]# history -d 1289
[root@centos79-3 ~]# history | grep 1289
1289 2023-09-28 13:55:16 root history
1312 2023-09-28 14:05:36 root history | grep 1289
1313 2023-09-28 14:05:46 root history -d 1289
1314 2023-09-28 14:05:50 root history | grep 1289
[root@centos79-3 ~]#
3.3.删除所有条目从而清空历史列表
命令:

history -c

[root@centos79-3 ~]# history -c
[root@centos79-3 ~]#
3.4.读取历史文件(.bash_history)到缓冲区
命令:

history -r

[root@centos79-3 ~]# history -r
[root@centos79-3 ~]# tail -n 4 .bash_history

1695879420

history -a

1695879044

ifconfig
[root@centos79-3 ~]#
3.5.将缓冲区信息写入到历史文件(.bash_history)
命令:

history -w

[root@centos79-3 ~]# history -w
[root@centos79-3 ~]# tail -n 4 .bash_history

1695880068

clear

1695880071

history -w
[root@centos79-3 ~]#
3.6.不记录已执行的历史命令
命令:

set +o history

[root@centos79-3 ~]# set +o history
[root@centos79-3 ~]# ping 111.111.111.111
PING 111.111.111.111 (111.111.111.111) 56(84) bytes of data.
^C
--- 111.111.111.111 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2021ms
[root@centos79-3 ~]# tail -n 1 .bash_history
set +o history
[root@centos79-3 ~]#
3.7.取消不记录已执行的历史命令
命令:

set -o history

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

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

第一个方式是:!数字

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

第三个方式是: !字符串
————————————————
版权声明:本文为CSDN博主「小黑要上天」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z19861216/article/details/133378177

目录
相关文章
|
28天前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
104 16
|
20天前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
|
3月前
|
监控 Linux 网络安全
Linux命令大全:从入门到精通
日常使用的linux命令整理
669 13
|
4月前
|
Linux 网络安全 数据安全/隐私保护
使用Linux系统的mount命令挂载远程服务器的文件夹。
如此一来,你就完成了一次从你的Linux发车站到远程服务器文件夹的有趣旅行。在这个技术之旅中,你既探索了新地方,也学到了如何桥接不同系统之间的距离。
558 21
|
4月前
|
JSON 自然语言处理 Linux
linux命令—tree
tree是一款强大的Linux命令行工具,用于以树状结构递归展示目录和文件,直观呈现层级关系。支持多种功能,如过滤、排序、权限显示及格式化输出等。安装方法因系统而异常用场景包括:基础用法(显示当前或指定目录结构)、核心参数应用(如层级控制-L、隐藏文件显示-a、完整路径输出-f)以及进阶操作(如磁盘空间分析--du、结合grep过滤内容、生成JSON格式列表-J等)。此外,还可生成网站目录结构图并导出为HTML文件。注意事项:使用Tab键补全路径避免错误;超大目录建议限制遍历层数;脚本中推荐禁用统计信息以优化性能。更多详情可查阅手册mantree。
linux命令—tree
|
2月前
|
Linux 网络安全 开发工具
技术栈:这50条最常用的 Linux 命令你一定要会!
建议多在终端中实践,遇到不懂的命令就用 man 或 --help 了解详情!
399 0
|
4月前
|
监控 Linux
Linux系统中使用df命令详解磁盘使用情况。
`df`命令是Linux系统管理员和用户监控和管理磁盘空间使用的重要工具。掌握它的基本使用方法和选项可以帮助在必要时分析和解决空间相关问题。简洁但功能丰富,`df`命令确保了用户可以快速有效地识别和管理文件系统的空间使用情况。
269 13
|
2月前
|
安全 Linux Shell
Linux系统中sudo命令的高效运用技巧。
用户可以通过sudo -l来列出自己目前可执行的命令列表,这有助于用户了解自己的权限范围。
123 0
|
2月前
|
监控 Linux Shell
linux命令
常用 Linux 命令汇总
|
4月前
|
Unix Linux
linux命令—cd
`cd` 命令是 Linux/Unix 系统中用于切换工作目录的基础命令。支持相对路径与绝对路径,常用选项如 `-L` 和 `-P` 分别处理符号链接的逻辑与物理路径。实际操作中,可通过 `cd ..` 返回上级目录、`cd ~` 回到家目录,或利用 `cd -` 在最近两个目录间快速切换。结合 Tab 补全和 `pwd` 查看当前路径,能显著提升效率。此外,需注意特殊字符路径的正确引用及脚本中绝对路径的优先使用。