0

so I'm trying to work out how to hash all files within a USB directory. I came across another post similar to mine including this script below:

find "$PWD" -type d | sort | while read dir; do [ ! -f "${dir}"/@md5Sum.md5 ] && echo "Processing " "${dir}" || echo "Skipped " "${dir}" " @md5Sum.md5 already present" ; [ ! -f "${dir}"/@md5Sum.md5 ] && md5sum "${dir}"/* > "${dir}"/@md5Sum.md5 ; chmod a=r "${dir}"/@md5Sum.md5;done

It works really well and does everything I'd want it to do however I can't figure out how to alter it to target the USB. I've already got the user to input the USB device by them inputting /dev/sdf or /dev/sdg etc. But I can't figure out how to carry this information into the command above. Any advice or suggestions on this would be appreciated.

1 Answers1

0

I made a test with the Ubuntu Server iso file ubuntu-20.04.3-live-server-amd64.iso

from https://releases.ubuntu.com/20.04.3/ with the sha256sum

f8e3086f3cea0fb3fefb29937ab5ed9d19e767079633960ccb50e76153effc98
  • The iso file was cloned to USB drive. (You can do it with any cloning tool.)

  • The used size on the USB drive was determined with

    sudo fdisk -lu /dev/sdx
    

    where x is the drive letter (identified with fdisk and for example lsblk -f) and the number of sectors (from 0 to 'end_sector') is end_sector + 1, so in my test example 2463616 and the sector size is 512 bytes.

  • Check that no file system in the drive is mounted, and unmount the partition(s) if mounted (but do not eject).

  • Check on the USB drive

    Calculate a checksum on the whole used part of the USB drive,

    sudo dd if=/dev/sdx bs=512 count=2463616|sha256sum
    

    The output of this check is the same as the uploaded checksum, which verifies that the cloning operation was successful. You may prefer to pipe the output from dd to md5sum.

  • If the checksum does not match for you, please check first that the number of sectors and number of bytes are correct (that you have checked exactly the correct part of the USB drive).


  • This same method works also when you clone from one USB drive to a another drive that is big enough, so check on the new drive (sha256sum or md5sum according to your choice).
sudodus
  • 47,684