Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ask for korn shell

 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

i use following command:

md5sum TEST.xml

the output looks like:

900hjidur84hjr938ikv TEST.xml

as you can see, the first part is the md5 code, the second part is the file name, but i only want the first part(md5 code), and save it to a file, how to do that? thanks.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11881
196
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So many ways to do it, although it should be noted that this may be undesirable - normally you verify a pre-computed md5 hash by looking at a file that contains both the md5 hash and the file name (exactly the format that the md5sum application puts it out in). You might want to look at Wikipedia's article on md5sum for information on how it is normally used.

As for what you are apparently trying to do -

We know that the standard output of md5sum is going to be a 32 character hexadecimal string. Therefore we can cut the first 32 characters to grab just the hash:



Since we know that the first non-hash character is a space, we can set that to be our delimiter, and then grab the first field:



Alternatively, we know that the hash will be the first word in the output, so we can just print the first word:



We could use the split command to break it into multiple files, based on the byte size:



 
Tim Holloway
Saloon Keeper
Pie
Posts: 18164
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what any of this has to do with the Korn Shell.

Just in case anyone's interested, however, I'll note that the md5sum program can not only run a sum on a single file - it can do a whole collection of files from a single command, with the sum and filename on each line. In fact, that's the reason WHY the filename is included on the output line. This is a feature that's popular with those who setup tamper-resistant websites as well as for people who want to detect possible intrusion or corruption of critical files on their local machine.
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for helps, but something strange is that i find the md5sum command in different linux generate different result, for example, i have tried the same file in CentOS and Rhel, The md5 results are different, it is quite headache, who know the tricks?
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it is the exact same file? And not just a similar file but released for that distro? If you copied the file (as binary!) from one machine to another, the md5sum should always be the same, regardless of the oS on the machines.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11881
196
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll turn your statement the other way around Peter.

Since calculating the MD5 hash on a file uses a very specific algorithm that must be the same on all machines, the fact that the resultant hash is different on different operating machines means only one thing - the files are guaranteed to be different.

I'll put one little caveat on this - the MD5 algorithm is very well known, so many people use it within their own software for such things as password encryption. However there are security risks if the same password in different applications created the same hash. Most applications therefore allow for different seeds on an installation basis, which results in different hashes for the same input not only on an application by application basis, but also on an installation by installation basis.

However this caveat is irrelevant for calculating the MD5 hash for a physical file - the seed is guaranteed to be the same on all operating systems, as is the algorithm, so therefore if the files are the same then the hash will be the same.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic