A symbolic link, commonly called a symlink, is a special type of file that points to another file or directory. Instead of containing data itself, a symlink acts as a reference to another location on the filesystem.
Symlinks are commonly used to create shortcuts, share files between locations, or make files and directories accessible from multiple paths without duplicating data.
Table of Contents
Contents
- Types of Links
- Using the
lnCommand - Creating a Symlink to a File
- Creating a Symlink to a Directory
- Overwriting an Existing Symlink
- Removing a Symlink
- Summary
Types of Links
Linux and UNIX systems support two types of links:
Hard Links
A hard link is an additional name for an existing file.
Characteristics:
- Multiple filenames can point to the same file data.
- Changes made through one link are visible through all other links.
- Hard links cannot point to directories.
- Hard links cannot span different filesystems or partitions.
Symbolic Links (Soft Links)
A symbolic link is similar to a shortcut in Windows.
Characteristics:
- Can point to files or directories.
- Can point to locations on different filesystems or partitions.
- Contains a reference to the target path rather than the actual data.
Using the ln Command
The standard Linux command used to create links is:
ln
By default, ln creates hard links.
To create a symbolic link, use the -s option:
ln -s [OPTIONS] TARGET LINK_NAME
Where:
TARGETis the existing file or directory.LINK_NAMEis the name of the symbolic link to create.
If the command completes successfully, no output is displayed.
Creating a Symlink to a File
To create a symbolic link to a file:
ln -s source-file symbolic-link
Example
ln -s my-file.txt my-link.txt
This creates a symbolic link named:
my-link.txt
that points to:
my-file.txt
Verifying the Symlink
Use:
ls -l my-link.txt
Example output:
lrwxrwxrwx 1 username users 11 Dec 2 23:03 my-link.txt -> my-file.txt
The leading:
l
indicates that the file is a symbolic link.
The arrow:
->
shows the destination to which the link points.
Creating a Symlink to a Directory
Creating a symbolic link to a directory uses exactly the same syntax.
Example
ln -s /images ~/pictures
This creates:
~/pictures
as a symbolic link pointing to:
/ images
Any access to the pictures directory will automatically redirect to the images directory.
Overwriting an Existing Symlink
If a symbolic link with the same name already exists, the command will fail:
ln -s my-file.txt my-link.txt
Output:
ln: failed to create symbolic link 'my-link.txt': File exists
To overwrite an existing symbolic link, use the -f (force) option:
ln -sf my-file.txt my-link.txt
This replaces the existing symlink with the new target.
Removing a Symlink
You can remove symbolic links using either the unlink or rm command.
Using unlink
unlink symlink-name
Example:
unlink my-link.txt
Using rm
rm my-link.txt
Both commands remove only the symbolic link and do not affect the original file or directory.
Important Note
Do not add a trailing slash (/) when removing a symbolic link to a directory.
Incorrect:
rm my-link/
Correct:
rm my-link
Broken Symlinks
If the original file or directory is moved or deleted, the symbolic link remains in place but no longer points to a valid location.
Such a link is called a broken symlink.
Example:
rm my-file.txt
The symlink:
my-link.txt
still exists but no longer works because its target has been removed.
Common Use Cases
Create a Shortcut
ln -s /home/user/projects/project1 ~/project1
Share a Directory Between Applications
ln -s /storage/uploads /var/www/html/uploads
Switch Application Versions Easily
ln -s php83 php
Updating the symlink later allows you to switch versions without changing application configurations.
Summary
To create a symbolic link in Linux, use:
ln -s TARGET LINK_NAME
Useful commands:
Create a symlink:
ln -s source-file link-name
Create a directory symlink:
ln -s /source-directory /target-link
Overwrite an existing symlink:
ln -sf source-file link-name
Remove a symlink:
unlink link-name
or
rm link-name
Symbolic links are a powerful way to simplify file management, create shortcuts, and organize applications without duplicating files or directories.