Merging directories in OS X

For a long time, I’ve been wanting to merge two directories in OS X. I have an internal harddrive and an external harddrive, and I wanted to merge a folder on each drive – but only visually: I did not want to move the files between the drives.

I wanted to merge the contents of the folders ‘a’ and ‘b’ into the folder ‘c’:

My friend, Tobias, hinted a possible solution: Make a shell script that runs through each directory and make symbolic links to all subdirectories.
I called the script merge_directories.command:

#!/bin/bash -
# Script by Kristian Risager Larsen, http://kezze.dk/merge-directories-in-unix/

# Use absolute path names
DIRECTORIES="/path/to/a
/path/to/b"
TARGET="/path/to/c"

rm $TARGET/*

for EACHDIR in $DIRECTORIES
do
  for CURRENTDIR in `ls -l $EACHDIR/ | grep '^d' | awk '{ print $9 }'`
  do
    ln -s $EACHDIR/$CURRENTDIR $TARGET/$CURRENTDIR
  done
done
open $TARGET

Remember to make it executable by running

chmod +x merge_directories.command

The result is that the target folder contains links to all subdirectories in the specified folders.
Note that this script is not limited to two source directories.

You will need to run this script every time you create new directories etc – but since this is something I rarely do, and therefore, it is not annoying. I just let it run at startup.

This probably applies to other UNIX variants (You’ll probably have to name the script .sh, and I cannot remember if the “open” command works).
Windows 7 comes with a very easy solution called libraries – this was my reason for doing this.

Skriv et svar

Din e-mailadresse vil ikke blive publiceret.