Symlinks on OS X - Just a Reminder

If you work in the terminal a lot like most developers then you’ll be familiar with the concept of symlinks. Unfortunately, for some reason, it’s really easy to forget exactly how they work and what the order of arguments should be. Today I created a Git repository for a new Wordpress theme I’m coding in a folder that holds all of the design and code assets for an organization I’m doing some work for. The theme folder itself is outside the Wordpress themes directory itself because I don’t want to be committing the entire Wordpress codebase to my repo when all I want to keep under version control is my theme. Yes, I know I could have just created the repo in the theme folder within Wordpress but there’s a good reason I didn’t which I won’t get into. Anyway, in the process of doing this I needed to create a symlink between the actual codebase for the theme and Wordpress’ themes folder. I totally blanked on how to properly symlink two folders and every article on Google was way too wordy and confusing. So here for my reference and yours, is a simple, straightforward guide to creating symlinks on a Mac (it’s the same for Linux, I’m just using a Mac today).

Okay, so let’s say I have two folders. original_folder is a directory with some files in it that I do not want to move. I also have hypothetical_path which is a symlink that will look and act like a new folder, in any location I want, that when accessed will show all the files that original_folder has in it. Let’s say my paths look like this:

1
2
3
4
# Path to original folder
~/User/original_folder
# Path where you want a new folder pointing to the original's contents
~/Sites/wordpress-themes/hypothetical_path

The way you make a symlink out of those is:

ln -s <original> <new>

In the example above <original> is the folder that already exists and <new> is where you want the symlink to live. So using our example we would:

ln -s ~/Users/original_folder ~/Sites/wordpress-themes/hypothetical_path

And now whenever you go to ~/Sites/wordpress-themes/hypothetical_path you’ll see the contents of the original_folder. Bonus: Changes you make in either folder will be synced between them. Remember, the symlink isn’t a real folder. It’s partially a shortcut for your convenience and it acts as a way for various applications to access copies of files and folders in locations different than the originals. This is useful for when an application (like Wordpress for example) needs files and folders in a certain location but you, for whatever reason, want them elsewhere. A symlink lets you have your cake and eat it too. Remember, deleting the symlink will not delete the original files or folders that are symlinked but deleting the original will break the symlink. Also, if you’ve symlinked an entire directory and delete a file within that symlinked directory, then you are actually deleting the original file, not a copy.

The best way to understand these things is to just use them. This is just a quick reference for the next time I forget how to symlink a directory. If you’re looking for an in-depth explanation of symlinks then look elsewhere.

Server administration, Web development

« Flat Design - Great for Lazy Designers Line Breaks in Wordpress Text Mode »

Comments