Resolve Symlinks When Copying Files With Rsync
In a project I’m working on there is a structure where images and css are common for some of the subprojects. One folder has static pages for testing HTML + CSS layout. This folder has some symlinks to the /css folder and /images folder.
Now, on my Mac during normal development, this works perfectly. But I also wanted to access the static HTML files via my virtual windows machine. And I cannot follow the unix symlinks from my virtual windows machine!
My solution was to copy the files to another folder with rsync, which can then resolve the symlink into real files.
#!/bin/sh
# set -x
# Script will copy contents of current dir (except the files defined in _exclude_list)
# into it's own separate folder. It will also expand symlinks so that windows users
# can use the script directly
# Created 2009-10-14 by Jesper Rønn-Jensen
echo ".*
_*" > _exclude_list.txt
cd `dirname $0`
BASE=`pwd -P`
DEST=`dirname ~/src/htmlguide` #remember trailing slash of parent
FLAGS=' -av --exclude-from _exclude_list.txt --delete'
rsync $FLAGS --copy-dirlinks $BASE $DEST/ #remember trailing slash at destination
The rsync flag --copy-dirlinks
does the magic by expanding all symlinks and copying the files. Besides from that, I make sure to --delete
files no longer present in the source folder.
I decided to use rsync in stead of for example a webserver, as this approach is more portable, and I can then zip the static files or deploy them elsewhere.
If you want to improve on the script, feel free to fork it on github gist 210796.
October 20th, 2009 at 15:02 (GMT-1)
i think it would be better to create two different folder for codes and for stuffs (images and libraries), and then merge them to one, and then access it from server, that will be great idea to differ them.
March 9th, 2012 at 04:18 (GMT-1)
Hi Jesper,
I found this post looking for the –copy-dirlinks flag. Unfortunately, whatever post-processor is being used on thsi blog is outputting non-ascii dashes and I ended up with the following output:
rsync: -\#342\#200\#223copy-dirlinks: unknown option
Perhaps you could update this post to use ascii minus instead, so others don’t run into this problem.