LoginSignup
9
8

More than 5 years have passed since last update.

find 結果それぞれにパイプ・リダイレクトを含むコマンドを適用する

Last updated at Posted at 2015-11-05

xargs の -I オプションを使うと簡潔に書けることを教えてもらった。
iwaim さんありがとうございます。

find . -type f | xargs -I{} sh -c "rpm2cpio {} | cpio -id"

ふと困ってしまったんだけどなんのことはなくて while read に通すだけだった。

find . -type f | while read -r file; do rpm2cpio ${file} | cpio -id; done

整形するとこう。

find . -type f |                    \
while read -r file;                 \
do                                  \
    rpm2cpio ${file} | cpio -id;    \
done

こうすると別に find である必要もない。

ls *.rpm | while read -r file; do rpm2cpio ${file} | cpio -id; done

割りと長いので次の内容のファイルをパスの通ったところにおいて使うと多少楽。

piped-command.sh
#!/bin/sh

while read -r arg;
do
    eval "$1";
done

こんな感じでたたく。

ls *.rpm | piped-command.sh 'rpm2cpio ${arg} | cpio -id'
9
8
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8