0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[高速版]ファイル or ディレクトリを検索して一括削除する

Posted at

他の人が既に投稿してるかもしれないが、よく使うコマンドなので備忘録。

結論

find+xargsで爆速になる。

find <path> -name <name> -type <type> | xargs rm -rf

解説

ググるとxargsの他に-execオプションを使う方法もヒットする。

find <path> -name <name> -type <type> -exec rm -rf {} \;

-exec {} ;の場合、1 行ずつコマンドに渡しrmを実行する。

rm -rf foo.txt
rm -rf bar.txt
rm -rf baz.txt
rm -rf qux.txt

xargsの場合、可能な限り複数行をまとめてコマンドに渡しrmを実行する。

rm -rf foo.txt bar.txt baz.txt qux.txt

毎回実行するか、まとめて実行するかの違い。なのでxargsの方が高速。

因みに-exec {} +を使うと、複数行をまとめてコマンドに渡せるけど、実行対象が数万件とかだとxargsの方が効率的らしい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?