[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: cp output format



Quoting Nicolas George (george@nsup.org):
> Le nonidi 29 messidor, an CCXXIII, Andrew McGlashan a écrit :

> >         md5_1=$(md5sum $HOME_DIR/.forward|cut -d\  -f1)
> >         md5_2=$(md5sum $wrk_dir/$fix_name/.forward|cut -d\  -f1)
> 
> You can write "md5="${md5%% *}" instead of using cut, one fork+exec less.
> 
> And you can write "md5sum < file" to have a dash instead of the file name,
> and therefore not need to remove it.
> 
> This is minor in this case, but in newly written code, avoid MD5, better
> use SHA-2.
> 
> And of course (unless the files are large (unlikely for .forward) and on the
> same mechanical drive), cmp file1 file2 is much simpler.

I may've missed something here. I can't think why computing the
md5/sha-2 digest would ever be better or simpler than cmp, even
if the files are large and/or on the same spindle).

In fact I can't think why you'd ever compute a digest just to
throw it away like this:

> >         if [ "$md5_1" != "$md5_2" ]

(with no further usage).

It's obviously beneficial if one of the files is remote and the
digest is being computed by the remote host at a speed greater than
the network could transfer it, but that's not the case with this
simple script, is it.

When I say beneficial, I mean in the case where most of the files
are likely to be identical. Otherwise, I don't see how anything
can be faster than   cmp --quiet   because cmp quits at the first
difference, viz:

$ time cat /usr/src/linux-source-3.16.tar.xz > /dev/null

real    0m8.621s
user    0m0.004s
sys     0m0.136s
$ time cat /usr/src/linux-source-3.16.tar.xz | cmp --quiet - ~/.bashrc

real    0m0.433s
user    0m0.000s
sys     0m0.000s

(both preceded by # echo 3 > /proc/sys/vm/drop_caches of course)

Cheers,
David.


Reply to: