How to Create Symbolic Links Using ln Command in Linux

How to Create Symbolic Links Using In Command in Linux
How to Create Symbolic Links Using In Command in Linux

Create Symbolic Links Using ln Command in Linux

Symbolic Link or Soft Link is a special type of file that refers to the symbolic path of another file. Symbolic links can work in any type of filesystems. In this tutorial, you are going to learn how to Create Symbolic Links Using In Command in Linux.

There are two types of Links in Linux:

  1. Hard Link : Hard link refers to the abstract location of specific physical data.
  2. Soft Link: Soft Link or Symbolic Link refers to the symbolic path of another file.

Basic Syntax For ln Command

For creating any type of links in Linux ln command is used. By using ln command you can create hard links as well as soft links. Following is the basic syntax for ln command in Linux.

ln [OPTIONS] FILE LINK

Where,
OPTIONS: These are the options that can be used with ln commands.
FILE: The actual file whose link will be created.
LINK: Name of the link that will be created.

Basically, ln command by default will create a hard link without any options. If you want to create a soft link then you should use -s option with it.

To create a symbolic link you should type in terminal:

ln -s FILE_NAME SYMBOLIC_LINK_NAME

Create a Symbolic Link

Now we are going to create an actual symbolic link. In the following example, it will create a symbolic link of file first_file.txt named first_link.txt

ln -s first_file.txt first_link.txt

To confirm the symbolic link is created run following command in Linux:

ls -l first_link.txt

The output should be:

lrwxrwxrwx. 1 linux4one linux4one 14 Apr 26 20:45 first_link.txt -> first_file.txt
How to Create Symbolic Links Using ln Command in Linux - output
How to Create Symbolic Links Using ln Command in Linux – output

In above output first_link.txt -> first_file.txt shows symbolic link is created.

If the symbolic link already exists you will get an error as the link already exists. At such time you should use -f force flag to create a symbolic link like given below:

ln -sf first_file.txt first_link.txt
How to Create Symbolic Links Using ln Command in Linux - force option
How to Create Symbolic Links Using ln Command in Linux – force option

Creating Symbolic Link of Directory

You can also create a symbolic link of a directory. To create a symbolic link of the directory you should specify the directory name and link name.

In the following example symbolic link of the directory /var/www/html will be created as html inside current working directory.

ln -s /var/www/html html

To confirm the symbolic link is created run following command in Linux:

ls -l html

The output should be:

How to Create Symbolic Links Using ln Command in Linux - directory
How to Create Symbolic Links Using ln Command in Linux – directory

Removing Symbolic Links

You can remove symbolic link using rm command in Linux. If you move the symbolic link elsewhere then it will not work. So it’s better to remove it if unused. following is the basic syntax for removing symlink:

rm LINK_NAME

In the following example symlink first_link.txt will be removed:

rm first_link.txt

To confirm the symbolic link is created run following command in Linux:

ls -l first_link.txt

The output should be:

How to Create Symbolic Links Using ln Command in Linux - remove symbolic link
How to Create Symbolic Links Using ln Command in Linux – remove symbolic link

Conclusion

Here you have successfully learned How to Create Symbolic Links Using ln Command in Linux. If you have any queries regarding this then you can comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here