GNU/Linux >> Linux の 問題 >  >> Linux

Linuxコマンドラインの歴史をマスターするための15の例

Linux コマンド ラインを頻繁に使用する場合、履歴を効果的に使用すると生産性が大幅に向上します。実際、ここで紹介した 15 の例をマスターすれば、コマンド ラインをより楽しく楽しく使用できるようになります。

1. HISTTIMEFORMAT を使用してタイムスタンプを表示

通常、コマンド ラインから history を入力すると、command# とコマンドが表示されます。監査の目的で、以下に示すように、コマンドとともにタイムスタンプを表示すると有益な場合があります。

# 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

2. Control+R を使用して履歴を検索

これは、履歴で最も頻繁に使用される機能であると強く信じています。非常に長いコマンドを既に実行した場合は、キーワードを使用して履歴を検索するだけで、同じコマンドを完全に入力しなくても再実行できます。 Ctrl+R を押してキーワードを入力 .次の例では、red を検索しました 、前のコマンド「cat /etc/redhat-release」を表示します 赤という単語を含む歴史の中で」。

# [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)

コマンドを実行する前に、履歴からコマンドを編集したい場合があります。たとえば、 httpd を検索できます service httpd stop が表示されます コマンド履歴からこのコマンドを選択し、ストップをスタートに変更 以下に示すように、もう一度実行してください。

# [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 start

3. 4 つの異なる方法を使用して、前のコマンドをすばやく繰り返します

さまざまな理由で、前のコマンドを繰り返すことになる場合があります。以下は、最後に実行されたコマンドを繰り返す 4 つの異なる方法です。

