Linux查看历史命令

2023年7月19日 发表评论
腾讯云正在大促:点击直达 阿里云超级红包:点击领取
免费/便宜/高性价比服务器汇总入口(已更新):点击这里了解

Linux查看历史命令

在Linux系统中当你执行命令之后系统会自动保存你此次执行的命令,当你再次使用此命令时不必输入命令直接从历史命令中直接调用,下面良许教程网为大家分享一下linux查看历史命令。

1. history作用

linux的history命令的作用是,记录执行过的命令。

用法:

history [n]  n为数字,列出最近的n条命令

-c  将目前shell中的所有history命令消除

history [-raw] histfiles

-a 将目前新增的命令写入histfiles, 默认写入~/.bash_history

-r  将histfiles内容读入到目前shell的history记忆中

-w 将目前history记忆的内容写入到histfiles

 shell > history      34  14-10-28 16:19:24 ll      35  14-10-28 16:19:26 vim test      36  14-10-28 16:19:33 ll      37  14-10-28 16:19:34 ll
     shell > history 3          1032  14-11-02 16:10:41 history          1033  14-11-02 16:10:46 history |more          1034  14-11-02 16:11:15 history 3

使用! 执行历史命令。

! number 执行第几条命令

! command 从最近的命令查到以command开头的命令执行

!! 执行上一条

 shell > !1046   history 3    1045  14-11-02 16:22:38 head ~/.bash_history    1046  14-11-02 16:35:37 history 3    1047  14-11-02 16:35:48 history 3   shell > !!   history 3    1046  14-11-02 16:35:37 history 3    1047  14-11-02 16:35:48 history 3    1048  14-11-02 16:35:52 history 3   shell > !head   head ~/.bash_history   #1414484377   cd update/   #1414484377

2. history配置修改

history记录的行数

 shell > echo $HISTSIZE   1000   shell >

默认记录1000行

配置文件在/etc/profile中修改

 HISTSIZE=1000   export  HISTSIZE

历史命令文件记录在 ~/.bash_history中

想要让linux的history命令显示时间,history是默认不带时间,

在/etc/profile 中增加

 export HISTTIMEFORMAT="%y-%m-%d %H:%M:%S "

查看.bash_history

 shell > head ~/.bash_history   #1414484377   cd update/   #1414484377   ll   #1414484388   vim address   #1414484439   ll   #1414484440   ll   shell >

3. 同一账号同时多次登录写入history

普通情况下, 当以bash登录系统时,系统会从~/.bash_history读取以前运行的命令

当注销时,把最新的1000(HISTSIZE)条命令更新到~/.bash_history文件中。

也可以使用history -w强制立刻写入,仅保留最新的。

当同一账号,同时登录多个bash时,只有最后一个退出的会写入bash_history,其他的都被覆盖了。

4. Ctrl+r 反向查询历史命令

使用Ctrl+r反向查询历史命令,将匹配的最新一条显示出来

如果还想继续向上查询,继续按Ctrl+r

 shell > history 3                                          1048  14-11-02 16:35:52 history 3    1049  14-11-02 16:36:11 head ~/.bash_history    1050  14-11-02 16:41:05 history 3   (reverse-i-search)`his': head ~/.bash_history

至此关于linux查看历史命令教程分享结束,大家有任何问题都可以通过评论区将问题提交给我们。

以上就是良许教程网为各位朋友分享的Linux系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你!

本文来源:www.lxlinux.net/1777.html,若引用不当,请联系修改。

1、腾讯云优惠活动汇总【点此直达
2、阿里云优惠活动大全【点击直达
3、游戏服务器:自建幻兽帕鲁服务器不卡【点击进入教程页 】一键部署!
4、免费服务器:雨云【 点击直达 】了解.
10年老程序吐血建议:云产品续费贵,【点此 】抢腾讯云3年5年服务器,升级贵,选配置高的,新人首购便宜,不要浪费新人首购资格,一次到位,后期少花钱、少折腾。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: