Linux ln Command Tutorial with Examples To Create Symbolic Links – POFTUT

Linux ln Command Tutorial with Examples To Create Symbolic Links

Linux file systems provides different mechanism to make system administrators and applications developer’s life easier. Symbolic links are one of the mechanism. Symbolic links provides the flexibility to use single file in multiple places with multiple names. But at the end there is only single file and data which is pointed by all of these symbolic links.

What is Symbolic Link

For example we have a file name myprogram which is located in /usr/bin and we need the same app with different name located at /home/poftut/erp . /home/poftut/erp will contain a symbolic link to the myprogram .

Create Soft Symbolic Link

Soft symbolic link only provide shortcut for the source file. If we remove the soft symbolic link the source file will not be removed. We can create a soft link with -s option.

In this example we create soft link for linux echo command with a new name myecho in the /home/ismail/ path.

$ ln -s /bin/echo /home/ismail/myecho
Create Soft Symbolic Link
Create Soft Symbolic Link

Detect A File is Symbolic Link

In previous example we have created a soft symbolic link named myecho . But is there a way to detect all ready existing symbolic links? We can use different tools for detection but the simplest way

$ file /home/ismail/myecho
Detect A File is Symbolic Link
Detect A File is Symbolic Link

Create Hard Symbolic Link

Hard links are a bit different from soft links. All hard links to the same files behave like the actual file. If one of the hard link file is removed all other copies will be removed. So while using hard links be cautious. We can create hard link without any option by default.

LEARN MORE  What Is Symbolic or Symlinks? How To Create Symlink For Windows and Linux?

In  this example we will create a hard symbolic link to the myfile .

$ ln myfile myhardlinkfile

Remove Soft and Hard Symbolic Link

We can remove hard or soft symbolic links. This will not remove the original or source file. This will only remove given symbolic link. We will use unlink command for removal.

In this example we will remove the link named /home/ismail/myecho with unlink

$ unlink /home/ismail/myecho

Leave a Comment