File and Folder Links
Created: Last updated:
If you work with a server you usually do some things differently; like with your desktop or laptop computer. It is a little bit of this, lets say, misconception what you can do with a Windows computer versus a Linux computer, or a Mac (lets not forget about them).
Some words about operating systems (OS) first. Windows was developed and invented for the personal computer (PC) as an extension to DOS. I know it was a long time ago but still, Windows OS programs have been and still are created with "personal" users in mind. This is also true for Apple's Mac and it was the first to have a visual experience for users. That's why they attracted the graphic geeks and are popular in that industry.
Linux on the other hand has different roots, the UNIX servers. That's right, attracting the server geeks. Linux server admins (and users) learn the following from the get-go. For Windows users the concept alone can be startling, even for server admins. Don't know about Apple users, you are left behind here, sorry!
Where is your file or folder
Windows trained users always look for files in terms or relation of drives and directories (aka folders). This is some leftover from the days of DOS.
Physically, on a hard disk there is not such thing, though. Well, there are partitions but that is another story. There is primarily a thing called a master file table (MFT) pointing to locations on the hard disk. It is like the table of contents (TOC) or better yet the index at the end of a book pointing to page numbers. Therefore it should be no problem to have more than one instance pointing at one file.
Albeit, it took quite a while for Microsoft to adjust the file table in Windows. The words you might want to google for are FAT and NTFS. However, it was still a missing piece until Windows 2000 with the updated version known as NTFS5 and with it the reparse points. Still, they have not included options in any of the GUI tools; they offered command line tools to work with reparse points! I am still clueless why Microsoft did not bother to include this tools into their operating systems from the beginning, not even with the server versions. You had to get (i.e. buy) the so called Resource Kit. Good news: There should be a tool included now, since Vista.
Get the lingo
Great, now we know you can set pointers from many places to a single file or folder. What is next?
Before I continue and tell you what you need and how you do it we have to talk about words, the lingo for these file and directory pointers in Windows.
From the top of my head I know of about five words floating around: shortcuts, link, junction, hard link and symbolic link.
Shortcuts are well known but don't work from a programming point of view and that is not what you need on a web server. They do not work from PHP and with it Zend Framework, WordPress, Joomla, Drupal you name it.
Shortcuts are more like regular files, stored in the file table, with the .lnk extension. Windows Explorer is basically the only program who can make sense out of them. Note that the "File open ..." menu item you find in programs is hooked up into Windows Explorer; they do not query the file table directly!
I mention the term "links" here because there are some sort of links available. First off, link is just a way to general term. Second most of these links are usually nothing more than hyperlinks similar to links in a web page. These are sometimes HTML or Java based programs. They are not in any way or sense related with the MFT of the OS.
Now it gets interesting. The junction is probably the first known existence of anything that resembles a link to something from more than one point in the file table. Junctions are also known as (because implemented through) reparse points I mentioned earlier with NTFS5. Junctions are limited to local directories! The Resource Kit packages since Windows 2000 have a command line program called linkd to set and read junctions.
There is also a program called junction, originally developed by Sysinternals and Mark Russinovich—Sysinternals is now part of Microsoft Technet.
Because the junction is limited to directories we need something else for a regular file and that would be the hard link. Windows propagated fsutil as the program for managing hard links. It is actually capable of doing more than just that, including reparse point (used for junctions) and wreck your system if you are not careful.
Here is some information at MSDN about hard links and junctions from a software programming point of view.
Today with the latest versions of Windows we have (only) symbolic links. And check this out, we can do everything with Symbolic Links—and some more. And it gets even better! The command line program mklink ships with your choice of Windows since Vista (I hope). Still no GUI, though, at least it has not raised its head for me!
Here is some information at MSDN about symbolic links, again from a strictly programming point of view.
If you develop and work with Vista or higher you should be good to go. If it is XP (like I do) or less you have to revert to the tools mentioned earlier.
How to create these links
Finally, you say! Well, most things have been said already; these are command line programs and if you call them with /? you will get the help menu. That should do it, but wait ...
What was the question again?
Lets take a little example and where you can use these links. I don't know how or why you came to this page; maybe you wonder what the problem or question is. What are these links good for and why should I bother? So, here is why.
Here is the key point. If you update the file it is instantly available wherever you have your link. First you don't have to access the main file in the repository and second the content is always in sync with all your applications. This first point might be a little bit scary to some people and why Microsoft is so petrified in propagating this feature. So my advice: Be careful and know what you are doing!
With hosting providers
One important message first: You cannot manage symbolic links with FTP!
If you have a hosting provider and a hosting plan with Windows Server you depend on your provider and if they have an option in their interface which is normally named something like "File Manager". Unless you have some hosting plan with a dedicated server where you have Terminal Server access. If you have a hosting plan with Linux Server and don't know much about Linux you have to look in the File Manager, too. Linux servers have a thing called telnet (secure shell) but some providers don't allow telnet and this is not about Linux. Honestly, I don't work much with Linux and don't talk about stuff I don't know much, that's why.
In the File Manager look out for the copy, rename, delete and whatnot options. If you are lucky, one of that options might be labeled symlink. That would be your option to create a symbolic link for the selected file or directory. If you don't see anything contact their support but chances are your provider does not support it. I know of a couple providers who for some reason have Linux hosting exclusively but don't have symbolic links in their file manager and don't grant telnet access either. It is mind-boggling!
Your Windows Server
First off and like if have mentioned, all programs are command line tools and therefore require command line access. There are some tools with a GUI out in the wild (see bottom for more) but this is about official tools provided and supported by Microsoft. If you don't have command line access, for whatever reason, you are out of luck.
In your command window type mklink first and if you get the help you are good to go. The help should pretty much tell you all you need.
The symbolic link options ([empty]=default and [/d]) allow you to actually go beyond the current drive and even system, i.e. with the symbolic option you can link to a file or directory on another drive and even the network. You cannot do that with hard links and junctions—in general and with the options [/h] and [/j]!
If nasuser1movies is your network share with some movies and you like to have a symbolic link named "videos" in the current directory you can do this:
- :: Create a directory symbolic link
- mklink /d videos nasuser1movies
- :: Delete the directory symbolic link
- rmdir videos
- :: Create a file symbolic link
- mklink video1.avi nasuser1moviesshow.avi
- :: Delete a file symbolic link
- del video1.avi
Does this mean I recommend symlinks to a share on a web server? No, it does not, I am just making a point here. You can symlink to a network share from your personal computer but I would be very cautious symlinking to a network share. Ask yourself this question: Does my web application crash if the source is not available? Make sure you have file_exist, try/catch or something in place!
linkd and fsutil
If you did not get the help with mklink you probably have an older system like Windows XP and you have to find a download for the Resource Kit. I resist to give a link because in the past Microsoft moved the location so frequently it was not funny. I don't know if they still do that but I am clearly a burned child here. Once you have linkd or fsutil drop them in your system root directory or wherever, just remember where you put them and how to call one.
- :: Create the junction
- :: Delete the junction
- linkd d:wwwserver1publicjscripts /d
- :: Create the hardlink
- :: Delete the hardlink
- del d:wwwserver1publicnewscripts.js
Well, that's about it. Happy linking and again be careful, know what you're doing!
Additonal GUI tools
If you know about a GUI tool or other options let me know; I'll add it to the list below. Please use them at your own discretion and again, know what you're doing.