複数のフォルダのファイルの違いを確認する

複数のフォルダのファイルの違いを確認する

スナップショットを使用して、バックアップ システム内でファイルが異なるかどうかを確認したいと思います。

同じアーキテクチャを持つフォルダが複数あります

ls -1 .snapshot
4-hourly.2024-04-14_0405
4-hourly.2024-04-14_0805
4-hourly.2024-04-14_1205
4-hourly.2024-04-14_1605
4-hourly.2024-04-14_2005
4-hourly.2024-04-15_0405
4-hourly.2024-04-15_0805
4-hourly.2024-04-15_1205
daily.2024-04-08_0010
daily.2024-04-09_0010
daily.2024-04-10_0010
daily.2024-04-11_0010
daily.2024-04-12_0010
daily.2024-04-13_0010
daily.2024-04-14_0010
daily.2024-04-15_0010
monthly.2024-01-01_0020
monthly.2024-02-01_0020
monthly.2024-03-01_0020
monthly.2024-04-01_0020
weekly.2024-02-25_0015
weekly.2024-03-03_0015
weekly.2024-03-10_0015
weekly.2024-03-17_0015
weekly.2024-03-24_0015
weekly.2024-03-31_0015
weekly.2024-04-07_0015
weekly.2024-04-14_0015

そして、これらの各フォルダ内にあるファイルを確認する必要があります。

たとえば、目標は、が .snapshot/weekly.2024-04-14_0015/my/path/to/the/file.phpweekly.2024-04-07_0015/my/path/to/the/file.php と異なるか、または.snapshot/weekly.2024-03-31_0015/my/path/to/the/file.php、または.snapshot/weekly.2024-04-07_0015/my/path/to/the/file.phpなどと異なるかどうかを確認することです。

これには明白で簡単な方法がありますか?

PS: このフォルダー内には変更された他のファイル/フォルダーがあり、フォルダー全体を比較することはできません。

答え1

シェル ループでは簡単な作業のように思えます。これは、zsh、bash、dash などでも機能するはずです。

cd .snapshot
original="weekly.2024-04-14_0015/my/path/to/the/file.php"
pathonly="$(echo "${original}" | sed 's;^[^/]*/\(.*\)$;\1;')" # cut off head directory

orig_hash=$(sha1sum "${original}" | sed 's/^\([^ ]*\) .*$/\1/')

for candidate in */"${pathonly}" ; do
  # don't compare file to itself, that'd be silly
  [[ "${candidate}" = "${original}" ]] && continue 

  cand_hash=$(sha1sum "${candidate}" | sed 's/^\([^ ]*\) .*$/\1/')

  # check hashes, so that we don't have to read the original N times
  ( [[ "${orig_hash}" = "${cand_hash}" ]] && \
  # if the hashes match, use the cmp tool to compare files byte by byte
    cmp -- "${candidate}" "${original}" ) || \
    # if either the hash equality or the content equality fail, print file name
    printf '%s differs\n' "${candidate}"
done

関連情報