How to create a symlink in Linux?

A symlink (symbolic link) is a type of file that points to other files or directories (folders) in Linux.

To create a new symlink (will fail if symlink exists already):

ln -s /path/to/file /path/to/symlink

To create or update a symlink:

ln -sf /path/to/file /path/to/symlink

How to create a symlink in Linux?

Open Bash prompt and type the below mentioned command to make a symbolic link to your file:

A) Goto the folder where you want to create a soft link and typeout the command as mentioned below:

$ ln -s (path-to-file) (symbolic-link-to-file)
$ ln -s /home/user/file new-file

B) Goto your new-file name path and type:

$ ls -lrt (To see if the new-file is linked to the file or not)

Example:

user@user-DT:[~/Desktop/soft]# ln -s /home/user/Desktop/soft/File_B /home/user/Desktop/soft/File_C
user@user-DT:[~/Desktop/soft]# ls -lrt
total 0
-rw-rw-r-- 1 user user  0 Dec 27 16:51 File_B
-rw-rw-r-- 1 user user  0 Dec 27 16:51 File_A
lrwxrwxrwx 1 user user 31 Dec 27 16:53 File_C -> /home/user/Desktop/soft/File_B


Note: Where, File_C -> /home/user/Desktop/soft/File_B  Means, File_C is symbolically linked to File_B

Creating Symbolic links or Soft-links on Linux:

All following code assumes that you want to create a symbolic link named /tmp/link that links to /tmp/realfile.

CAUTION: Although this code checks for errors, it does NOT check if /tmp/realfile actually exists ! This is because a dead link is still valid and depending on your code you might (rarely) want to create the link before the real file.


Shell (bash, zsh, …)

#!/bin/sh
ln -s /tmp/realfile /tmp/link

Real simple, just like you would do it on the command line (which is the shell). All error handling is done by the shell interpreter. This code assumes that you have a working shell interpreter at /bin/sh .

If needed you could still implement your own error handling by using the $? variable which will only be set to 0 if the link was successfully created.

C and C++

#include <unistd.h>
#include <stdio.h>

int main () {
  if( symlink("/tmp/realfile", "/tmp/link") != 0 )
    perror("Can't create the symlink");
}

symlink only returns 0 when the link can be created. In other cases I’m using perror to tell more about the problem.

Perl

#!/usr/bin/perl
if( symlink("/tmp/realfile", "/tmp/link") != 1) {
  print STDERR "Can't create the symlink: $!\n"
}

This code assumes you have a perl 5 interpreter at /usr/bin/perlsymlink only returns 1 if the link can be created. In other cases I’m printing the failure reason to the standard error output.

How to create, update and delete a symlink in Linux?

Difference between Soft Link and Hard Link in UNIX

In this section, we will see some differences between soft links and hard links in UNIX. These differences are by no means complete so please contribute if you know any other difference between UNIX soft link and hard link. You can also let us know about how you are using symlinks or UNIX soft links.

Soft link vs Hard links in UNIX

1) The first difference between a soft link and a hard link is that  Unix Soft links are pointers to programs, files, or directories located elsewhere (just like Windows shortcuts) while Unix Hard links are pointers to programs and files, but NOT directories.



2) The second major difference between UNIX soft link and the hard link is that If the original program, file, or directory is renamed, moved, or deleted, the soft link is broken and it will show in red color if you using ls -lrt –color option. On the other hand, If the original program or file is renamed, moved, or deleted, the hard link is NOT broken



3) One not so important difference on soft link vs hard link is that,  If you type ls -F you can see which files are UNIX soft links because they end with @

4)  Another difference between soft link vs hard link is how you create them, To create a soft link called “current” that points to a file or directory called “new_package”, use this: ln -s new_package latest  to remember this command always remember that name of the soft link comes as the last argument. On the other side to create a UNIX hard link called myhardlink.txt that points to a file called myfile.txt, use this: ln myfile.txt myhardlink.txt

5) One more significant difference between soft link and hard link on UNIX or Linux is that soft link can point to a network mounted directory also. For creating UNIX soft link remember to use the option “-s” with UNIX link command “ln”. While Hard links in UNIX cannot span disk drives, so you CANNOT have a hard link on /dev/hdb that refers to a program or file on /dev/hda

Creating a symbolic link or symlink in UNIX

Here we will see how to create a soft link and hard link in UNIX, also known as symbolic link or symlink in Linux. For our symlink example, we will use a folder called symlinks which will have some directories representing a different version of a particular application and we will learn how to create a symlink, remove symlink and update symlink or soft link in Unix and Linux.

Here is the initial snapshot of our example symlink directory, currently there is no symlink in the directory.

