A symbolic link, also known as a symlink or soft link, is a special type of file in Linux that points to another file or directory. Symlinks allow you to create shortcuts or aliases to files and folders on your filesystem. They are extremely useful for organizing your filesystem and increasing accessibility to files and folders.
Table of Contents
What is a Symbolic Link
A symbolic link is essentially a pointer to another file or directory on the filesystem. The symbolic link file contains the path to the original file or folder it is linking to.
When you access a symbolic link, the operating system automatically follows the path stored in the link and opens the target file or folder. To the user and applications, the symlink appears as if it was the actual file or folder it links to.
Differences Between Symbolic Links and Hard Links
Symbolic links differ from hard links, another type of file linking method in Linux. The main differences are:
- A symbolic link is a special file containing a text path to another file or folder. A hard link directly references the same inode as the original file.
- Deleting the original file deletes the data, but a hard link preserves that data. Deleting the original file causes a symbolic link to dangle and become unusable.
- Symbolic links can link to files and folders across different filesystems. Hard links only work within the same filesystem.
Why Use Symbolic Links
Here are some common reasons to use symbolic links in Linux:
- Organization – Symlinks allow you to organize your filesystem in a logical way regardless of the actual location of files and folders.
- Accessibility – You can create symlinks in easy to access locations pointing to frequently used files buried deep in your filesystem.
- Relocation – If you move a folder, you can retain all the symlinks that point to files inside it. Only the underlying symlink path needs to change.
- Cross-filesystem links – You can link between files on different filesystems, for example between your root and home partitions.
Creating Symbolic Links with ln
The primary command for making symbolic links in Linux is ln
. It comes with every Linux distribution.
Here is the basic syntax for creating symlinks with ln:
ln -s /path/to/source_file /path/to/symlink
For example, to create a symlink called documents
to your /home/user/Documents
folder:
ln -s /home/user/Documents /home/user/documents
You can also use relative paths for the source file path:
ln -s Documents/ documents
And the symlink can have the same name as the original file or folder if desired.
Linking Files vs Folders
The process for creating symlinks for files or folders is the same.
When creating folder symlinks, link to the folder itself, not its contents. This ensures that path updates as files get added, removed or renamed within the folder.
Symlink Permissions and Ownership
A symbolic link file is separate from the original file. The symlink has its own file permissions and owner, independent of the target.
You will likely want your symlink permissions to match those of the target file. For example, restrict access to sensitive symlinks using chmod.
Symlink Chains
You can create symlinks that link to other symlinks, known as a symlink chain. This allows complex nested structures.
When working with symlink chains, use readlink -f
to resolve the ultimate target file from any link in the chain.
Symlinks vs Aliases
Symbolic links have some overlap in functionality with shell command aliases. However, symlinks work at a filesystem level rather than shell level. This gives symlinks much more flexibility in terms of the contexts and programs in which they work.
Conclusion
Symbolic links are extremely useful for organizing files while retaining access to them from different locations. Mastering symlinks will help streamline your workflow.
The ln -s
command allows simple creation of symlinks from the terminal. Use symlinks liberally to avoid getting lost in deep filesystem paths.