recsha 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/sh
  2. SUMFILE="SHA256SUM"
  3. SUMPROG="sha256sum"
  4. die()
  5. {
  6. echo "$*" >&2
  7. exit 1
  8. }
  9. # Checksum directory ($1) contents
  10. checksum_dir()
  11. {
  12. local path="$1"
  13. [ -d "$path" ] || die "'$path' is not a directory"
  14. echo "entering directory '$path'"
  15. local olddir="$(pwd)"
  16. cd "$path" || die "cd '$path' failed"
  17. for file in *; do
  18. [ "$file" = "$SUMFILE" ] && {
  19. # Do not checksum the checksum file
  20. continue
  21. }
  22. [ -d "$file" ] && {
  23. # Recurse into subdirectory
  24. checksum_dir "$file"
  25. continue
  26. }
  27. [ -f "$file" ] || {
  28. # This is not a regular file. Don't checksum it.
  29. continue
  30. }
  31. [ -f "$SUMFILE" ] && {
  32. cat "$SUMFILE" | while read line; do
  33. if [ "x$file" = "x$(echo "$line" | cut -d' ' -f2- | cut -c2-)" ]; then
  34. # This file is already checksummed. Skip it.
  35. return 1
  36. fi
  37. done || continue
  38. }
  39. "$SUMPROG" -b "$file" >> "$SUMFILE" ||\
  40. die "checksumming of '$file' failed"
  41. done
  42. echo "leaving directory '$path'"
  43. cd "$olddir" || die "cd '$olddir' failed"
  44. }
  45. for path in "$@"; do
  46. checksum_dir "$path"
  47. done