<オール>
  • 上矢印を使用 前のコマンドを表示し、Enter キーを押して実行します。
  • タイプ !! コマンドラインからEnterキーを押します
  • !-1 と入力 コマンド ラインから Enter キーを押します。
  • Ctrl+P を押します 前のコマンドが表示されます。Enter キーを押して実行します
  • 4.履歴から特定のコマンドを実行

    次の例では、コマンド #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)

    5.特定の単語で始まる前のコマンドを実行

    タイプ !その後に、再実行するコマンドの最初の数文字を続けます。次の例では、!ps と入力して Enter を入力すると、ps で始まる前のコマンドが実行されます。 grep yp’.

    # !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 を使用して履歴の合計行数を制御します

    次の 2 行を .bash_profile に追加し、bash シェルに再度ログインして変更を確認します。この例では、450 コマンドのみが bash 履歴に保存されます。

    # vi ~/.bash_profile
    HISTSIZE=450
    HISTFILESIZE=450

    7. HISTFILE を使用して履歴ファイル名を変更します

    デフォルトでは、履歴は ~/.bash_history に保存されます ファイル。次の行を .bash_profile に追加し、bash シェルに再ログインして、履歴コマンドを .bash_history ファイルではなく .commandline_warrior ファイルに保存します。私はまだこれの実用的な使い方を理解していません。異なる履歴ファイル名を使用して、異なる端末から実行されたコマンドを追跡したい場合に、これが使用されていることがわかります。

    # vi ~/.bash_profile
    HISTFILE=/root/.commandline_warrior

    履歴ファイルの名前を変更する正当な理由がある場合は、この機能をどのように使用しているかを知りたいので、それを共有してください。

    8. HISTCONTROL を使用して、履歴から連続して繰り返されるエントリを削除します

    次の例では、 pwd が 3 回入力されています。履歴を実行すると、3 回連続して発生したことがすべて表示されます。重複を排除するには、以下に示すように HISTCONTROL を ignoreups に設定します。

    # 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 を 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 を使用して履歴に特定のコマンドを記憶させないようにする

    コマンドを実行するとき、以下に示すように、HISTCONTROL を ignorespace に設定し、コマンドの前にスペースを入力することで、コマンドを無視するように履歴に指示できます。履歴からコマンドを隠すことができるため、多くのジュニア システム管理者がこれに興奮しているのを目にすることができます。 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

    11.オプション -c を使用して以前の履歴をすべて消去します

    以前の履歴をすべて消去したいが、履歴は前に進めたい場合があります。

    # history -c

    12.履歴コマンドからの単語の置換

    履歴を検索しているとき、別のコマンドを実行したい場合がありますが、検索したばかりのコマンドと同じパラメーターを使用します。

    以下の例では、!!:$ 次の vi コマンドは、前のコマンドから現在のコマンドへの引数を取得します。

    # ls anaconda-ks.cfg
    anaconda-ks.cfg
    # vi !!:$
    vi anaconda-ks.cfg

    以下の例では、!^ 次の vi コマンドは、前のコマンド (つまり cp コマンド) から現在のコマンド (つまり vi コマンド) までの最初の引数を取得します。

    # cp anaconda-ks.cfg anaconda-ks.cfg.bak
    anaconda-ks.cfg
    # vi  !^
    vi anaconda-ks.cfg

    13.特定のコマンドを特定の引数に置き換えます。

    以下の例では、 !cp:2 cp で始まる履歴内の前のコマンドを検索し、cp の 2 番目の引数を取り、以下に示すように ls -l コマンドに置き換えます。

    # 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 で始まる履歴内の前のコマンドを検索し、cp の最後の引数 (この場合、上記の 2 番目の引数でもあります) を取り、以下に示すように ls -l コマンドに置き換えます。

    # ls -l !cp:$
    ls -l /really/a/very/long/path/long-filename.txt

    14. HISTSIZE を使用して履歴の使用を無効にする

    履歴をまとめて無効にし、入力したコマンドを bash シェルに記憶させたくない場合は、以下に示すように HISTSIZE を 0 に設定します。

    # export HISTSIZE=0
    # history
    # [Note that history did not display anything]

    15. HISTIGNORE を使用して履歴から特定のコマンドを無視する

    pwd や ls などの基本的なコマンドで履歴を乱雑にしたくない場合があります。 HISTIGNORE を使用して、履歴から無視するすべてのコマンドを指定します。 HISTIGNORE に ls を追加すると、ls -l ではなく ls のみが無視されることに注意してください。そのため、履歴から無視したい正確なコマンドを提供する必要があります。

    # 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]

    推奨読書

    Bash 101 ハック、Ramesh Natarajan 著 .私はほとんどの時間を Linux 環境に費やしています。だから当然、私は Bash のコマンド ラインとシェル スクリプトの大ファンです。 15 年前、さまざまな種類の *nix に取り組んでいたとき、C シェルと Korn シェルで多くのコードを記述していました。後年、システム管理者として Linux の作業を開始したとき、Bash シェル スクリプトを使用して、考えられるすべてのタスクをほぼ自動化しました。私の Bash での経験に基づいて、Bash コマンド ラインとシェル スクリプトの両方に関する 101 の実用的な例を含む Bash 101 Hacks eBook を作成しました。 Bash の習得を考えている場合は、ぜひこの本を読んでください。Bash のコマンド ラインとシェル スクリプトを制御するのに役立ちます。

    すばらしい Linux 記事

    以下は素晴らしい 15 の例です。 役に立つと思われる記事

    • Unix LS コマンド:15 の実例
    • Grep をつかもう! – 15 の実用的な grep コマンドの例
    • Linux Crontab:15 の素晴らしい Cron ジョブの例
    • ママ、見つけたよ! — 15 の実用的な Linux 検索コマンドの例

    Linux
    1. Linux での sa コマンドの例

    2. Linux での ac コマンドの例

    3. Linux での df コマンドの例

    1. LinuxでBASHコマンドライン履歴をクリアする方法

    2. Linux での du コマンドの例

    3. Linux / ubuntu ターミナルのコマンドラインで ctrl + r

    1. Linux履歴コマンド

    2. 7 Linux df コマンドの例

    3. 8 Linux TR コマンドの例