Linux Unix Symbolic Soft and Hard Links with ln Command
Linux provides different ways to work with files. Generally a file is accessed directly. But there are some situations to create links those are used as original file.
As we will see in the following of this tutorial there is two type of links named soft link and hard link. They are both link but their behavior are different from each other.
Hard links are two way links where a change in the link will affect to the original file and also the reverse is true. There are some restrictions for hard links.
- Hard links can not link directories
- Hard links can not link to other files systems than source like two separate partition.
Soft or Symbolic Link
Soft links are one way there a change in the link will no affect the source file.
What is ln Linux?
Ln is Linux tool to manage symbolic links.
ln becomes very useful when there is file restrictions or file name related issues.
Man page of ln can be accessed from a linux box like below.
$ man ln
If you prefer web page about ln man look page below.
As we see ln syntax is like below.
ln OPTION TARGET LINK_NAME
Create Hard Link
We can create hard links by using
ln command. We do not need to provide any options. We will just provide the source and destination or hard link path. In this example we will create a hard link name
MyInput to the file named
myinput.py like below.
$ ln myinput.py MyInput
Create Symbolic Link
Linux provides a mechanism named symbolic link. It can be created a soft link like below. In other ways we call it creating symbolic link Linux. In this exmaple we will create a symbolic or soft link name
RealInput where it will point to the
$ ln -s myinput.py RealInput
We can see that symbolic link files are shown in different color if bash coloring is enabled. It is shown that
RealInput is linked to the file
Remove Symbolic Link
Removing symbolic links is easy as removing file. Rm command is used to remove symbolic link but original file stays without effecting this. We will remove
RealInput soft link.
$ rm RealInput