Linux命令(94)之history

linux命令之history

1.history介绍

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

2.history用法

history [参数]

|----|-----------------------------------|
| 参数 | 说明 |
| -a | 将当前会话的历史信息追加到历史文件(.bash_history)中 |
| -c | 删除所有条目从而清空历史列表 |
| -d | 从指定位置删除历史列表 |
| -r | 读取历史文件(.bash_history)到缓冲区 |
| -s | 将指定的命令添加到缓冲区 |
| -w | 将缓冲区信息写入到历史文件(.bash_history) |
[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 快捷键从小往上搜索,右方向键确定

第三个方式是: !字符串

相关推荐
傻啦嘿哟1 小时前
Python正则表达式:用“模式密码“解锁复杂字符串
linux·数据库·mysql
浪裡遊2 小时前
Linux常用指令
linux·运维·服务器·chrome·功能测试
SugarPPig3 小时前
PowerShell 查询及刷新环境变量
服务器
段ヤシ.3 小时前
银河麒麟(内核CentOS8)安装rbenv、ruby2.6.5和rails5.2.6
linux·centos·银河麒麟·rbenv·ruby2.6.5·rails 5.2.6
深夜情感老师5 小时前
centos离线安装ssh
linux·centos·ssh
我的作业错错错5 小时前
搭建私人网站
服务器·阿里云·私人网站
王景程6 小时前
如何测试短信接口
java·服务器·前端
微网兔子6 小时前
伺服器用什么语言开发呢?做什么用什么?
服务器·c++·后端·游戏
夸克App7 小时前
实现营销投放全流程自动化 超级汇川推出信息流智能投放产品“AI智投“
运维·人工智能·自动化
Rainbond云原生7 小时前
83k Star!n8n 让 AI 驱动的工作流自动化触手可及
运维·人工智能·自动化