xargs 標準入力からアイテムを読み取るSystemのようなUNIXのコマンドです。 空白(二重引用符または一重引用符または円記号で保護できます)または改行で区切られ、コマンドを実行します(デフォルトは / bin / echo) 初期引数とそれに続く標準入力から読み取られた項目を含む1回以上。標準入力の空白行は無視されます。
xargsコマンドは、他のコマンドと組み合わせると非常に便利です。 デフォルトでは、 STDINからの入力を想定しています。 .xargsは基本的に、初期コマンドの出力を拡張し、その出力を多数の操作の実行に利用するために使用されます。 。
この投稿では、linuxxargsコマンドの11の実用的な例について説明します
例:1xargsの基本的な使用法
xargsと入力すると、入力が必要になります。次の行にEnterキーを押して入力を開始し、 ctrl + dを実行します。 以下のように出力を確認します。
[email protected]:~$ xargs hello john this is me ( ctrl+d) hello john this is me [email protected]:~$home/Downloads#
例:2 xargs(-d)での区切り文字の使用
ここでは、オプション -dを使用して区切り文字を指定します 、 \ n 区切り文字として。 Ctrl + Dを押すと、文字列が画面にエコーバックされます
[[email protected] ~]# xargs -d\n Hi Welcome here Now press Ctrl+D Hi Welcome here Now press Ctrl+D [[email protected] ~]#
例:3行あたりの出力の制限(-n)
xargsコマンドの-nオプションを使用して、要件ごとに出力を制限できます。たとえば、1行に2つのアイテムのみを表示します。
[email protected]:~$ echo a1 b2 c3 d4 e45 a1 b2 c3 d4 e5 [email protected]:~$ echo a1 b2 c3 d4 e5 | xargs -n 2 a1 b2 c3 d4 e5 [email protected]:~$
例:4実行前にユーザープロンプトを有効にする(-p)
オプション-pを使用する xargsコマンドでは、実行前に yでユーザーにプロンプトが表示されます (はいを意味します)および n (ないことを意味します)オプション。
[email protected]:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2 /echo a1 b2 ?...y a1 b2 echo c3 d4 ?...y c3 d4 echo e5 ?...n [email protected]:~$ [email protected]:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2 /echo a1 b2 ?...y a1 b2 echo c3 d4 ?...y c3 d4 echo e5 ?...y e5 [email protected]:~$
例:5findとxargsを使用したファイルの削除
/tmpフォルダーから*.txtファイルを削除するとし、次のコマンドを実行します。
[email protected]:~$ find /tmp -type f -name '*.txt' | xargs rm
注: システムの時間とリソースが少なくて済むため、上記のfindコマンドとxargsコマンドの組み合わせを使用して1000以上のファイルを削除することを常にお勧めします。
例:6検索にXargsとgrepコマンドを使用する
xargsでgrepコマンドを使用して、findコマンドの結果から特定の検索をフィルタリングできます。例を以下に示します:
[email protected]:~$ find . -name "stamp" | xargs grep "country" country_name [email protected]:~$
例:7ファイル名のスペースを処理する
xargsは、 print0 を使用して、ファイル内のスペースを処理することもできます。 およびxargs-0 コマンドを見つけるための引数。
[email protected]:~$ find /tmp -name "*.txt" -print0 | xargs -0 ls /tmp/abcd asd.txt /tmp/asdasd asdasd.txt /tmp/cdef.txt [email protected]:~$ find /tmp -name "*.txt" -print0 | xargs -0 rm [email protected]:~$
例:8cutコマンドでxargsを使用する
デモンストレーションでは、最初に以下の内容のcars.txtを作成しましょう:
[email protected]:~$ cat cars.txt Hundai,Santro Honda,Mobilio Maruti,Ertiga Skoda,Fabia
最初の列にデータを表示するには 以下に示すように。
[email protected]:~$ cut -d, -f1 cars.txt | sort | xargs Honda Hundai Maruti Skoda [email protected]:~$
例:9各ファイルの行数を数える
[email protected]:~$ ls -1 *.txt | xargs wc -l 4 cars.txt 13 trucks.txt 17 total [email protected]:~$
例:10ファイルを別の場所に移動する
[email protected]:~$ pwd /home/linuxtechi [email protected]:~$ ls -l *.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcde.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcd.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 fg.sh [email protected]:~$ sudo find . -name "*.sh" -print0 | xargs -0 -I {} mv {} backup/ [email protected]:~$ ls -ltr backup/ total 0 -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcd.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcde.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 fg.sh [email protected]:~$
例:11Xargsコマンドの文字列の置換(-i)
以下のコマンドを実行すると、現在の作業ディレクトリに3つのファイルa、b、cが作成されます
[email protected]:~$ printf "a\nb\nc\n" | xargs touch
a.txt、b.txt、c.txtを作成してから、xargsコマンドで-iパラメータを使用すると、文字列「 a」が置き換えられます。 ‘with a.txt など
[email protected]:~$ printf "a\nb\nc\n" | xargs -i touch {}.txt
この記事の内容は以上です。これらのxargsコマンドの例が参考になることを願っています。フィードバックやコメントをお気軽に共有してください。