Trying to make sure that the USB file is identical to source
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.