linux history

本文详细介绍Linux环境下history命令的功能、语法及使用方法,并通过多个实际案例展示如何查询、修改历史记录,以及如何执行历史命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux命令总结—history命令

(1)命令功能

history命令用于显示指定数目的指令命令,读取历史命令文件中的目录到历史命令缓冲区和将历史命令缓冲区中的目录写入历史命令文件。

说明:bash启动的时候会读取~/.bash_history文件并载入到内存中,这个变量就用于设置.bash_history文件,bash退出时也会把内存中的历史回写到.bash_history文件,即history中显示的命令只保存在当前历史命令缓冲区中,只有退出登陆后系统才会把缓冲区的内容写入到历史命令文件中,当然也可以使用下面的命令参数实现。

(2)命令语法

history(选项) (参数)

(3)选项说明

  • -c:清空当前历史命令;

  • -d <行号>:清除指定行号的历史输入命令;

  • -a:将历史命令缓冲区中的命令写入历史命令文件中;

  • -r:将历史命令文件中的命令读入当前历史命令缓冲区;

  • -w:将当前历史命令缓冲区命令写入历史命令文件中。

(4)参数说明

  • n:打印最近的n条历史命令。

(5)实例

实例1:显示历史命令—history 数字

1
2
3
4
5
6
7
8
9
10
11
[root@moban ~]#history 10
  991 alias
  992 alias -p
  993 man cd
  994 unalias network
  995 type network
  996 alias network='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
  997 unalias network
  998 type network
  999 man history
 1000 history 10

       实例2:清除指定行号历史输入命令—history -d 行号

1
2
3
4
5
6
7
[root@moban ~]#history -d 1000
[root@moban ~]#history 5
  997 unalias network
  998 type network
  999 man history
 1000 history -d 1000
 1001 history 5

可以看到实例1中的历史输入命令“1000  history 10”已被清除。

实例3:执行指定行号的历史输入命令--! 行号

1
2
3
4
5
6
7
8
9
[root@moban ~]#history 5
  999 man history
 1000 history -d 1000
 1001 history 5
 1002 pwd
 1003 history 5
[root@moban ~]#!1002
pwd
/root

实例4:运行上一个输入命令— !!

1
2
3
4
5
[root@moban ~]# pwd
/root
[root@moban ~]# !!
pwd
/root

实例5:将历史命令缓冲区中的命令写入历史命令文件中—history -a

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@moban ~]# tail -10 .bash_history
cat .bash_history 
more .bash_history
history -5
history 5
pwd
history 5
pwd
ls -a
sz .bash_history
init  6
[root@moban ~]#history -a
[root@moban ~]# tail -10 .bash_history
pwd
ls -a
sz .bash_history
init  6
history 3
history 10
sy -y .bash_history
sz -y .bash_history
tail -10.bash_history
history -a
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值