* xargsコマンド [#o870e54d]

*** xargs 引数なし(デリミタはデフォルトでスペース・タブ・改行) [#d01fa7d8]

 $ echo "/etc/passwd
 /etc/hosts
 /etc/resolv.conf" | xargs wc -l
 
  28 /etc/passwd
   1 /etc/hosts
   3 /etc/resolv.conf
  32 合計

 $ echo "/etc/passwd /etc/hosts /etc/resolv.conf" | xargs wc -l
 
  28 /etc/passwd
   1 /etc/hosts
   3 /etc/resolv.conf
  32 合計

*** xargs -d デリミタ指定 [#u6276396]

 # デリミタにスペースを指定
 $ echo -n "/etc/passwd /etc/hosts /etc/resolv.conf" | xargs -d " " wc -l
 
  28 /etc/passwd
   1 /etc/hosts
   3 /etc/resolv.conf
  32 合計

 # デリミタに改行を指定
 $ echo "/etc/passwd
 /etc/hosts
 /etc/resolv.conf" | xargs -d "\n" wc -l
 
  28 /etc/passwd
   1 /etc/hosts
   3 /etc/resolv.conf
  32 合計

*** xargs -I {} 引数を展開 [#g67fa39e]

 $ echo -n /etc/passwd /etc/hosts /etc/resolv.conf | xargs -d " " -I {} wc -l {}
 
  28 /etc/passwd
  1 /etc/hosts
  3 /etc/resolv.conf

オプション-Iを付けない場合は、

  wc -l /etc/passwd /etc/hosts /etc/resolv.conf
 wc -l /etc/passwd /etc/hosts /etc/resolv.conf

が実行されるのに対し、-Iを付けると

 wc -l /etc/passwd
 wc -l /etc/hosts
 wc -l /etc/resolv.conf

のように3回wcコマンドが実行される。{}で引数を展開できるので、


 $ ls *.txt
 1.txt  2.txt  3.txt
 
 $ ls -1 *.txt | xargs -I {} cp {} {}.backup
 
 $ ls *
 1.txt  1.txt.backup  2.txt  2.txt.backup  3.txt  3.txt.backup

のようなことが可能になる。

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS