Symbolically merging one directory into another

I found this technique necessary when working with vBulletin. I want to keep the core directory clean for easy upgrades (which often involves replacing the directory with the new release) and I don’t want to have to tediously copy files in each time. Solution: keep the files separate and recreate symbolic links on the fly.

# First remove any pre-existing links
find . -type l -print0 | xargs -0 rm;
# Then symbolically link dir2 into dir1
for file in `cd dir2; find`; do
	if [ ! -e dir1/$file ]; then
		depth=`echo $file | awk -F '/' '{print NF}'`;
		prefix=`perl -e 'print "../"x$ARGV[0]' $depth`;
		ln -s "$prefix"dir2/$file dir1/$file;
	fi
done

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>