スクリーンショット(名前にスペース含むファイル)をまとめて消すコマンド


% find . -name 'スクリーン*'
./スクリーンショット 2016-06-18 16.59.14.png
./スクリーンショット 2016-06-18 16.59.23.png
./スクリーンショット 2016-06-18 16.59.27.png

こんな状態のときに、まとめて消そうとして、以下のコマンドだとちょっとダメです。

% find . -name 'スクリーン*' | xargs rm
rm: ./スクリーンショット: No such file or directory
rm: 2016-06-18: No such file or directory
rm: 16.59.14.png: No such file or directory
rm: ./スクリーンショット: No such file or directory
rm: 2016-06-18: No such file or directory
rm: 16.59.23.png: No such file or directory
rm: ./スクリーンショット: No such file or directory
rm: 2016-06-18: No such file or directory
rm: 16.59.27.png: No such file or directory

ファイルにスペースが含まれているので、そこで区切られてrmコマンドに渡ってしまいます。なので以下のようにします。

% find . -name 'スクリーン*' -print0 | xargs -0 rm

ありがとうございました。

参考

はじめてNUL文字を知りました。

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).
man_find
     -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).