Linux下history命令用法

更新时间:2023-07-20 19:12:00 阅读: 评论:0

Linux下history命令⽤法
如果你经常使⽤ Linux 命令⾏,那么使⽤ history(历史)命令可以有效地提升你的效率。本⽂将通过实例的⽅式向你介绍 history 命令的 15 个⽤法。
1. 使⽤ HISTTIMEFORMAT 显⽰时间戳
当你从命令⾏执⾏ history 命令后,通常只会显⽰已执⾏命令的序号和命令本⾝。如果你想要查看命令历史的时间戳,那么可以执⾏:
# export HISTTIMEFORMAT='%F %T '
# history | more
1 2008-08-05 19:02:39 rvice network restart
2 2008-08-05 19:02:39 exit
3 2008-08-05 19:02:39 id
4 2008-08-0
5 19:02:39 cat /etc/redhat-relea
注意:这个功能只能⽤在当 HISTTIMEFORMAT 这个环境变量被设置之后,之后的那些新执⾏的 bash 命令才会被打上正确的时间戳。在此之前的所有命令,都将会显⽰成设置 HISTTIMEFORMAT 变量的时间。[感谢 NightOwl 读者补充]
2. 使⽤ Ctrl+R 搜索历史
Ctrl+R 是我经常使⽤的⼀个快捷键。此快捷键让你对命令历史进⾏搜索,对于想要重复执⾏某个命令的时候⾮常有⽤。当找到命令后,通常再按回车键就可以执⾏该命令。如果想对找到的命令进⾏调整后再执⾏,则可以按⼀下左或右⽅向键。
# [Press Ctrl+R from the command prompt, which will display the rever-i-arch prompt]
(rever-i-arch)`red‘: cat /etc/redhat-relea
[Note: Press enter when you e your command, which will execute the command from the history]
# cat /etc/redhat-relea
Fedora relea 9 (Sulphur)
3. 快速重复执⾏上⼀条命令
有 4 种⽅法可以重复执⾏上⼀条命令:
1. 使⽤上⽅向键,并回车执⾏。
2. 按 !! 并回车执⾏。
3. 输⼊ !-1 并回车执⾏。
4. 按 Ctrl+P 并回车执⾏。
4. 从命令历史中执⾏⼀个指定的命令
在下⾯的例⼦中,如果你想重复执⾏第 4 条命令,那么可以执⾏ !4:
# history | more
1 rvice network restart
2 exit
3 id
4 cat /etc/redhat-relea
# !4
cat /etc/redhat-relea
Fedora relea 9 (Sulphur)
5. 通过指定关键字来执⾏以前的命令
在下⾯的例⼦,输⼊ !ps 并回车,将执⾏以 ps 打头的命令:
# !ps
ps aux | grep yp
root 16947 0.0 0.1 36516 1264 ? Sl 13:10 0:00 ypbind
root 17503 0.0 0.0 4124 740 pts/0 S+ 19:19 0:00 grep yp
6. 使⽤ HISTSIZE 控制历史命令记录的总⾏数
将下⾯两⾏内容追加到 .bash_profile ⽂件并重新登录 bash shell,命令历史的记录数将变成 450 条:
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450
7. 使⽤ HISTFILE 更改历史⽂件名称
默认情况下,命令历史存储在 ~/.bash_history ⽂件中。添加下列内容到 .bash_profile ⽂件并重新登录 bash shell,将使⽤.commandline_warrior 来存储命令历史:
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
8. 使⽤ HISTCONTROL 从命令历史中剔除连续重复的条⽬
在下⾯的例⼦中,pwd 命令被连续执⾏了三次。执⾏ history 后你会看到三条重复的条⽬。要剔除这些重复的条⽬,你可以将HISTCONTROL 设置为 ignoredups:
# pwd
# pwd
# pwd
# history | tail -4
44 pwd护理质量
45 pwd
46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]
47 history | tail -4
# export HISTCONTROL=ignoredups
# pwd
# pwd判断的意思
# pwd
# history | tail -3
56 export HISTCONTROL=ignoredups
57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above]
58 history | tail -4
9. 使⽤ HISTCONTROL 清除整个命令历史中的重复条⽬
上例中的 ignoredups 只能剔除连续的重复条⽬。要清除整个命令历史中的重复条⽬,可以将 HISTCONTROL 设置成eradups:
# export HISTCONTROL=eradups
# pwd
# rvice httpd stop
# history | tail -3
38 pwd
39 rvice httpd stop
40 history | tail -3
# ls -ltr
# rvice httpd stop
# history | tail -6
35 export HISTCONTROL=eradups
36 pwd
37 history | tail -3
38 ls -ltr
39 rvice httpd stop
[Note that the previous rvice httpd stop after pwd got erad]
40 history | tail -6
10. 使⽤ HISTCONTROL 强制 history 不记住特定的命令
将 HISTCONTROL 设置为 ignorespace,并在不想被记住的命令前⾯输⼊⼀个空格:
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
# rvice httpd stop [Note that there is a space at the beginning of rvice, to ignore this command from history]
# history | tail -3
67 ls -ltr
68 pwd
69 history | tail -3
11. 使⽤ -c 选项清除所有的命令历史
如果你想清除所有的命令历史,可以执⾏:
# history -c
12. 命令替换
在下⾯的例⼦⾥,!!:$ 将为当前的命令获得上⼀条命令的参数:
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
情感口述实录
补充:使⽤ !$ 可以达到同样的效果,⽽且更简单。[感谢 wanzigunzi 读者补充]
下例中,!^ 从上⼀条命令获得第⼀项参数:
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi -5 !^
vi anaconda-ks.cfg
13. 为特定的命令替换指定的参数
在下⾯的例⼦,!cp:2 从命令历史中搜索以 cp 开头的命令,并获取它的第⼆项参数:
# cp ~/ /really/a/very/long/
雷锋精神的故事
# ls -l !cp:2
ls -l /really/a/very/long/
下例⾥,!cp:$ 获取 cp 命令的最后⼀项参数:
# ls -l !cp:$
ls -l /really/a/very/long/
14. 使⽤ HISTSIZE 禁⽤ history
如果你想禁⽤ history,可以将 HISTSIZE 设置为 0:
# export HISTSIZE=0
# history
# [Note that history did not display anything]
15. 使⽤ HISTIGNORE 忽略历史中的特定命令
下⾯的例⼦,将忽略 pwd、ls、ls -ltr 等命令:
# export HISTIGNORE=”pwd:ls:ls -ltr:”
# pwd
# ls
# ls -ltr广东高考试卷
# rvice httpd stop
# history | tail -3
79 export HISTIGNORE=”pwd:ls:ls -ltr:”
80 rvice httpd stop
81 history
[Note that history did not record pwd, ls and ls -ltr]
如果你经常使⽤Linux命令,那么使⽤history命令⽆疑会提升你的⼯作效率。
History命令主要⽤于显⽰历史指令记录内容, 下达历史纪录中的指令。
1>History命令语法:
[test@linux]# history [n]
[test@linux]# history [-c]
[test@linux]# history [-raw] histfiles
葡挞参数:
n  :数字,要列出最近的 n 笔命令列表
-c  :将⽬前的shell中的所有 history 内容全部消除
-a  :将⽬前新增的history 指令新增⼊ histfiles 中,若没有加 histfiles ,
则预设写⼊ ~/.bash_history
-
r  :将 histfiles 的内容读到⽬前这个 shell 的 history 记忆中
-w  :将⽬前的 history 记忆内容写⼊ histfiles
Linux系统当你在shell(控制台)中输⼊并执⾏命令时,shell会⾃动把你的命令记录到历史列表中,⼀般保存在⽤户⽬录下的.bash_history⽂件中。默认保存1000条,你
也可以更改这个值。
如果你键⼊ history, history会向你显⽰你所使⽤的前1000个历史命令,并且给它们编了号,你会看到⼀个⽤数字编号的列表快速从屏幕上卷过。你可能不需要查看1000个命令中的所有项⽬, 当然你也可以加⼊数字来列出最近的 n 笔命令列表。
linux中history命令不仅仅让我们可以查询历史命令⽽已. 我们还可以利⽤相关的功能来帮我们执⾏命令。
2>运⾏特定的历史命令
history会列出bash保存的所有历史命令,并且给它们编了号,我们可以使⽤“叹号接编号”的⽅式运⾏特定的历史命令.
语法说明:
[test@linux]# [!number]  [!command] [!!]
参数说明:
number  :第⼏个指令的意思;
command  :指令的开头⼏个字母
!        :上⼀个指令的意思!
3>History命令实战
列出所有的历史记录:
[test@linux] # history
只列出最近10条记录:
[test@linux] # history 10 (注,history和10中间有空格)
香锅使⽤命令记录号码执⾏命令,执⾏历史清单中的第99条命令
[test@linux] #!99 (!和99中间没有空格)
重复执⾏上⼀个命令
[test@linux] #!!
执⾏最后⼀次以rpm开头的命令(!?  ?代表的是字符串,这个String可以随便输,Shell会从最后⼀条历史命令向前搜索,最先匹配的⼀条命令将会得到执⾏。)
[test@linux] #!rpm
梦见前男友不理我逐屏列出所有的历史记录:
[test@linux]# history | more
⽴即清空history当前所有历史命令的记录
[test@linux] #history -c
除了使⽤history命令,在 shell 或 GUI 终端提⽰下,你也可以使⽤上下⽅向键来翻阅命令历史(向下箭头会向前翻阅),直到你找到所需命令为⽌。这可以让我们很⽅便地编辑前⾯的某⼀条命令,⽽不⽤重复输⼊类似的命令。
History命令的⽤途确实很⼤!但需要⼩⼼安全的问题!尤其是 root 的历史纪录档案,这是⿊客们的最爱!因为不⼩⼼的 root 会将很多的重要资料在执⾏的过程中会被纪录在 ~/.bash_history 当中,如果这个档案被解析的话,后果不堪设想!

本文发布于:2023-07-20 19:12:00,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1107447.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:命令   历史   记录   内容   指令   列表
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图