Symbolic Links in Linux [With ln command]

Written by: Linuxopsys   |   Last updated: November 6, 2023

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. They are similar to shortcuts and are used for linking libraries, files, or folders without copying the actual files.

Unlike hard links, symlinks do not contain the data in the target file itself but redirect access to the target file or directory.

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]

Create Symbolic Links in Linux

As mentioned before 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.

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
create symbolic link to a file

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
verify symbolic link

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

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
rename symlink

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
create symlink to a directory

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
show symbolic links

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

Removing 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
remove symlink using unlink

To delete symlink using the rm command, type:

rm new_link.txt
remove symlink using rm command

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
list symbolic links in the directory

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.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment