Using Dropbox to Share (most of) Your Home Directory Across Multiple Computers

2011/10/3

I’m a very happy customer of Dropbox. It allows painless syncing of files across multiple computers without extra features to complicate it. The top rated answer on Quora to the question “Why is Dropbox more popular than other programs with similar functionality?” sums things up perfectly.

One of my favorite uses of Dropbox is to sync almost all of the non-machine specific configuration files and directories in my home directory across all my OSX computers (currently my iMac, MacBook Air, and my work laptop).

Doing this lets me make a configuration change to one computer and have it almost instantly available on any other computer without any manual steps.

This is especially important for my zshell and Vim configurations as I’m always tweaking those, but it’s also helpful to have my Documents, Downloads and Pictures shared.

I have a folder in my Dropbox directory called home, I use a script called link.sh to automatically create symlinks in my home directory to the things I’ve got stored in Dropbox.

Dropbox/home currently has these files and directories in it:

.ackrc
.dbvis
.groovy
.gvimrc
.hg
.hgignore_global
.ssh
.subversion
.vim
.viminfo
.vimrc
.zshenv
Desktop-starling.local/   # unique Desktop for my MacBook Air
Desktop-kestrel.local/    # unique Desktop for my iMac
Desktop-thrush.local/     # unique Desktop for my work MacBook Pro
Documents/
Downloads/
Pictures/
bin/

My Dropbox/home directory also has a shell script in it called link.sh:

#! /usr/bin/env bash
cd $(dirname $0)
 
function linkFile() {
    LINK_TO_NAME=$2
    if [ -z $LINK_TO_NAME ]; then
        LINK_TO_NAME=$1
    fi
    if [ -a $HOME/$LINK_TO_NAME ]; then
        echo "**** Found existing $LINK_TO_NAME, skipping..."
    elif [ -h $HOME/$LINK_TO_NAME ]; then
        echo "Already symlinked $LINK_TO_NAME, skipping..."
    else
        echo "Linking $1 to $LINK_TO_NAME"
        ln -s $PWD/$1 $HOME/$LINK_TO_NAME 
    fi
}
 
 
for F in $(ls -a1 | grep -v link.sh | grep -v Desktop | egrep -v "^..?$" | egrep -v "^.*un~$" | grep -v .DS_Store); do
    linkFile $F
done
 
export HOSTNAME=$(hostname)
 
if [ -d "Desktop-$HOSTNAME" ]; then
    linkFile "Desktop-$HOSTNAME" "Desktop"
else 
    echo "Unable to find Desktop-$HOSTNAME to link to Desktop"
fi

What the script does is:

  1. cd into the directory that the script is located in (it only symlinks files in the same directory)
  2. list out all of the files and directories in the same directory as the script
  3. filter out the things we don’t want to link (like ., .., the link.sh script itself, etc)
  4. For all of the files/directories that pass the filter, call linkFile to create a symlink in the current user’s home directory as long as there isn’t already a file or a symlink there
  5. Then look for a file called Desktop-$HOSTNAME where $HOSTNAME is the name of the current machine and create a ~/Dropbox symlink to it if it’s found.

It should be safe and non-destructive and only create symlinks when there isn’t anything else there with the same name.

I didn’t have my Pictures, Documents, and Downloads in my Dropbox for quite a while and was able to get away with the free 2GB plan. I recently upgraded to a paid Dropbox plan as I wanted those directories shared as well (though I exclude a couple of them from my work MacBook Pro).

For “special” directories like Desktop, Pictures, Documents, and Downloads, I needed to use sudo rm -r [dirname] to remove it before I could create the symlink (BACKUP THE DIRECTORY FIRST).

I’ve been using this for over a year, and haven’t noticed any apps that care that those directories are symlinks.

Also? I have used this shell script many times on my systems, and I think it’s safe, but PLEASE backup before using it, or deleting any directories. An adult crying is not a pretty sight :).

There are 6 comments in this article:

  1. 2011/10/8ixape say:

    For anyone reading this who hasn’t already tried Dropbox – you really should! You won’t change your mind or look back..

  2. 2011/10/12Eric S. Smith say:

    egrep -v "^..?$" is actually filtering out all one- and two-character filenames, since dots are wildcards. If you really want to restrict it to ".." and ".", you should escape the dots:

    egrep -v "^\.\.?$"
    

    There’s a similar issue with grep -v .DS_Store, and technically also with grep -v link.sh, though obviously you’re much less likely to encounter collisions with those — I’d expect something like hyperlink.shtml to bite you first. Use ^\.DS_Store$ and ^link\.sh$ if you care. You might also want to go with ^Desktop, depending on the intended behaviour there.

    Also, I can’t see why you’d need the ^ in egrep -v ^.*un~$ — it looks like you're going to exclude everything ending in un~ either way.

  3. 2011/10/12Eric S. Smith say:

    Ooh, just in case anyone’s tempted to copy and paste: beware the smart quotes in my previous comment.

  4. 2011/10/12tednaleid say:

    Thanks for the suggestions Eric, I’ve included most of them in the post above (and fixed the smart quotes in your comment to be code blocks so they’re normal).

  5. 2012/05/16Mike say:

    Thanks for writing this article! Are you still using Dropbox for your home folder? If so, have you changed anything in the last 7 months since you wrote this?

  6. 2012/05/16tednaleid say:

    @Mike yep, I’m still using a script very much like the one above. Mine has a couple of additional tweaks that are specific to the directories I’m using (some vim related linking/updating plugins stuff and an echo of a reminder about setting /etc/sshd_config to have a flag set), but otherwise it’s exactly the same as the above script I’m using on all my systems to share everything.

Write a comment: