How to Use LN Command to Create Symbolic Links in Linux?

Posted in

How to Use LN Command to Create Symbolic Links in Linux?
maazbinasad

Maaz Bin Asad
Last updated on March 29, 2024

    If you are a hardcore Linux user, you might have heard of Symbolic Links in Linux. In fact, there are two types of such link-based files in Linux, namely Hard Links and Soft Links. To proceed with this tutorial, it’s important that you become familiar with both the links and understand what they are used for. To begin with, a hard link simply represents a file in Linux. For a single file in Linux, you can create more than one name or hard links that associate these file names to the same inode. Hard links can’t be generated for folders and files that exist on different partitions. On the other hand, a soft link (symlink or symbolic link) is just like a shortcut to a directory, folder, or file. It acts as a reference or a pointer to a file. However, unlike a hard link, we can create soft links for files in different partitions. In this tutorial, we will discuss a command called ln which is used to create or generate hard links between files. We will use it along with some options to create soft links for directories and files. To move ahead with this tutorial, you will need to have access to a Linux machine, a command-line interface (terminal), and a user account with sudo privileges. Also, please note that working with sudo privileges or as a root user is always risky and causes a lot of mess if we try to play with the configuration files. Hence, it’s important to be extra cautious while working with files when we are logged in as a sudo user. Now, let us start discussing what the ln command does and what syntax does it follow.

    LN for Symbolic Links

    The ln command in Linux to create soft or symbolic links follows the syntax below: ln -s [options] [source-file] [destination-link] Note: The ln command creates a hard link by default if we don’t use the -s ( symbolic ) option along with it. In the above syntax, if we mention both the arguments, the command will create a link or reference to the source file in the destination directory with the name specified in the second argument. If we only specify the first argument, the command will create a soft link to the source file in the current directory with the same name as that of the source file in the first argument. Following are the options that you can use with the ln command:

    • -s option to create a soft link instead of a hard link.
    • -f option to forcefully overwrite a file or a symbolic link that already exists in the same directory.

    Generate a Symbolic Link

    Let’s use the ln command to create a soft link to a path or a directory/file.

    $ ln -s ~/Documents/file1.txt ~/Documents/file2.txt

     $ ln -s ~/Documents/file1.txt ~/Documents/file2.txt

    You can see that the ln command created a new symbolic link to the file1.txt. To verify the same, let’s use the ls command on the new link.

    $ ls -l ~/Documents/file2.txt

     $ ls -l ~/Documents/file2.txt You can see that the file type is 1, which refers to a symbolic link. Also, the arrow symbol denotes the location of the file that the new symbolic link actually refers to.

    Soft Links for Directories

    We can also leverage the ln command along with the -s (symbolic) option to generate symbolic links for directories. We just need to specify paths for directories instead of files in the same command.

    $ ln -s ~/Documents/Folder1 ~/Documents/Folder2

     $ ln -s ~/Documents/Folder1 ~/Documents/Folder2

    Here, we have specified the source directory path in the first argument and the destination path of the soft link that we want to generate in the second argument. Let’s see what happens if we don’t specify any argument in the second parameter.

    $ ln -s ~/Documents/File1.txt

     $ ln -s ~/Documents/File1.txt

    You can see that the command has created a symbolic link with the same name in the current directory.

    Overwriting Symbolic Links Forcefully

    Suppose, we want to generate a symbolic or soft link that already exists in the destination path. Well, in this case, the command-line or terminal will throw an error.

    $ ln -s ~/Documents/File1.txt ~/Documents/File2.txt

     $ ln -s ~/Documents/File1.txt ~/Documents/File2.txt

    In fact, we will get the same error if we are in the same directory as that of the source file and do not include a path or name in the destination parameter.

    jarvis@iamrj846:~/Documents $ ln -s ~/Documents/File1.txt

     jarvis@iamrj846:~/Documents $ ln -s ~/Documents/File1.txt

    To overwrite the already existing symbolic links, we can use the force -f option along with the -s option.

    $ ln -sf ~/Documents/file1.txt ~/Documents/file2.txt

    Please note that here the file2.txt is a symbolic link that already exists and we are trying to overwrite it.  $ ln -sf ~/Documents/file1.txt ~/Documents/file2.txt

    You can see that if we use the force option, it does not prompt an error.

    Deleting a SymLink

    We can leverage the remove (rm) command in Linux to delete soft links. This is so because symbolic links are also a type of file and the rm command removes or deletes files in Linux. Hence, we can use the rm command to remove soft links as well.

    $ rm ~/Documents/File2.txt

     $ rm ~/Documents/File2.txt

    Instead of the rm command, we can also use another command called unlink in Linux. Note that if we use this command to remove symbolic links, the file or location that the symbolic link we are trying to remove points to does not get removed.

    $ unlink ~/Documents/File2.txt

     $ unlink ~/Documents/File2.txt

    Wrapping it Up

    In this tutorial, we started with a general introduction of links and discussed the different types of links in Linux. We then moved on to discuss how we can leverage the ln command in Linux to create hard links and use the -s option with the ln command to generate soft links. Furthermore, we discussed how we could create soft links for files and directories, use the -f (force) option to overwrite existing symlinks, delete or remove a symlink. Also, we saw what happens if we don’t mention the destination argument in the ln command. We certainly hope that through this detailed guide on the ln command in Linux, you will learn to seamlessly create soft links for directories, folders, or any other type of files in Linux. People are also reading:

    Leave a Comment on this Post

    0 Comments