The symbolic link is a connection to the specified file or folder in Linux. Also, the provided symbolic link points to the source file or folder. In this tutorial, we will try to explain the symbolic link with its types like soft link and hard link and how to create the link.
Symbolic Link Types (Hard Link and Soft Link)
There is two types of symbolic link types canlled hard link and soft link.
- Soft Link is similar to the shortcuts in the Windows operating system. It is just a pointer to the source. The soft link is more popular than the hard link. Different from the hard link a symbolic link can point to another file system file or folder.
- Hard Link is similar to the soft link with a big difference. Hard Link uses inodes in order to link to the source file or folder like it exist in the provided location. A hard link can not be created in different file systems and partitions.
ln Command Syntax
The ln command has the following syntax. The default link type for the ln command is hard link.
ls OPTION SOURCE LINK
- OPTION is used to set the link type or similar.
- SOURCE is the source or actual file or folder.
- LINK is the newly created link for a file or folder.
Create Soft Link For File
The most popular use case for the ln command is creating the soft link. In order to create a soft link, we will use the -s or –symbolic option with the ln command. Then we will provide the source which can be a file and the link. In the following example we willcreate a soft link for the file named myfile.txt with the name of mylink.
ln -s myfile.txt mylink
The absolute of full path can be used to create a soft link. In the following example the file is located under the users home directory and we will create the link in the /var directory.
ln -s /home/ismail/myfile.txt /var/mylink
Show Link Source
Links are popularly used with the Linux distirutions in order to provide different file and folders in different paths without copy and paste. The link sources can be displayed in command line interface and desktop environment by using a file manager. For desktop environments the file managers use different icons to depict a link to a file.
In terminal “ls -l” command can be used to list files and their types where the links will be listed and their source also provided with the -> sing. Also the file type contains the l to describe it is a link type at the start of the permissions.
lrwxrwxrwx 1 ismail ismail 10 Ara 26 17:05 mylink -> myfile.txt
Create Soft Link For Folder or Directory
Not only files are used to create soft links a file or directory link can be created by using the ln command. As stated previously the link type will be set as soft link with the -s or –symbolic parameters. We will create the soft link for the directory named /var/lib inside the user home directory.
ln -s /var/lib /home/ismail/lib
Overwrite Symbolic Link
In some cases a file or symbolic can be exist allready and we may try to create same name link. We will get the following error.
ln: failed to create symbolic link 'mylink': File exists
This requires to over write to the existing link. The -f option is used to over write into the existing link.
ln -f -s myfile.txt mylink
Create Hard Link For File and Folder
Hard link is different from the soft link where the real file inodes are copied into the link. If you rm this link the original file will be deleted too. Hard link creatation do not requires a parameter for the ln command.
ln myfile.txt mylink
We can also create a hard link with the full or absolute path like below.
ln /home/ismail/myfile.txt /var/myfile
Remove or Delete Symbolic Link
The links can be deleted by using the unlink command or rm command. The native command to remove or delete a symbolic link is the unlink command. Just provide the link name or path to remove it. The unlink command can delete both soft links and hard links also with an absolute or relational path.