xargsで実行結果が無いときは実行しない

よくfindコマンドとxargsコマンドを組み合わせますが,findの結果が0のときでもxargsは動いてファイルがないのでエラーが出力されてしまいます.

$ find . -name 'hoge' -print | wc -l

0

$ find . -name 'hoge' -print | xargs flickcurl upload

flickcurl: Minimum of 1 arguments for command `upload'

  USAGE: flickcurl upload FILE [PARAMS...]

Try `flickcurl --help' for more information.

xargsを使わないでfindの-execで回避できるのですが,xargsを使いたいです.

$ find . -name 'hoge' -exec flickcurl upload {} \;

manをみるとそのもののオプションがありました.

-r, –no-run-if-empty
If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a
GNU extension.

早速試してみるとうまく行きました :)

$ find . -name 'hoge' -print | xargs -r flickcurl upload

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)