tl;dr:-delete POSIX では必須ではありません。-exec
事実
find の POSIX 1003.1 man ページ -exec を指定します -delete ではありません .
これは -exec を意味します 事実上どこでも機能するはずです。 find を見つけると驚くでしょう -delete を持つ -exec なし .その逆も十分可能です。特に busybox を使用する軽量システム 基本的なコマンド ライン オプションのみを提供する傾向があります。
例えば。ルーターの 1 つとその find に OpenWRT があります -exec を理解する 、それは理解していません -delete .
-delete を持っていない -exec rm … があれば大したことではありません .一方で -delete -exec を置き換えることはできません 一般に。 -delete を省略できるようにするのは賢明な設計です
個人的な経験、観察、意見
上記は すべき -exec rm {} \; の主な理由 広く推奨されています。二次的なものは雪だるま効果かもしれません。ユーザーは記事と例を読み、-exec に慣れる 独自のコマンドを公開します (例:ここではスーパー ユーザー)。 -deleteすら知らない人もいるかもしれません
「-delete を使用できます」 代わりは'。すると、「ありがとう、知らなかった」という反応が返ってきました。 「わかっていますが、これは POSIX ではありません」という返事は覚えていません。
これらすべてを言った後、私は -delete に言及する傾向があります いつでも -exec rm {} \; が表示されます。理由は -delete です -exec rm {} \; の間、新しいプロセスを生成しません 別の rm を呼び出します 一致したファイルごとに。 -deleteが使えない場合 次に考えるのは -exec rm {} + です 単一の rm で複数のファイルを削除できる (それでも rm を呼び出します 必要に応じて複数回)
-exec … + ではない理由 広く推奨されていますか?それは、その制限のためかもしれません。経験の浅いユーザーが「これは rm で動作する」と考えていることを想像できます 、 mv で使用させてください !'次に -exec mv {} foo/ + {} のため動作しません + の直前の最後にある必要があります .ユーザーはイライラして mama のところに戻ってきます
-delete を推奨 ここでは通常、スーパーユーザーで安全だと思います。ほとんどの質問は、「大きな」OS-es、find を指定しています。 コマンドには豊富なオプションがあります。 find のユーザーがいたとしても は限られているので、おそらくフィードバックが得られるでしょう。彼または彼女は解決策がうまくいかないと言い、私は -exec rm … を提案します 代わりに、問題などを説明してください。
-delete を推奨するスタンドアロンの記事 そのようなフィードバックは得られません。問題が発生した場合、ユーザーは Google から返された次のリンクに移動するだけです。
違いは柔軟性にあります。 -exec を使用すると、選択したファイルごとにコマンドが実行されます。 -exec を使用すると、他の検索オプションを柔軟に適用できます。 -delete を使用すると、-prune の使用が制限されます。さらに、-delete の配置は結果に影響します。以下のドキュメント スニペットを参照してください:
-delete
Delete files; true if removal succeeded. If the removal failed,
an error message is issued. If -delete fails, find’s exit status will be
nonzero (when it eventually exits). Use of -delete automatically turns on
the ‘-depth’ option.
Warnings: Don’t forget that the find command line is evaluated as an
expression, so putting -delete first will make find try to delete every-
thing below the starting points you specified. When testing a find
command line that you later intend to use with -delete, you should
explicitly specify -depth in order to avoid later surprises.
Because -delete implies -depth, you cannot usefully use -prune and -delete
together.
-exec command ;
Execute command; true if 0 status is returned. All following arguments
to find are taken to be arguments to the command until an argument
consisting of ‘;’ is encountered. The string ‘{}’ is replaced by the
current file name being processed everywhere it occurs in the arguments
to the command, not just in arguments where it is alone, as in
some versions of find. Both of these constructions might need to be escaped
(with a ‘\’) or quoted to protect them from expansion by the shell.
See the EXAMPLES section for examples of the use of the -exec option.
The specified command is run once for each matched file. The
command is executed in the starting directory. There are unavoidable security
problems surrounding use of the -exec action; you should use the -execdir
option instead.
-exec command {} +
This variant of the -exec action runs the specified command on the
selected files, but the command line is built by appending each selected
file name at the end; the total number of invocations of the command
will be much less than the number of matched files. The command line is
built in much the same way that xargs builds its command lines.
Only one instance of ‘{}’ is allowed within the command. The command is
executed in the starting directory