J@unix_machine ~/symlinks
$ ls -lrt
total 0
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.3

Now we will create a soft link in UNIX in symlink directory.

J@unix_machine ~/symlinks
$ ln -s 1.3 latest


This will create a soft link name “latest” which will point to directory “1.3”. Let’s see whether this soft link in UNIX created or not.  We can see that in last line a symlink is created. Notice lrwxrwxrwx  (first “l” denotes it is a link in UNIX)

J@unix_machine ~/symlinks
$ ls -lrt
total 1
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.3
lrwxrwxrwx  1 Javin None 3 Apr 22 12:17 latest -&gt; 1.3

Updating a symbolic link or symlink in UNIX

We have seen how to create a symlink in UNIX now we will see how we can update that symlink or soft link  without removing it.

J@unix_machine ~/symlinks
$ ln -nsf 1.2 latest

This will update the symlink latest to point to directory “1.2” instead of “1.3”. notice command line option “-nsf”. Now let’s see whether this symlink is updated or not.

Javin@unix_machine ~/symlinks
$ ls -lrt
total 1
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:18 1.3
lrwxrwxrwx  1 Javin None 3 Apr 22 12:19 latest -&gt; 1.2

Now let’s create another link previous in this directory pointing to 1.2 and latest pointing to 1.3

J@unix_machine ~/symlinks
$ ln -nsf 1.3 latest

J@unix_machine ~/symlinks
$ ln -s 1.2 previous

J@unix_machine ~/symlinks
$ ls -lrt
total 2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
lrwxrwxrwx  1 Javin None 3 Apr 22 12:20 latest -&gt; 1.3
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:21 1.3
lrwxrwxrwx  1 Javin None 3 Apr 22 12:24 previous -&gt; 1.2

If you have noticed here that when you run “ls –lrt” , total will display a number of soft link in UNIX present in that directory , you can see on previous examples its changes from 0 to 1 and now its 2 because of two symlink present here.

Removing a symbolic link or symlink in UNIX

Removing a symbolic link is similar to removing any file; you need to use the “rm” UNIX command to remove any symlink. This will only remove the symlink and not delete the source directory.

J@unix_machine ~/symlinks
$ rm latest previous

J@unix_machine ~/symlinks
$ ls -lrt
total 0
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:14 1.2
drwxr-xr-x+ 2 Javin None 0 Apr 22 12:18 1.3

Answer #4: Symbolic links in Linux

An arrow may be a helpful mnemonic, especially since that’s almost exactly how it looks in Emacs’ dired.

And big picture so you don’t get it confused with the Windows’ version

Linux:

ln -s target <- linkName

Windows:

mklink linkName -> target

You could also look at these as

ln -s "to-here" <- "from-here"
mklink "from-here" -> "to-here"

The from-here should not exist yet, it is to be created, while the to-here should already exist (IIRC).

(I always get mixed up on whether various commands and arguments should involve a pre-existing location, or one to be made.)

EDIT: It’s still sinking in slowly for me; I have another way I’ve written in my notes.

ln -s (target exists) (link is made)
mklink (link is made) (target exists)

How to remove a symlink to a directory in Linux?

# this works:
rm foo
# versus this, which doesn't:
rm foo/

Basically, you need to tell it to delete a file, not delete a directory. I believe the difference between rm and rmdir exists because of differences in the way the C library treats each.

At any rate, the first should work, while the second should complain about foo being a directory.

If it doesn’t work as above, then check your permissions. You need write permission to the containing directory to remove files.

To create a symbolic link to a given file, open your terminal and type:

ln -s source_file symbolic_link

Replace source_file with the name of the existing file for which you want to create the symbolic link and symbolic_link with the name of the symbolic link.

The symbolic_link parameter is optional. If you do not specify the symbolic link, the ln command will create a new link in your current directory:

In the following example, we are creating a symbolic link named my_link.txt to a file named my_file.txt:

ln -s my_file.txt my_link.txt

To verify that the symlink was successfully created, use the ls command:

ls -l my_link.txt

The output will look something like this:

lrwxrwxrwx 1 linuxize users  4 Nov  2 23:03  my_link.txt -> my_file.txt

The l character is a file type flag that represents a symbolic link. The -> symbol shows the file the symlink points to.

How to create Symlink To a directory?

The command for creating a symbolic link to a directory is the same as when creating a symbolic link to a file. Specify the directory name as the first parameter and the symlink as the second parameter.

For example, if you want to create a symbolic link from the /mnt/my_drive/movies directory to the ~/my_movies directory you would run:

ln -s /mnt/my_drive/movies ~/my_movies

Shortest answer- Summary:

ln -s TARGET LINK_NAME

Where the -s makes it symbolic.

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →