Linux bash で、あるディレクトリ内のファイルが別のディレクトリに再帰的に存在するかどうかを調べ、存在するか存在しないかを印刷します。

Linux bash で、あるディレクトリ内のファイルが別のディレクトリに再帰的に存在するかどうかを調べ、存在するか存在しないかを印刷します。

Linux bash で、あるディレクトリ内のファイルが別のディレクトリに再帰的に存在するかどうかを調べ、存在するか存在しないかを印刷します。

あなたが持っているとしましょう

  • pth1/dirA/file1 pth1/dirA/DirB/file2 そして
  • pth2/dirA/file1 pth2/dirA/DirB/file3

私はレポートが欲しい

file1 exists 
files2 dont exist in pth2
files3 dont exist in pth1

両方のディレクトリの現在のレベルで機能するコードを見つけましたが、再帰的に機能させることはできませんここから引用

pth1="/mntA/newpics";
pth2="/mntB/oldpics";
for file in "${pth1}"/*; do
    if [[ -f "${pth2}/${file##*/}" ]]; then
       echo "$file exists";
    fi
done

両方のパスで再帰的に動作させるにはどうすればよいですか?

答え1

私は別の方法でそれを実行しました。 1 つのディレクトリ内のすべてのファイルを検索し、パスを削除してから、結果を 2 つの異なるファイルに保存して、meld または他のプログラムで比較するか、または検索結果を meld で直接比較することができます。

ファイルを並べ替えて、結果に重複ファイルではなく一意のファイルのみを選択することに注意してください。また、ファイル名が「jpg」拡張子で終わるファイルのみを対象としています。

pth1="/mnt/oldfiles";
pth2="/mnt/newfiles";

その後

(find "${pth1}"/ -exec basename {} \; | grep "jpg$" | sort | uniq )  > a.txt;
(find "${pth2}"/ -exec basename {} \; | grep "jpg$" | sort | uniq )  > b.txt;
meld a.txt b.txt

または直接

meld <(find "${pth1}"/ -exec basename {} \; | grep "jpg$" | sort | uniq )  <(find "${pth2}"/ -exec basename {} \; | grep "jpg$" | sort | uniq )

更新: 一方のディレクトリが他方のディレクトリよりはるかに大きい場合、直接コマンドは機能しません (MILD は両方のコマンドが完了せずに開きます)。

答え2

何を望んでいるのかはよく分かりませんが、これでできると思います。このコマンドは、 の下にあるすべてのファイルを の下にあるファイルと比較し/path/1/path/2存在と等価性をチェックします。

diff --brief --recursive /path/1 /path/2

実例

# Create some files
mkdir -p 1/{x,y} 2/{x,z}
touch 1/{x,y}/file1
date | tee 2/x/file1 >2/z/date

# Show what we have
tree 1 2
1
├── x
│   └── file1
└── y
    └── file1
2
├── x
│   └── file1
└── z
    └── date
4 directories, 4 files

# Compare the two directory trees
diff --brief --recursive 1 2
Files 1/x/file1 and 2/x/file1 differ
Only in 1: y
Only in 2: z

関連情報