History命令用法15例
5078 ワード
history(履歴)コマンドを使用すると、効率が向上します.ここでは、historyコマンドの15の使い方を例に挙げて説明します.
HISTTIMEFORMATを使用したタイムスタンプの表示
コマンドラインからhistoryコマンドを実行すると、通常は実行済みコマンドのシーケンス番号とコマンド自体しか表示されません.コマンド履歴のタイムスタンプを表示するには:
# 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
Ctrl+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)
前のコマンドをすばやく繰り返し実行
前のコマンドを繰り返し実行するには、次の4つの方法があります.
アップ矢印キーを使用して、車に戻って実行します.
押せ!車に戻って実行します.
入力!-1車に戻って実行します.
Ctrl+Pを押して、車に戻って実行します.
コマンド履歴から指定したコマンドを実行
次の例では、4番目のコマンドを繰り返し実行したい場合は、実行できます.4:
# history | more
1 service network restart
2 exit
3 id
4 cat/etc/redhat-release
# !4
cat/etc/redhat-release
Fedora release 9 (Sulphur)
キーワードを指定して以前のコマンドを実行
次の例では、入力!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
ヒストリコマンドレコードの合計行数をHISTSIZEで制御する
以下の2行の内容を追加する.bash_profileファイルはbash shellに再ログインし、コマンド履歴のレコード数は450件になります.
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450
HISTFILEを使用して履歴ファイル名を変更する
デフォルトでは、コマンド履歴は~/.bash_historyファイルにあります.次の内容を追加します.bash_profileファイルはbash shellに再ログインし、使用します.commandline_warriorはコマンド履歴を格納します.
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
HISTCONTROOLを使用して、コマンド履歴から連続して繰り返されるエントリを削除します.
次の例では、pwdコマンドが3回連続して実行される.historyを実行すると、3つの重複するエントリが表示されます.重複するエントリを削除するには、HISTCONTROOLを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
HISTCONTROOLを使用して、コマンド履歴全体の重複エントリをクリアします.
前の例のignoredupsでは、連続した重複エントリのみが削除されます.コマンド履歴全体の重複エントリをクリアするには、HISTCONTROOLを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
HISTCONTROOLでhistoryに特定のコマンドを覚えさせない
HISTCONTROOLをignorespaceに設定し、覚えたくないコマンドの前にスペースを入力します.
-cオプションを使用してすべてのコマンド履歴をクリア
すべてのコマンド履歴をクリアしたい場合は、次の操作を行います.
# history -c
コマンド置換
次の例では、!!:$現在のコマンドについて、前のコマンドのパラメータが取得されます.
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
次の例では、!^前のコマンドから最初のパラメータを取得します.
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi -5 !^
vi anaconda-ks.cfg
指定したパラメータを特定のコマンドに置き換える
次の例では、!cp:2コマンド履歴からcpで始まるコマンドを検索し、2番目のパラメータを取得します.
# 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
次の例では、!cp:$cpコマンドの最後のパラメータを取得します.
# ls -l !cp:$
ls -l/really/a/very/long/path/long-filename.txt
HISTSIZEでhistoryを無効にする
historyを無効にするには、HISTSIZEを0に設定します.
# export HISTSIZE=0
# history
# [Note that history did not display anything]
ヒストリ内の特定のコマンドを無視するにはHISTIGNOREを使用します.
次の例では、pwd、ls、ls-ltrなどのコマンドは無視されます.
# 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]
HISTTIMEFORMATを使用したタイムスタンプの表示
コマンドラインからhistoryコマンドを実行すると、通常は実行済みコマンドのシーケンス番号とコマンド自体しか表示されません.コマンド履歴のタイムスタンプを表示するには:
# 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
Ctrl+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)
前のコマンドをすばやく繰り返し実行
前のコマンドを繰り返し実行するには、次の4つの方法があります.
アップ矢印キーを使用して、車に戻って実行します.
押せ!車に戻って実行します.
入力!-1車に戻って実行します.
Ctrl+Pを押して、車に戻って実行します.
コマンド履歴から指定したコマンドを実行
次の例では、4番目のコマンドを繰り返し実行したい場合は、実行できます.4:
# history | more
1 service network restart
2 exit
3 id
4 cat/etc/redhat-release
# !4
cat/etc/redhat-release
Fedora release 9 (Sulphur)
キーワードを指定して以前のコマンドを実行
次の例では、入力!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
ヒストリコマンドレコードの合計行数をHISTSIZEで制御する
以下の2行の内容を追加する.bash_profileファイルはbash shellに再ログインし、コマンド履歴のレコード数は450件になります.
# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450
HISTFILEを使用して履歴ファイル名を変更する
デフォルトでは、コマンド履歴は~/.bash_historyファイルにあります.次の内容を追加します.bash_profileファイルはbash shellに再ログインし、使用します.commandline_warriorはコマンド履歴を格納します.
# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior
HISTCONTROOLを使用して、コマンド履歴から連続して繰り返されるエントリを削除します.
次の例では、pwdコマンドが3回連続して実行される.historyを実行すると、3つの重複するエントリが表示されます.重複するエントリを削除するには、HISTCONTROOLを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
HISTCONTROOLを使用して、コマンド履歴全体の重複エントリをクリアします.
前の例のignoredupsでは、連続した重複エントリのみが削除されます.コマンド履歴全体の重複エントリをクリアするには、HISTCONTROOLを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
HISTCONTROOLでhistoryに特定のコマンドを覚えさせない
HISTCONTROOLをignorespaceに設定し、覚えたくないコマンドの前にスペースを入力します.
# 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
-cオプションを使用してすべてのコマンド履歴をクリア
すべてのコマンド履歴をクリアしたい場合は、次の操作を行います.
# history -c
コマンド置換
次の例では、!!:$現在のコマンドについて、前のコマンドのパラメータが取得されます.
# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg
次の例では、!^前のコマンドから最初のパラメータを取得します.
# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi -5 !^
vi anaconda-ks.cfg
指定したパラメータを特定のコマンドに置き換える
次の例では、!cp:2コマンド履歴からcpで始まるコマンドを検索し、2番目のパラメータを取得します.
# 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
次の例では、!cp:$cpコマンドの最後のパラメータを取得します.
# ls -l !cp:$
ls -l/really/a/very/long/path/long-filename.txt
HISTSIZEでhistoryを無効にする
historyを無効にするには、HISTSIZEを0に設定します.
# export HISTSIZE=0
# history
# [Note that history did not display anything]
ヒストリ内の特定のコマンドを無視するにはHISTIGNOREを使用します.
次の例では、pwd、ls、ls-ltrなどのコマンドは無視されます.
# 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]