xargsコマンドの説明します。

説明

xargsは、標準入力を引数として他のコマンドに渡すコマンドです。

使い方:xargs
読み方:エックスアーグス
  略:x arguments

具体例

# ■■■ 1.事前準備
# 作業ディレクトリ作成
[root@CE08PRD101 ~]# mkdir -p /work/xargs;cd /work/xargs;pwd
/work/xargs

# テストディレクトリ作成
[root@CE08PRD101 xargs]# mkdir -p test1 test2/test4 test3

# テストファイル作成
[root@CE08PRD101 xargs]# touch test1.txt test2.txt test3.txt
[root@CE08PRD101 xargs]# touch test1/test4.txt test2/test5.txt test3/test6.txt test2/test4/test7.txt

# テストファイル確認
[root@CE08PRD101 xargs]# ll -R
.:
合計 0
drwxr-xr-x 2 root root 23  9月 26 22:36 test1
-rw-r--r-- 1 root root  0  9月 26 22:36 test1.txt
drwxr-xr-x 3 root root 36  9月 26 22:36 test2
-rw-r--r-- 1 root root  0  9月 26 22:36 test2.txt
drwxr-xr-x 2 root root 23  9月 26 22:36 test3
-rw-r--r-- 1 root root  0  9月 26 22:36 test3.txt

./test1:
合計 0
-rw-r--r-- 1 root root 0  9月 26 22:36 test4.txt

./test2:
合計 0
drwxr-xr-x 2 root root 23  9月 26 22:36 test4
-rw-r--r-- 1 root root  0  9月 26 22:36 test5.txt

./test2/test4:
合計 0
-rw-r--r-- 1 root root 0  9月 26 22:36 test7.txt

./test3:
合計 0
-rw-r--r-- 1 root root 0  9月 26 22:36 test6.txt

# テストファイルのみ表示(この表示結果をxargsでrmに渡す)
[root@CE08PRD101 xargs]# find ./ -name test*txt
./test1.txt
./test2.txt
./test3.txt
./test1/test4.txt
./test2/test4/test7.txt
./test2/test5.txt
./test3/test6.txt

# ■■■ 2.テストファイルのみ削除する
[root@CE08PRD101 xargs]# find ./ -name test*txt |xargs rm -f

# ■■■ 3.事後作業
# テストファイルが削除されたことを確認する
[root@CE08PRD101 xargs]# ll -R
.:
合計 0
drwxr-xr-x 2 root root 6 9月 26 22:43 test1
drwxr-xr-x 3 root root 19 9月 26 22:43 test2
drwxr-xr-x 2 root root 6 9月 26 22:43 test3

/test1:
合計 0

./test2:
合計 0
drwxr-xr-x 2 root root 6 9月 26 22:43 test4

./test2/test4:
合計 0

./test3:
合計 0

# 作業ディレクトリ削除
[root@CE08PRD101 xargs]# cd ../;rm -fr xargs