Symbolic Links Quick Reference
Introduction
symbolic link, also termed a soft link, is a special kind of file or folder that points to another file or folder, much like a shortcut in Windows or a Macintosh alias. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system.
This reference only deals with folder symbolic links like a 'photos' folder link that, when you open it, appears to contain all the directories and files of the real target folder somewhere else on the system. And changes made here are reflected in the real folder. The icon for a folder symbolic link looks like a folder with a little arrow on it.
How Folder Symbolic Links Work
A normal directory listing for a file or folder points to a discrete area of the hard drive where it's data is stored. A symbolic link also points to the same area of the hard drive, so no matter how many symbolic links point to it, there is only one copy of the target files.
The big difference between them is this: if you delete a symbolic link, only the link is removed. If you delete a normal directory listing the listing and all of it's content are removed.
Folder symbolic links pointing to the same media files do have the advantage of saving disk space, but the biggest benefit is that one can add to, and maintain media files in one place and the updates will appear everywhere. For example, you could have a dozen version-specific TNG test sites, each with Symlinks pointing to the same media files. Maintaining and backing up multiple collections of the same media files is difficult at best.
Important: This will be obvious to some, but still bears repeating. For those who would consolidate all their media folders under a master folder like exhibits, both in the remote location and in the TNG root, there only needs to be a single folder symbolic link -- exhibits -- in the TNG root which points to the remote folder. With that one link, all the real folders and files in the remote folder will be available in the TNG root just as if all the content were still in the root. On the other hand, if one moves each media folder (photos, documents, etc.) to separate remote locations, then they will need a folder symbolic link in the TNG root for each one of them.
How to Create Them
Below we provide the syntax or code for creating a folder symbolic link on Windows 10, Linux and on your shared hosting website.
Windows 10 Command Line
- Type cmd in the search box.
- Right-click on Command Prompt
- Select Run as administrator.
- Navigate to directory where you want to create the symbolic link.
Syntax
mklink /D link_name real_target_folder
/D creates a directory symbolic link named "link_name" in the current directory. It will point the "real_target_folder".
Example
D:\documents> mklink /D symlink_folder "..\real_folder"
- Note the quotes around the target path.
- We are in the directory (documents) where we want to place the symbolic link.
- We use the command
mklink /D
so we will be creating a directory symbolic link. symlink_folder
is the name of the directory symbolic link to create.../real_folder
is the target folder whose contents will appear when you opensymlink_folder
.
Linux Terminal Command Line
Open a console window and navigate to the folder where the directory link is to be created.
Syntax
ln -s -d target symlink_name (command is lower case LN)
note: this is opposite to the arguments in windows mklink where the symlink_name comes first
-s creates symbolic link as opposed to hard link -d creates a directory link (a file link is default)
Example
To create a folder -- exhibits
-- in current directory pointing to a real folder of the same name above the document root:
ln -s -d /home/rick/bfam/exhibits exhibits
In this example we will create a directory symbolic link in the document root named symfolder and point it to real_folder located in the private area above the document root.
Use the following as a guide, create a php file and drop it into your site, then run it from the browser. Adjust the path values to match your own site's path, and change the real_folder and symfolder names to suit your needs.
<?php
if( !symlink( “/home/rick/real_folder”, “/home/rick/public_html/symfolder” ) {
echo “Cannot create link!”;
exit;
}
else {
echo “Symbolic link created!”;
exit;
}
?>
https://myfamily.com/makelink.php. After the script is run successfully, you should delete it from the website to prevent a mischief maker running it.
Related Links
- Symbolic Links an in depth discussion of symbolic links
- The Complete Guide to Creating Symbolic Links (aka Symlinks) on Windows
- How to Create and Use Symbolic Links (aka Symlinks) on a Mac