Meaningless Drivel is fun!*
The moose likes Linux / UNIX and the fly likes ask for korn shell Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "ask for korn shell" Watch "ask for korn shell" New topic

ask for korn shell

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416

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

Joined: Mar 28, 2003
Posts: 11404

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:

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964

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.

Customer surveys are for companies who didn't pay proper attention to begin with.
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
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

Joined: May 14, 2008
Posts: 5812

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.

JBoss In Action
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404

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.
Don't get me started about those stupid light bulbs.
subject: ask for korn shell
Similar Threads
Why is Java's MD5 different from MySQL MD5?
XPath expression constrained by a predicate doesn't work (//element[@attribute="value"])
MD5 Class
root.getRootElement() is throwing an NullPointerException
build problems and relative paths