Linux上の2つのディレクトリ間で欠落しているファイル名を見つける

Linux上の2つのディレクトリ間で欠落しているファイル名を見つける

Linuxで2つのディレクトリ(A / B)を比較し、Aに存在しないBのファイルを削除しようとしています。

たとえば、ファイル "1.jpg"がBディレクトリにはありますが、Aディレクトリにはない場合はBから削除する必要があります。

diffを使ってみましたが、すべてのファイルが本質的に異なるため、うまくいかないようです。 (サイズは異なりますが、IDは同じサムネイルです。)したがって、これはファイルの実際の内容を無視し、ファイル名だけで行う必要があります。

最小限の努力でこれを行う方法を教えてくれる人はいますか?

ベストアンサー1

rsync必要な作業をすばやく簡単に実行できます。

rsync --dry-run --verbose --recursive --existing --ignore-existing --delete-after A/ B/

ヘルプから:

 --existing              skip creating new files on receiver
 --ignore-existing       skip updating files that already exist on receiver
 --delete                delete extraneous files from destination dirs

dry-run提案された結果に満足したら、実際に削除を実行するオプションを削除してください。


マニュアルページにはオプションのより明確な説明があり、ユースケースにも言及されています。

   --existing, --ignore-non-existing
      This  tells rsync to skip creating files (including directories)
      that do not exist yet on the destination.   If  this  option  is
      combined  with  the  --ignore-existing  option, no files will be
      updated (which can be useful if all you want to do is to  delete
      extraneous files).


   --ignore-existing
      This  tells  rsync  to skip updating files that already exist on
      the destination (this does not ignore  existing  directores,  or
      nothing would get done).  See also --existing.

おすすめ記事