LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Trying to make sure that the USB file is identical to source (https://www.linuxquestions.org/questions/linux-general-1/trying-to-make-sure-that-the-usb-file-is-identical-to-source-4175705313/)

postcd 12-23-2021 05:28 AM

Trying to make sure that the USB file is identical to source
 
Hello,
which command, switches You would use to make absolutely sure that the file copied to USB is identic to source file located on system drive?

Code:

cmp -n `stat -c '%s' a` b
(i am unsure if -n is beneficial, why not to omit it)

Code:

sudo head -c $(stat -c '%s' a) b | sha1sum
a source, b destination file

they say that due to kernerl Vm cache one should run "sync" un-mount usb drive: "umount /dev/sdX1"
mount it again and then do mentioned cmp and sha1sum commands. Then sync and gracefully un-mount the drive.

How you would do it? Should i prefix the checking command by "sync && " or something else? Linux type mentioned in signature.

hazel 12-23-2021 05:51 AM

The quickest and most reliable way is to take md5sums of both files and compare them. If there is even one bit miscopied, the md5sums will be totally different.

HappyTux 12-23-2021 06:59 AM

Quote:

Originally Posted by postcd (Post 6311902)
Hello,
which command, switches You would use to make absolutely sure that the file copied to USB is identic to source file located on system drive?

You could use something like rsync and let it do all the work of the calculations for you.

Code:

rsync -avP /path/to/file/to/be/copied /path/to/destination/directory/
That will get you the file copied to the destination directory with all the checks done to make certain it is identical to the source by the copying program rsync.

Edit: Oh and the best part is if the file is to be copied many times due to changes in it then the rsync program will only copy the differences necessary to make them identical again.

pan64 12-23-2021 08:35 AM

what about: cmp a b ?

but if you want to be sure the file is really written to the usb you need to execute sync first.

rknichols 12-23-2021 08:37 AM

Quote:

Originally Posted by HappyTux (Post 6311925)
You could use something like rsync and let it do all the work of the calculations for you.

Code:

rsync -avP /path/to/file/to/be/copied /path/to/destination/directory/

As written, that command will just check that the file sizes and modification times match. You would have to use the "-c" (--checksum) option to actually compare the contents.

TB0ne 12-23-2021 09:24 AM

Quote:

Originally Posted by postcd
Hello,
which command, switches You would use to make absolutely sure that the file copied to USB is identic to source file located on system drive?
Code:

cmp -n `stat -c '%s' a` b
(i am unsure if -n is beneficial, why not to omit it)


Did you read/understand the man page of the cmp command, and what the -n does?? Did you think about why the -n might be beneficial for a larger file?
Quote:

Code:

sudo head -c $(stat -c '%s' a) b | sha1sum
a source, b destination file

they say that due to kernerl Vm cache one should run "sync" un-mount usb drive: "umount /dev/sdX1" mount it again and then do mentioned cmp and sha1sum commands. Then sync and gracefully un-mount the drive.
Who is the "they" you mention? And doing a sync when using removable media is typically a best-practice, as someone who's been using Linux for many years (as you have) should know.
Quote:

How you would do it? Should i prefix the checking command by "sync && " or something else? Linux type mentioned in signature.
I'd use rsync (as you've been asking about for the past seven years now), and cleanly unmount the removable media. Doing a basic umount should also force a complete of any pending tasks to the drive.
Quote:

Originally Posted by rknichols (Post 6311944)
As written, that command will just check that the file sizes and modification times match. You would have to use the "-c" (--checksum) option to actually compare the contents.

Yes...the OP is very familiar with rsync, and agree that the -c would checksum a file for the sake of being very thorough. No actual problem description from the OP, though, as to why they think they're having issues, or why a standard "cp" won't work.
https://www.linuxquestions.org/quest...ze-4175538411/
https://www.linuxquestions.org/quest...nc-4175585087/
https://www.linuxquestions.org/quest...te-4175691912/
https://www.linuxquestions.org/quest...on-4175685524/

rtmistler 12-24-2021 07:01 PM

I like to be very simple about things. If I already know what to do, and it works, then I continue to do that.

There are some command forms for Unix or Linux which I've used for probably 30 years.

Convincing myself to type something different is difficult, my finger memory literally types the, tried and true, by default.

The commands cited in the OP, are ones which you've used. If they work, or if there are other commands you use to do this, I wouldn't look to change them to something you're unfamiliar with.

Sorry this may be my misinterpretation, however I wouldn't be questioning the operation of some fairly long-time commands, cmp, sha1sum, and umount all work.

dugan 12-24-2021 10:22 PM

"diff -b"


All times are GMT -5. The time now is 06:23 PM.