Shell を利用する際に気をつけるべきこと

それは find コマンド

ディレクトリ名に 0x20 (空白)が入る Mac OS X 環境で find に -print0 を使わずに xargs で受け取るのはヤバい。かなり危険。一度 AppleiTunes のアップデートスクリプトで 0x20 デリミタの想定外動作をやらかして、誤消去したことがあったよなぁ。
なので「find には必ず -print0 オプションを付けて、xargs -0 で受け取る」というのを広く世に広めたい。

いまどきfindとxargsを使う時は -print0 と -0 を忘れずに
find . -type f -name '*~'  -print0 | xargs -0 rm 

find -print0 を利用する際に気をつけるべき点

-print0 を -name などよりも先に指定すると、他の条件は無関係に全てのファイルを出力してしまう。

いまどきfindとxargsを使う時は -print0 と -0 を忘れずに

そもそも -print0 や -0 は何か?

% man find
     -print  This primary always evaluates to true.  It prints the pathname of
             the current file to standard output.  If none of -exec, -ls,
             -print0, or -ok is specified, the given expression shall be
             effectively replaced by ( given expression ) -print.

     -print0
             This primary always evaluates to true.  It prints the pathname of
             the current file to standard output, followed by an ASCII NUL
             character (character code 0).

% man xargs
     -0      Change xargs to expect NUL (``\0'') characters as separators,
             instead of spaces and newlines.  This is expected to be used in
             concert with the -print0 function in find(1).