LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 12-23-2021, 05:28 AM   #1
postcd
Member
 
Registered: Oct 2013
Posts: 527

Rep: Reputation: Disabled
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.

Last edited by postcd; 12-23-2021 at 06:36 AM.
 
Old 12-23-2021, 05:51 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,633
Blog Entries: 19

Rep: Reputation: 4469Reputation: 4469Reputation: 4469Reputation: 4469Reputation: 4469Reputation: 4469Reputation: 4469Reputation: 4469Reputation: 4469Reputation: 4469Reputation: 4469
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.
 
2 members found this post helpful.
Old 12-23-2021, 06:59 AM   #3
HappyTux
Senior Member
 
Registered: Mar 2003
Location: Nova Scotia, Canada
Distribution: Debian AMD64
Posts: 4,170

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by postcd View Post
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.

Last edited by HappyTux; 12-23-2021 at 07:06 AM.
 
1 members found this post helpful.
Old 12-23-2021, 08:35 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
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.

Last edited by pan64; 12-23-2021 at 08:37 AM.
 
1 members found this post helpful.
Old 12-23-2021, 08:37 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Quote:
Originally Posted by HappyTux View Post
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.
 
2 members found this post helpful.
Old 12-23-2021, 09:24 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,700

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
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 View Post
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/

Last edited by TB0ne; 12-23-2021 at 09:25 AM.
 
Old 12-24-2021, 07:01 PM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
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.
 
1 members found this post helpful.
Old 12-24-2021, 10:22 PM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,243

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
"diff -b"
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] automake to update configure due to updated source code to make sure make install works. BW-userx Programming 2 07-27-2017 05:59 PM
autofs local map files identical but not identical to automount jwaldram Linux - Server 2 10-26-2012 10:35 AM
I'm not sure how this happened but I'm sure there is a lesson in there somewhere mreff555 Linux - Newbie 6 09-24-2012 02:09 AM
Identical disks that are not identical staphanes Linux - Hardware 8 03-11-2006 11:50 AM
Trying to make sure I understand cvsup and stuff Geocritter *BSD 2 02-03-2006 07:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration