A symbolic link is also known as a symlink or soft link, and it is a special file that points to another file or directory on our system.
In this tutorial, you will understand the ln command in Linux and we will discuss how we can use the ln
command to create a symbolic link.
Types of Symbolic links
Symbolic links have two types in Linux/UNIX :
- Hard links – Hard link is just like an additional name for an existing file. Multiple hard links can be created for a single file. Hard links does not allow to point files or directories of a different filesystem. If we create a hard link for a file and then delete or moved the file , we can still access that file data by using the hard links. Hard links have the actual content.
- Soft links – Soft links work same as shortcut works in windows. Soft links can point files or directories in any filesystem. If we create a soft link for a file and then delete or moved file , we cannot access with soft links and soft link becomes dangling.
Read Also: How to Install Webuzo Control Panel on CentOS 7
How to Use the ln
Command
ln
is a command used for creating links between files or directories. To create Hard links just simply use ln
and for Soft links use ln
with -s
(--symbolic
) option.
Let’s review the syntax of the ln
command.
ln -s [OPTIONS] FILENAME LINKNAME
- If both the arguments are available
FILENAME
andLINKNAME
thenln
will create a link from the first argumentFILENAME
to the second argumentLINKNAME
. - If only the file is available as argument and the second argument is a
.
thenln
will create the link file in the current directory with same name of the file which it points.
Creating Symlink To a File
Use the following command to create the symbolic link for a file :
ln -s source LINK
Provide the name of the file for which we have to create symbolic link at source
and name of the link at LINK
If symbolic_link
is not available, then ln
by default create new link in the current directory:
The symbolic_link
parameter is optional. If you do not specify the symbolic link, the ln
command will create a new link in your current directory:
In the below example, we will create a symbolic link: of file name Test_file.txt
to symbolic link Link.txt
ln -s Test_file.txt Link.txt
To check symbolic links has successfully created or not, use ls -l
command
ls -l Link.txt
Output:
lrwxrwxrwx 1 linuxpanda linuxpanda 5 Oct 2 11:35 Link.txt -> Test_file.txt
The l
represents the symbolic link and ->
represents the file on which symlink points.
Creating Symlinks To a Directory
The command for creating the symbolic link for a directory is same as for creating link for the file. Only we have to replace first parameter and provide the symlink parameter.
In the below example , we will create symbolic link for a directory /home/user/Download
to ~/DirLink
For example, if you want to create a symbolic link from the /mnt/your_drive/music
directory to the ~/my_music
directory you would run:
ln -s /home/user/Download ~/DirLink
Linux distribution did not allow to create Hard Links for the Directories.
Overwriting Symlinks
ln
command always shows an error while creating a symlink which already exists, To overcome this error we have to create the link forcefully or create with a new name .
ln -s Test_file.txt Link.txt
ln: failed to create symbolic link 'Link.txt': File exists
Use the -f
(--force
) option to overwrite Symbolic link.
ln -sf Test_file.txt Link.txt
Removing Symlinks
To delete symbolic link we can use unlink
command, otherwise directly delete the link with rm
command.
Let’s review the syntax of the unlink
command
unlink symlink_to_delete
We can also use rm
command to delete symbolic link:
rm symlink_to_delete
Always remember while removing symbolic links do not use /
after the symlink name, it may delete the content in the link but not the link.
The symbolic link will become dangling (broken) if we remove or move the source file of the link.
Conclusion
In Linux, to create symbolic links use ln
command (Hard link) and ln -s
(Soft links)
For the complete detailed information about ln
command, visit man ln
or ln --help
in the Linux terminal.
If you guys have any queries related to this, let me know in the comments.
Leave a Reply