xargs は、標準入力からコマンドを構築および実行するために使用できる Unix コマンドです。
重要性:
grep などの一部のコマンドは入力をパラメーターとして受け入れることができますが、一部のコマンドは引数を受け入れます。ここで xargs が登場します。
Linux の「xargs」コマンドの構文
xargs [options] [command]>
Linux の「xargs」コマンドで利用可能なオプション
| オプション | 説明 |
|---|---|
| -0 | 入力項目は空白ではなくヌル文字で終了します |
| -ファイル | 標準入力の代わりにファイルから項目を読み取る |
| –delimiter = 分割 | 入力項目は特殊文字で終了します |
| -E eof-str | ファイル文字列の終わりを eof-str に設定します |
| -strを置き換えます | 初期引数内の replace-str の出現を、標準入力から読み取られた名前で置き換えます |
| -L 最大行数 | コマンド ラインごとに最大 max-lines の空白以外の入力行を使用します。 |
| -p | 各コマンドラインを実行するか、ターミナルから行を読み取るかについてユーザーにプロンプトを表示します。 |
| -r | 標準入力に空白以外の文字が含まれていない場合は、コマンドを実行しないでください。 |
| -バツ | サイズを超えた場合は終了します。 |
| -ヘルプ | オプションの概要を xargs に出力して終了します |
| -バージョン | バージョン番号を出力します。 xargs の終了と終了 |
例 :

xargs の例
以下は C プログラムです。このプログラムは、テキスト ファイル test.txt を読み取り、このプログラムの出力を touch コマンドへの入力として使用します。テキストファイルtest.txtの内容
file1 file2 file3 file4>
C
// C program to read contents of file> #include> // Driver Code> int> main(){> >int> c;> >FILE> *file;> >// open file test.txt> >file =>fopen>('test.txt', 'r');> >if> (file) {> >// read file line-by-line until> >// end of file> >while> ((c =>getc>(file)) != EOF)> >putchar>(c);> >fclose>(file);> >}> >return> 0;> }> |
>
>
出力:
file1 file2 file3 file4>
ここで、次の出力を使用します。 ./a.out タッチコマンドへの入力として

touch を使用した xargs の例
オプションを指定したコマンドの使用法:
xargs --version>
xargs コマンドのバージョン番号を出力して終了します。
出力:
Linux どれ
xargs (GNU findutils) 4.7.0-git Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.>
xargs -a test.txt>
ファイルの内容が表示されます
file1 file2 file3 file4>
xargs -p -a test.txt>
-p オプションは、各コマンド ラインを実行する前に確認を求めます。応答が「y」または「Y」で始まる場合にのみコマンドラインを実行します。 出力:
# xargs -p -a test.txt echo file1 file2 file3 file4 ?...y file1 file2 file3 file4 # xargs -p -a test.txt echo file1 file2 file3 file4 ?...n>
xargs -r -a test.txt>
ここで、ファイル test.txt が空で、上記のコマンドが実行されたとします。 -r オプションにより、標準入力が空の場合はコマンドが実行されないため、上記のコマンドは出力を生成しません。 ただし、上記のコマンドが実行された場合、 -r オプションを使用しない場合、出力として空行が生成されます。インスタンスとして以下の画像を参照してください。

-r オプションを使用した xargs