Linux Unix Symbolic Soft and Hard Links with ln Command – POFTUT

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 that are used as original file.

Link Types

As we will see in the following of this tutorial there are two types of links named soft link and hard link. They are both links but their behavior are different from each other.

Hard Link

Hard links are two-way links, where a change in the link will affect 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 partitions.

Soft or Symbolic Link

Soft links are one way there a change in the link will not affect the source file. ln command is a Linux tool to manage symbolic links. ln becomes very useful when there is file restrictions or file name related issues.

ln Command Syntax

As we see ln syntax is like below.

ln OPTION TARGET LINK_NAME
  • `OPTION` provides different behavior like a soft and hard link for the ln command.
  • `TARGET` is the file or folder we want to create link.
  • `LINK_NAME` is the new link name and path we will create.

ln Command Help

We can list help information about the ln command with the --help option like below.

$ ln --help
ln Command Help
ln Command Help

Create Hard Link

We can create hard links by using ln command. We do not need to provide many options. We will just provide the source and destination or hard link path. In this example, we will create a hard link named MyInput to the file named myinput.py like below.

$ ln myinput.py MyInput
Create Hard Link
Create Hard Link

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 example, we will create a symbolic or soft link name RealInput where it will point to the myinput.py

$ ln -s myinput.py RealInput
Symbolic Link Linux
Symbolic Link Linux

We can see that symbolic link files are shown in a different color if bash coloring is enabled. It is shown that RealInput is linked to the file myinput.py with lscommand.

LEARN MORE  How To Remove Symbolic Links In Linux?

Remove Symbolic Link

Removing symbolic links is as easy as removing a file. Rm command is used to remove the symbolic link but the original file stays without effecting this. We will remove RealInput soft link.

$ rm RealInput

Leave a Comment