Skip to content

Instantly share code, notes, and snippets.

@mjf
Created March 28, 2013 10:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjf/5262353 to your computer and use it in GitHub Desktop.
Save mjf/5262353 to your computer and use it in GitHub Desktop.
Shell script to detect file or directory has changed using ls, sha1sum, mv and cmp
#! /bin/sh
checksum=`echo $@ | sha1sum - | cut -d ' ' -f 1`
if ! [ -e /tmp/$checksum.ls-ZlaR ]
then
if ! ls -ZlaR $@ > /tmp/$checksum.ls-ZlaR
then
echo changed: could not create /tmp/$checksum.ls-ZlaR 1>&2
exit 1
fi
fi
if ! mv /tmp/$checksum.ls-ZlaR /tmp/$checksum.ls-ZlaR.ref
then
echo changed: could not rename /tmp/$checksum.ls-ZlaR to /tmp/$checksum.ls-ZlaR.ref 1>&2
exit 1
fi
if ! ls -ZlaR $@ > /tmp/$checksum.ls-ZlaR
then
echo changed: could not create /tmp/$checksum.ls-ZlaR 1>&2
exit 1
fi
if cmp -s /tmp/$checksum.ls-ZlaR /tmp/$checksum.ls-ZlaR.ref
then
exit 0
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment