空入力時の xargs の挙動

pdumpfs でのバックアップから古いものを削除しようと思い

find . -mindepth 1 -maxdepth 2 -type d -links 2 -print | xargs rmdir

というようなコマンドを cron で回していたところ、該当なし(つまり find が何も出力しない)ときにも rmdir が実行されてエラーになっていた。

apt-get source findutils 

して xargs.c を眺めてみたら

      /* SYSV xargs seems to do at least one exec, even if the
         input is empty.  */
      if (cmd_argc != initial_argc
          || (always_run_command && procs_executed == 0))
        do_exec ();
    }

てなコードがあったので、そういう仕様らしい。直感的でないなあ…と xargs(1) をよく眺めるとちゃんと動作変更オプションがありました。

--no-run-if-empty, -r
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.