记录linux历史命令,linux下多个终端命令历史记录保存

在此之前我一直以为linux下使用screen 或者tmux,等终端复用工具的话,命令历史是不会保存的。

可是事实是会保存到~/.bash_history 文件中去,只是历史记录并不是实时保存,只有在当前(session1)回话结束之后(执行了exit命令后) bash才会把历史记录写入到.bash_history文件中。即使已经写入到了.bash_history中,假如某个回话(session2)在写入之前就已经开启了,那么这个回话(session2)中的历史记录并不会实时刷新,也就是说你执行一遍history 打印出来的命令并没有 (session1)中的历史命令

做个测试

首先新建两个回话

Shell

root@local:~# screen -S session2

[detached from 21165.session2]

root@local:~# screen -S session1

[detached from 21178.session1]

root@local:~# screen -list

There are screens on:

21178.session1 (06/07/2013 01:59:18 PM) (Detached)

21165.session2 (06/07/2013 01:59:09 PM) (Detached)

2 Sockets in /var/run/screen/S-root.

1

2

3

4

5

6

7

8

9

root@local:~# screen -S session2

[detachedfrom21165.session2]

root@local:~# screen -S session1

[detachedfrom21178.session1]

root@local:~# screen -list

Therearescreenson:

21178.session1(06/07/201301:59:18PM)(Detached)

21165.session2(06/07/201301:59:09PM)(Detached)

2Socketsin/var/run/screen/S-root.

进入session1

root@local:~# screen -r session1

1

root@local:~# screen -r session1

session1

root@local:~# history 10

1992 ls s13

1993 exit

1994 ls s21

1995 exit

1996 tail -f .bash_history

1997 exit

1998 ls

1999 tail -f .bash_history

2000 exit

2001 history 10

root@local:~# ls s6767

ls: cannot access s6767: No such file or directory

1

2

3

4

5

6

7

8

9

10

11

12

13

root@local:~# history 10

1992lss13

1993exit

1994lss21

1995exit

1996tail-f.bash_history

1997exit

1998ls

1999tail-f.bash_history

2000exit

2001history10

root@local:~# ls s6767

ls:cannotaccesss6767:Nosuchfileordirectory

在进入session2

session2

1999 tail -f .bash_history

2000 exit

2001 history

root@local:~# history 10

1993 exit

1994 ls s21

1995 exit

1996 tail -f .bash_history

1997 exit

1998 ls

1999 tail -f .bash_history

2000 exit

2001 history

2002 history 10

1

2

3

4

5

6

7

8

9

10

11

12

13

14

1999tail-f.bash_history

2000exit

2001history

root@local:~# history 10

1993exit

1994lss21

1995exit

1996tail-f.bash_history

1997exit

1998ls

1999tail-f.bash_history

2000exit

2001history

2002history10

执行history 从第2001 行开始 都是不一样的

再次进入session1 并退出 新建session3 执行history命令(由于history记录数的限制,只能存储2000条历史记录,所以行数发生了变化)第1999行为在session1中执行的命令

session3

1997 exit

1998 history 10

1999 ls s6767

2000 exit

2001 history

1

2

3

4

5

1997exit

1998history10

1999lss6767

2000exit

2001history

进入session2(可以看到没有发生变化)

session2

root@local:~# history 15

1989 ls b2

1990 exit

1991 ls s12

1992 ls s13

1993 exit

1994 ls s21

1995 exit

1996 tail -f .bash_history

1997 exit

1998 ls

1999 tail -f .bash_history

2000 exit

2001 history

2002 history 10

2003 history 15

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

root@local:~# history 15

1989lsb2

1990exit

1991lss12

1992lss13

1993exit

1994lss21

1995exit

1996tail-f.bash_history

1997exit

1998ls

1999tail-f.bash_history

2000exit

2001history

2002history10

2003history15

若想多个回话之间实时共享历史记录,可以试试下面这种方法

~/.bashrc 或 ~/.bash_profile 文件中:

shopt -s histappend

PROMPT_COMMAND=’history -a’

这个是在网上搜到的,我没试因为我觉得还是分开的好些 一般建立多个回话是有多个任务要进行

要是历史记录共享的话就有些乱了

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值