How to use the Ln command to create a symbolic link in Linux

Symbolic links are often called Soft link Either Symbolic link, This is a special type of file that refers to another file or directory.

In this guide, you will learn how to use the ln command to create symbolic links in Linux to establish links between files.

Link type

In Linux, we have two types of links: soft links and hard links.

Hard link: A hard link is a copy of the original file. It can access the data in the target file. Users cannot create hard links to directories just for files. In addition, hard-linked files have a common inode number. After deleting or deleting the original file, the hard link will continue to work and will contain the contents of the deleted file. It is also important to note that hard links do not span different file systems.

Soft link: Unlike hard links, soft links are only pointers to the file name and do not contain the contents of other files or referenced target files. If the target file is deleted or deleted, the soft link will no longer exist. The advantage of soft links is that they can be used to link to files or directories and can span different file systems.

How to use the ln command

The ln command is used to establish links between files. By default, this command creates a hard link. To create a soft link, just attach -s Options ( -symbol).

syntax:

ln [OPTION] TARGET LINK_NAME

To avoid confusion, when creating a symbolic link, please use the absolute path (relative path) of the source file and the target file.

How to create a symbolic link to a file

To create a symbolic link to the file, run the following command:

$ ln -s { OPTIONS } file symlink

When a file and a symbolic link are defined at the same time, the ln command starts from file This is the first parameter of the file defined in the second parameter Symbolic link.

For example, to create a symbolic link to a file, use the following syntax,

$ ln -s  original_file symlink

Note: ln does not return any output when successful.

In the command, replace Original file With the existing file you want to create a symbolic link for and Symbolic link Link with symbol.

Let us give a real example:

$ ln -s  file1.txt   sample_link.txt

The above command creates a symbolic link named “sample_link.txt” to the existing file “file1.txt” in the current directory.

To verify the creation of the link, simply use the ls command as shown below:

$ ls -l sample_link.txt

Your output should be similar to the following:

lrwxrwxrwx 1 winnie  winnie 9 Jul 12 23:43 sample_link.txt -> file1.txt

In permissions, l The sign indicates that this is a symbolic link, and the character - > Indicates that the symbolic link points to the file file1.txt.

Sometimes the symbolic link does not work due to path problems. It is recommended to use the full path:

# absolute (full path)
ln -s /path/to/originals/originalfile1.txt backup/copy.txt

# relative
cd backup
ln -s ../originals/originalfile1.txt copy.txt

How to create a symbolic link to a directory

Creating a symbolic link to a directory is as easy as creating a link to a file. The syntax is basically the same. The first parameter takes the directory name, and the symbolic link is specified as the second parameter.

The following syntax is an example of how you can do this:

$ ln -s  /path/to/directory  ~/directory

For example, create a symbolic link from a directory /download music/ to ~/my_music Run the command:

$ ln -s  /Downloads/music  ~/my_music

How to overwrite symbolic links

Just call to overwrite symbolic links ln command If there are no other parameters, the following error will always be displayed:

For example, if you try to run the following command again:

$ ln -s  file1.txt   sample_link.txt

You will get the error displayed:

ln: failed to create symbolic link 'sample_link.txt': File exists

The solution to this problem is to introduce another option -F Either -Force Override the target path of the symbolic link as follows:

$ ln -sf  file1.txt   sample_link.txt

This time, overwriting the soft link will not cause any errors.

How to delete symbolic links

To get rid of the symbolic link of Linux or symbolic link, you can use the rm command or Unlink command. It’s easy. For Unlink Command, use the following syntax to delete symbolic links:

$ unlink symlink_to_be_removed

Using the rm command to delete soft links is the same as deleting or deleting regular files:

$ rm symlink_to_be_removed

Are symbolic links important?

If you want to achieve any of the following purposes, it is usually recommended to create a symbolic link to the file:

  1. Files can be accessed from multiple locations without creating duplicate copies, which can consume a lot of disk space.
  2. If you want to keep the original version of the file, make sure the link points to the latest version of the file. This is possible because even if you replace the file with another file with the same file name, the symbolic link will remain active.

Linux programs use symbolic links as aliases, so users do not need to know the code version used.

# which python
/usr/bin/python
# ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 7 Feb 21 13:38 /usr/bin/python -> python2

in conclusion

Symbolic links are similar to standard shortcuts, but there are some differences between the two.

A standard shortcut, like you find in Windows, is just a regular file that points to a specific directory, file, or application. The shortcut is usually displayed as an icon that can be created in any location. Click the shortcut to open the original file, directory, or application.

A symbolic link actually functionally represents the original file. It can directly replace objects such as files. Your Linux system reads the Linux symbolic link as if it were the target object. Ordinary shortcuts only refer to files or directories and do not perform other operations.

That’s all about how to create a symbolic link in Linux. For more information, see ln man. I hope you enjoy reading and leave your comments and suggestions.

Sidebar