Register
It is currently Wed Oct 01, 2014 10:14 am

'lnr' - composes one directory into another using hardlinks


All times are UTC - 6 hours


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Sun Mar 09, 2008 6:41 pm   

Joined: Sun Mar 09, 2008 6:35 pm
Posts: 1
Location: London UK
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...


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP