If you’re just here for the goods – here’s the quick syntax for a symlink ( read on for further notes etc ):
Create a Symlink.
ln -s target_path link_path
target_path – This is the file/folder that you want to create a symlink from.
link_path - This is the path of the location where you want the linked file to reside.
ln – This is the link command and creates a link between two files.
-s – This represents the symbolic part of the command and is what specifies a symlink.
Here’s a real world example, common in nginx server configurations:
ln -s /etc/nginx/sites-available/my_site /etc/nginx/sites-enabled/my_site
will create a symlink from /etc/nginx/sites-available/my_site pointing to /etc/nginx/sites-enabled/my_site.
Now when any updates are made in the target file, these will be transposed to the symlink file. A few points to note:
- Deleting, renaming or moving the symlink will not affect the target file.
- Deleting, renaming or moving the target file will leave the symlink ‘dangling’. That is, the link will not update to reflect the new target file location.
- A symlink is also known as a soft link and a symbolic link
- The link name doesn’t haven’t to be specified; if ommitted the default will be the same as the target’s name. In the above example, the following would be valid and yield the same result:
ln -s /etc/nginx/sites-available/my_site /etc/nginx/sites-enabled/
Why do I need a symlink?
A few reasons spring to mind:
- To use as an alternative to long winded directory paths. Instead of remembering for example, /var/www/mysite/assets/images/blog, you could symlink this to ~/blog_images. Now all you need to do is access blog_images from your home directory.
- When you have two files that need to be synchronised – for example, this is often the case with nginx configuration files : /etc/nginx/sites-available and /etc/nginx/sites-enabled. If you create a symlink between these two files, then there is no danger of the files getting out of sync.
So there you go, a nice short and to the point post. If you would like to be kept up to date with new posts here at the web developing cat, then be sure to add your name to our mailing list below.