Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I began to use org-mode for planning out my tasks in GTD-style system. Putting every org files in a directory of a Dropbox folder, I run emacs to edit / manage these files from three different local machines: Cygwin, Mac OS X, and Debian. As I also use MobileOrg to access these org files from my iPad, a checksums.dat file must be kept up-to-date when any changes are made. This can be done by running md5sum *.org > checksums.dat.

The problem is that there are three different commands for md5sum command: md5sum.exe in Cygwin, md5 in Mac OS X, and md5sum in Debian. The most ideal situation is a makefile, stored in a Dropbox foder, detects which command is available in the current machine and runs that command to do the md5 checksum operation.

share|improve this question
gnu autotools –  Kevin Jan 13 '12 at 4:19
@Kevin That sounds like an overkill. I don't want to install a program in three distinct systems. In each system, a Dropbox folder is accessible as a local directory. When I run make checksum to generate checksum data under a Dropbox folder, I want the makefile uses an appropriate command after detecting which checksum command is available in the current machine. –  Federico Magallanez Jan 13 '12 at 4:44
If you are going to do a significant amount of scripting, and the project is small, I'd suggest scons. –  Faheem Mitha Jan 13 '12 at 8:58
add comment

4 Answers

up vote 4 down vote accepted

Possible commands to generate a checksum

Unfortunately, there's no standard utility to generate a cryptographic checksum. There is a standard utility to generate a CRC: cksum; this may be sufficient for your purpose of detecting changes in a non-hostile environment.

I would recommend using SHA1 rather than MD5. There aren't many systems that have an MD5 utility but no SHA1, and if you're going to use cryptographic checksums, you might as well use an algorithm with no known method to find collisions (assuming you also check the size).

One tool that's not standard but common and can calculate digests is OpenSSL. It's available for Cygwin, Debian and OSX, but unfortunately not part of the default installation on OSX.

openssl dgst -sha1

On OSX 10.6, there is a shasum utility, which is also present on Debian (it's part of the perl package) and I believe on Cygwin too. This is a Perl script. Most unix systems have Perl installed, so you could bundle that script alongside your makefile if you're worried about this script not being available everywhere.

Selecting the right command for your system

Ok, let's say you really can't find a command that works everywhere.

In the shell

Use the type built-in to see if a command is available.

for x in sha1sum sha1 shasum 'openssl dgst -sha1'; do
  if type "${x%% *}" >/dev/null 2>/dev/null; then sum=$x; break; fi
if [ -z "$sum" ]; then echo 1>&2 "Unable to find a SHA1 utility"; exit 2; fi
$sum *.org

GNU make

You can use the shell function to run a shell snippet when the makefile is loaded and store the output in a variable.

sum := $(shell { type sha1sum || type sha1 || type shasum; } 2>/dev/null)
%.sum: %
        $(sum) $< >$@

Portable (POSIX) make

You can only run shell commands in rule, so each rule that computes a checksum has to contain the lookup code. You can put the snippet in a variable. Remember that separate lines in rules are evaluated independently. Also remember that $ signs that are to be passed to the shell need to be escaped to $$.

determine_sum = \
        sum=; \
        for x in sha1sum sha1 shasum 'openssl dgst -sha1'; do \
          if type "$${x%% *}" >/dev/null 2>/dev/null; then sum=$$x; break; fi; \
        done; \
        if [ -z "$$sum" ]; then echo 1>&2 "Unable to find a SHA1 utility"; exit 2; fi

checksums.dat: FORCE
    $(determine_sum); \
    $$sum *.org
share|improve this answer
add comment

It's something of a hack, but you could test whether each exists and execute them if they do:

[ -x "`which md5 2>/dev/null`" ] && md5 newfile >>sums
[ -x "`which md5sum 2>/dev/null`" ] && md5sum newfile >>sums

I'm quite certain cygwin recognizes commands (including md5sum) without the .exe, but include it if you have to.

share|improve this answer
which exit values are not portable. Use type instead (and you probably want to use if; then; elif to avoid having problems in there are multiple executables). which and type only look for executables anyway, so your test with -x is pointless. –  Chris Down Jan 13 '12 at 7:00
more on using "type" : cmd=$(for cmd in foo md5 md5sum bar; do type $cmd >/dev/null 2>&1 && echo $cmd && break; done); $cmd file1 file2 file3 > md5.txt –  michael_n Jan 14 '12 at 0:00
add comment

Either GNU autotools (as mentioned in a comment), or also CMake is an option.

GNU autotools are the more traditional approach, but (and perhaps speaking only for myself) I don't think people are really too excited at the prospect of initially setting a project up using them. Well, it's a learning curve, in any case. CMake is the newer kid on the block, and perhaps less common (and still has a learning curve). It has its own syntax, and it generates makefiles for your platform. CMake does has the distinct advantage of being not so un*x-centric; it works reliably on unix, windows and mac as well (e.g., it can generate msvc projects, for instance, instead of makefiles.)

Edit: and of course since 'makefiles' were suggested, this answer goes along with that presumption. But perhaps just a python script or (gasp) a simple Java program would be more appropriate for this task; the scripting/programming language would do this the same across platforms.

share|improve this answer
add comment

Since you are running emacs, then you could use emacs itself to compute the checksums: http://www.gnu.org/software/emacs/manual/html_node/elisp/MD5-Checksum.html.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.