I have some install scripts I wrote for setting up websites which compose several source folders into one destination. Usually I'd use softlinks, but PHP scripts often try to work out their real location on the system so softlinks are often no good.
Instead of making full copies of the source into the destination, wasting disk space, I decided that composing all of the sources into the destination by copying the directory structure and then hard-linking the files would be a much better idea. So, here's a tool to do just that!
I named it 'lnr' - as in 'ln recursive'.
http://www.ben-xo.com/lnr
I hope someone finds this useful.
All of the meat of the script is in the last 7 lines.
Code:
#!/bin/bash
function usage() {
printf "Usage: %s: [-v] [-s] [-h] source dest\n %s -h for help.\n" `basename $1` `basename $1`
exit 2
}
function showhelp() {
printf "Usage: %s: [-v] [-s] [-h] source dest\n" `basename $1`
echo "Recursively hard-links the contents of SOURCE into DEST."
echo " -v: verbose"
echo " -s: use softlinks"
echo " -h: this help message"
exit 2
}
VFLAG=
SFLAG=
SOURCE=
DEST=
while getopts hsv name
do
case $name in
v) VFLAG=-v;;
s) SFLAG=-s;;
h) showhelp $0;;
[?]) usage $0;;
esac
done
shift $(($OPTIND - 1))
SOURCE=$1
DEST=$2
if [ ! -d "$SOURCE" ]; then
echo "'$SOURCE' is not a directory."
usage $0
fi
if [ ! -d "$DEST" ]; then
echo "'$DEST' is not a directory."
usage $0
fi
# The real work.
(cd $SOURCE; find . -mindepth 1 -name .svn -prune -or -type d -print) | (cd $DEST; while read i; do
mkdir -p $VFLAG -- "$i"
done)
(cd $SOURCE; find . -mindepth 1 -name .svn -prune -or -type f -print) | while read i; do
ln -f $VFLAG $SFLAG -- "$SOURCE/$i" "$DEST/$i"
done
Oh yes. Forgot to mention: it also prunes '.svn' directories, as that's what I work from. I should probably have parametrised that...