Symbolic Links in Linux - How to Use Ln Command to Create Symbolic Links

Channel: Linux
Abstract: we will learn about symbolic links or soft links and learn how to use ln command to create it. What are Linux Symbolic Links (Symlink) A symbolic link

Links are similar to 「shortcuts」 in Microsoft Windows. Links help easy navigate to the deeply buried path, only need to change permission for the original file and save space. In Linux, there are two types of links: Hard Links and Soft Links.

In this tutorial, we will learn about symbolic links or soft links and learn how to use ln command to create it.

What are Linux Symbolic Links (Symlink)

A symbolic link (also known as a soft link or a symlink) is a special type of Linux file that creates a reference to a particular file or directory on your system. In another way, you can say a symlink or symbolic link is like a shortcut to an existing file.

The symbolic link file contains only the address of the file to which it is being linked. Symbolic links can be used to link to files or directories in the same disk partition, other disk partitions, or different storage devices. You can also create a symbolic link to a file that is not yet created.

Using the symlinks, you can achieve the following:

  • Enable access to a file or directory from multiple locations, in the same file system, across file systems, same storage device, or different storage devices.
  • Avoid creating duplicate copies of the files or directories, without using additional disk space.
  • Retain the actual file or directory and ensure that the symlink points to the current version of a file or directory.
  • Keep links to a file or directory even if you replace the file or directory with a different one that has the same name.

You can run certain commands or operate on symbolic links like they are actual files. This creates multiple access points for a file, without creating additional copies and the files are updated automatically.

Differences between Hard Links and Symbolic Links

A hard link is a copy of the original file, and a symbolic link or soft link is a link to the original file. A hard link has access to the data available in the original file, however, a soft link works as a reference or a pointer to the file name. Each symbolic link has its own inode number and file permissions and will be different from the original file.

You cannot use hard links to different file systems, but soft links can be used across file systems. If the original file is deleted, in case of a hard link you will still have access to the data of the original file, but you will lose access to the file in case of a soft link.

Symlink files are accessed indirectly and thus are slightly slower to open than hard links. But you may not see a notable difference.

Ln Command in Linux

The ln command is a built-in Linux command line utility that is used to create symbolic links (soft links) or hard links. By default, this command creates a hard link, however you can use the -s option to create a symbolic link.

The basic syntax of the ln command:

ln [options] [source_file] [Link_Name]
ln [options] [source_file] [Directory]
How to Create Symbolic Links in Linux

Use the ln command with the -s (--symbolic) option to create a symbolic link in Linux. Basically, to create a symlink you require a path to the source file and a name for the symlink.

Create Symbolic Links to an Existing File

The following is the syntax to create a symbolic link:

ln -s /source/file/path symlink 

For example, to create a symbolic link named document.txt, where /home/linuxopsys/data.txt is the existing file, type:

ln -s /home/linuxopsys/data.txt document.txt

The link was successfully created because there was no error message. The symbolic link is created in the current directory unless you specify a path for the link.

You can verify the link using the following ls command:

ls -l document.txt

You can see the symbolic link document.txt pointing to the source file indicated by the arrow symbol.

You may also create a symlink using the following commands, the only difference is that it creates a symlink with the same name of the file.

ln -s /home/linuxopsys/data.txt 
ln -s /home/linuxopsys/data.txt . 

The symlink file name will be data.txt and will be created in the current directory.

Use the -f option to overwrite the existing symlink, and -i to confirm the overwrite operation:

ln -s -f -i $HOME/dev.txt document.txt
Create Symbolic Links to a Directory

Similar to the symlinks to a file, the ln command can also be used to create symlinks that refer to a directory.

For example:

sudo ln -s /home/docuser dirlink

Here in the example, a symbolic link was created to a home directory of another user. You can verify the symlink using the following command:

sudo ls -l dirlink

Change to the symlink directory and list the files, you should see the same source files.

How to Remove Symlinks

You may need to remove symlinks if the original file is deleted, moved, or the link is not useful anymore. However, it is really important to make sure that the file you are removing is actually a symlink, and not an original file.

Linux operating system has two ways to delete a symlink: unlink command or rm command.

The following command deletes the symlink named document.txt using the unlink command:

unlink document.txt

To delete symlink using the rm command, type:

rm new_link.txt

Do not use / after a directory symlink, otherwise, you will get an error:

rm dirlink/

When you use the rm command to remove symbolic links, you can also remove multiple links in one command. However, unlink can be used to remove only one symbolic link at a time.

Identify Symbolic Links

Files or directories and their symbolic links are two different things. When you delete a symbolic link, the original files or directories remain intact. However, it is important to differentiate between these two because you may lose your significant data if you accidentally delete a file or a directory instead of symlinks.

The following command list all files in the current directory including symbolic links

ls -l

The symlinks are displayed as 'link name -> target filename or directory name'. In the example, there are a total of four symlinks, the first three are symlinks to files, and the last one is a symlink to a directory.

Find Symbolic Links

You may use the find command to search all symlinks in a directory tree. For example to search only the symlinks

find -L /path/to/directory/ -xtype l

Note: Here find command will do a recursive search.

To find all symlinks and their target, type:

find -L /path/to/directory/ -xtype l -exec ls -al {} \;
FAQ Do Symbolic Links Take Up Disk Space

Yes, symbolic links take up a few bytes of disk space, which could be 10 to 20 bytes. The disk space taken by the symbolic link does not have any relation with the disk space taken by the target file. When a symlink is created, Linux operating system also creates a directory entry and a file system entry that stores link properties and link target information.

What Happens to Symbolic Link When File is Deleted

When a file is deleted, the symbolic link stops working and it becomes a dangling link. It points to a file that does not exist. In such a case, the symlinks stop working because they are not automatically updated.

Conclusion

In this tutorial, we learned about symbolic links in Linux and how to create it using the ln command. You can always refer to man page of ln command for more information.

Ref From: linuxopsys

Related articles