- 论坛徽章:
- 0
|
history?如何提高效率
摘要:本文详细给出了关于linux命令history的15个例子,能够提高我们的效率
history?是的,就是它,可能大多数管理员都不屑一顾,但是请看下面的15个例子,学会了能极大的提高生产力!
1. 用HISTTIMEFORMAT显示命令执行时间# export HISTTIMEFORMAT=' %F %T '
# history | more
1 2008-08-05 19:02:39 service network restart
2 2008-08-05 19:02:39 exit
3 2008-08-05 19:02:39 id
4 2008-08-05 19:02:39 cat /etc/redhat-release译者注:这条命令应该在bash 3.0版本以上可以使用,另外你可以在/etc/profile中添加这个命令。
2. 用Control+R查询命令
输入ctrl+R,然后输入你曾经输入过命令的关键字,就可以迅速找到输入的命令,回车可以重新执行。# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`red‘: cat /etc/redhat-release
[Note: Press enter when you see your command,
which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)如果你需要修改选中的命令,可以敲-》箭头,选中命令,修改后执行。# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`httpd‘: service httpd stop
[Note: Press either left arrow or right arrow key when you see your
command, which will display the command for you to edit, before executing it]
# service httpd start3. 重复执行刚刚执行过的命令
1. 上箭头,浏览执行过的命令,回车执行
2. !! 上一条命令
3. Control+P 上一条命令
4. 在历史命令清单中执行指定的命令
![n] n是只命令的序号,下面就是执行第四行命令# history | more
1 service network restart
2 exit
3 id
4 cat /etc/redhat-release
# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)5. 执行以指定字符开头的命令
!{aaa} 这个命令将执行命令清单中以aaa开头的命令行
# !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限制命令清单的长度
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450
7. 用HISTFILE改变历史命令清单的位置
默认历史清单记录在 ~/.bash_history
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
If you have a good reason to change the name of the history file, please share it with me, as I’m interested in finding out how you are using this feature.
8. 用HISTCONTROL去掉连续重复输入的命令
设置histcontrol可以去掉重复输入的命令
# 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去除整个命令清单中重复的命令
设置the HISTCONTROL为erasedups,可以去除整个命令清单中的重复输入.注:这个通常还是别用了。
# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38 pwd
39 service httpd stop
40 history | tail -3# ls -ltr
# service httpd stop
# history | tail -6
35 export HISTCONTROL=erasedups
36 pwd
37 history | tail -3
38 ls -ltr
39 service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40 history | tail -6
10. 控制HISTCONTROL不记录指定的命令
比如下行的命令就设置不记录以空格开头的命令
# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
# service httpd stop [Note that there is a space at the beginning of service,
to ignore this command from history]
# history | tail -3
67 ls -ltr
68 pwd
69 history | tail -3
11. 清理历史记录
# history -c
12. 获取迁移命令的参数
!! 返回前一命令的参数
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!
vi anaconda-ks.cfg
!^ 返回前一命令的第一个参数
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi !^
vi anaconda-ks.cfg
13. Substitute a specific argument for a specific command.
In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.
# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt
In the example below, !cp searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.
# ls -l !cp
ls -l /really/a/very/long/path/long-filename.txt
14. 设置HISTSIZE为零,不记录历史清单
# export HISTSIZE=0
# history
# [Note that history did not display anything]
15. 用HISTIGNORE不记录指定的命令
# export HISTIGNORE=”pwd:ls:ls -ltr:”
# pwd
# ls
# ls -ltr
# service httpd stop
# history | tail -3
79 export HISTIGNORE=”pwd:ls:ls -ltr:”
80 service httpd stop
81 history
[Note that history did not record pwd, ls and ls -ltr] |
|