Symlinks in Unix

Posted by Stefan Kecskes on Friday, March 15, 2019

Create a link in Unix

A symbolic link is a special form of a file. Actually it is not a file just a pointer to a file. This link points to another file somewhere in the file system. You can create link to files and folders. By the look, feel and functionality it is same as Windows shortcut or alias on iOS systems. If the content of the original file changes, so does the content of symlink. If the original file is moved, renamed or deleted, then the symbolic link stops working correctly (called hanging link). Removing the link doesn’t affect the original file in any way. You can symlink files across network too. The symbolic link is often called a soft link or shortly symlink.

To create a symbolic link, type this command:

ln -s <original source file> <target linked file>

It is good practice to use full system path altogether with filename.

There are also Hard links, which reference rather to the source of file. While Symbolic link references to the pathname of the target. So the Hard links have actual file contents. The original filename is actually a hard link too. So when you create a new hard link you are pointing to the same physical file in the system. The new hard link is added to list of links for the file. The Hard links can link only to files. We cannot create hard links for folders to avoid recursive loops. If the original link is removed the new hard link will still show the content of the file. Effectively removing any link, just reduces the link count, but doesnt affect other links.

To create hard link, type this command:

ln <original source file> <target file>

That’s all. Enjoy linking!