Operating System - HP-UX
1833694 Members
3326 Online
110062 Solutions
New Discussion

Does cksum work for folders

 
Roshan George Cheereth
Occasional Contributor

Does cksum work for folders

I have a situations where in I move a folder structure onto multiple servers and have to validate that all the servers have recieved it without any data corruption.

I tried cksum on the folders but it didn't come back with matching results. But if I check the individual files within the folders, those match.

So I have couple of questions here,
1. Does cksum work for folders with some option?
2. If not, is there any way to validate the data integrity of the folders in question?

Let me know.
Thanks,
Roshan.
6 REPLIES 6
Mel Burslan
Honored Contributor

Re: Does cksum work for folders

cksum does not have any command line switches of any kind, under 11i. The best would be to use something like

foldersum=0
for file in `ls -1 /my/directory/level/*`
do
cs=`cksum $file`
(( foldersum=$foldersum+$cs ))
done
echo $foldersum

it is not an error proof way butit is highly unlikely that one file will have enough dicrepancy to compensate and zero out the other discrepancies.

Hope this helps
________________________________
UNIX because I majored in cryptology...
A. Clay Stephenson
Acclaimed Contributor

Re: Does cksum work for folders

First of all, UNIX guys have no idea what a folder is; there is no mkfolder command. Cksum will work on a directory but it is simply the cksum of the directory itself and not of the directory's contents. It is possible to do what you but it would require a script to test each file. Here's another approach that should do what you want:

Create a script like this:


find . -type f -print | sort | while read FNAME
do
cksum "${FNAME}"
done

--------------

Now cd to source directory and run the above script:

sh myscript.sh > /var/tmp/src.txt

Next, cd to destinationation directory and run the script again.

sh myscript.sh > /var/tmp/dest.txt

Next diff the two files:
diff /var/tmp/src.txt /var/tmp/dest.txt

If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Does cksum work for folders

cksum of a directory won't provide consistent data.

If you cksum a directory before copying, the original directory may have "holes" and left over data from the addition and removal of files under that directory.

When the folder is copied somewhere, the new folder will be cleaned up and thus the cksum will be a different number, even though "logically" the structures are the same.

HTH

-- Rod Hills
There be dragons...
Mark Ellzey
Valued Contributor

Re: Does cksum work for folders

Roshan,

If you have to distribute the same directory or files to multiple servers, you should use rdist, or get rsync.
(from : http://samba.anu.edu.au/rsync/)

rdist will do binary comparisons of files and update only if they differ.

See man rdist

Regards,
Mark
Roshan George Cheereth
Occasional Contributor

Re: Does cksum work for folders

Thanks guys for the answers. Scripting seems to be the only solution here, I was expecting something smarter like cksum on folders or something close to it.

Rodney, I don't think its due to holes of addition and removal of files. I had thought about it and validated that its not the case.

Mark, I am using rsync but I have seen issues where in it had issues over a large set of servers and a huge folder structure. I wanted an additional validation check apart from running rsync multiple times.
A. Clay Stephenson
Acclaimed Contributor

Re: Does cksum work for folders

As usual, UNIX gives you the basic building blocks but what you do with them is up to you. This is a very straightforward script but normally cksum'ing is not necessary because all your need to do is look at the exit status of the copy command. If you are ftp'ing then things are a bit more tricky but even in that case using Perl's Net::FTP package, you get error checking for free.
If it ain't broke, I can fix that.