Introduction
A symbolic link (symlink) references a file or a directory in Linux and other UNIX-like operating systems. Symlinks have many applications, from organizing the OS's tree structure to increasing the accessibility of files on the filesystem.
In this guide, learn how to create symbolic links in Linux using the ln
command.
Prerequisites
- A system running a Linux distribution.
- Command-line access.
- Sudo privileges to access protected files and directories.
What Is a Symlink (Symbolic Link)?
Symlink is a file that stores a path to an existing target (file or directory) on any local or external volume. When a user, application, or system call attempts to access the symlink, the OS automatically interprets the stored path and serves the contents of the target.
Note: Symlinks are conceptually similar to Windows shortcuts. However, the system does not resolve shortcuts automatically, making them readable only by specific programs (Windows Shell, file browsers, etc.).
Soft Links vs. Hard Links
Linux supports two link types: soft links (i.e., symlinks) and hard links. The table below provides the essential points of comparison between the two.
Soft Link | Hard Link | |
---|---|---|
Reference Type | Follows a path to a file or a directory. | Points to the actual file data on the storage volume. |
Compatible Targets | Uses files and directories on local and external volumes. | Can be created only locally as it references a physical location on the volume |
Target Dependence | Does not work after its target file is moved or deleted. | Works after the target file is moved or deleted. |
Usage | Offers quick access to a frequently-used file. | Provides flexibility when organizing a filesystem. |
As seen above, the differences between soft and hard links stem from how they link to their targets. While a soft link references the path to the target, a hard link references the target's data.
Note: In Linux, each file is a hard link to the physical location of its data.
Hard-linking results in two separate files that reference the same data on the volume. This property of hard links has two consequences:
- Changes you make on one reflect on all the hard links.
- Deleting the target file does not affect the other copies.
How to Create Symbolic Link in Linux (ln Command)
Use the ln
command to create links to files and directories in Linux. The sections below provide more information about the procedure, alongside some examples.
Create Symlink for File
To create a symbolic link to a file, open a terminal window and enter the command below:
ln -s [target] [symlink]
The command consists of the following elements:
- The
-s
option instructsln
to create a symlink. With no options specified, the command creates a hard link. - [target] is the file the link references.
- [symlink] is the location to save the link. If this element is omitted, the command places the symlink in the current working directory.
The example below creates a symbolic link (link-file.txt) that points to the target file (target-file.txt) located in the test directory.
ln -s test/target-file.txt link-file.txt
The command provides no output. However, the ls command shows link-file.txt was created:
We add the -l
option to the ls
command to see the properties of link-file.txt:
ls -l link-file.txt
The letter l
at the beginning of the permissions block shows that the file is a symlink. The output also contains the path to the target file.
Create Symlink for Directory
A symbolic link can point to a directory's absolute or relative path. Use the following syntax to create a symbolic link to a directory in Linux:
ln -s [target-directory] [symlink]
The example below creates a symbolic link named test-link in the home (~/) directory. The link references a directory on a mounted CD.
ln -s /media/marko/VBox_GAs_6.1.38/cert ~/test-link
The ls
command shows the newly created symlink in the home directory.
The ls -l
output shows the l
symbol in the permissions block and the path to the target directory.
Finally, the test-link directory's content corresponds to the cert directory's content on the CD.
Note: If the system has a connection to another computer, such as a corporate network or a remote server, symlinks can reference resources on those remote systems.
Overwrite Symbolic Links
When creating a symbolic link, you might receive the following error message:
ln: failed to create symbolic link '[filename]': File exists
The error message means that there is already a file with the same name in the destination. To force the system to overwrite the destination link, use the -f
option:
ln -sf [target] [destination]
Warning: Using the -f
option permanently deletes the existing file.
Find Broken Symbolic Links
The symlink becomes unusable if the original file is moved, deleted, or unavailable (e.g., when a server goes offline). However, the system does not automatically remove broken symbolic links.
To perform the search and locate the links that do not work, use the following find command:
find [directory] -type l ! -exec test -e {} \; -print
Replace [directory] with the path to the directory where you want to look for the broken links. For example, use the (~) symbol to search the home directory:
find ~ -type l ! -exec test -e {} \; -print
Remove Symbolic Links
If a symlink is broken or you do not need it anymore, remove it with the unlink
command:
unlink [symlink]
Replace [symlink] with the path to the link. Alternatively, use the rm command, as you would with any other file:
rm [symlink]
Both commands provide no output if the operation is successful.
Conclusion
After reading this article, you should understand hard and symbolic (soft) links and know how to work with them. The article provided the syntax for the ln
command and offered ways to inspect, overwrite, and remove symlinks on your system.
Next, improve your Linux skills by reading about Linux Commands All Users Should Know.