歡迎您光臨本站 註冊首頁

history?如何提高效率

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

history?如何提高效率

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是只命令的序號,下面就是執行第四行命令# 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

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
#
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
《解決方案》

謝謝分享

[火星人 ] history?如何提高效率已經有640次圍觀

http://coctec.com/docs/service/show-post-1185.html