How to Create Hard and Soft (symlink) Links on Linux Systems

Many a times in any Linux/Unix based Operating Systems it is wise to understand the underlying concepts then only one can appreciate the beauty of commands and how they are implemented.

Some small details will help us a lot in debugging and troubleshooting many challenging situations if we know well in advance about these commands and related concepts.

Hard-Soft-Links-Linux

In this topic, I will be covering what are links, different types, distinguishing characters and how they can be better used along with concepts required.

Difference-between-HardLink-SoftLink-Linux

By executing “man ln” command you can see that this says “make link between files” and doesn’t say about soft or hard links.

[email protected] ~}$ man ln

Similarly the command “man link” describes as “call link function to create a file”.

Soft-link :

Soft link as the name suggests is a just a new link created to the new file. In this case the new file’s inode number will be pointing to the old file.

SoftLink-Figure

Hard Link :

In this case the old and new file both will be pointing to same inode number.

HardLink-Figure

Symbolic Link :

In some Unix/Linux flavors both the symbolic and soft links are treated as same. But actual difference is that both the inode numbers of new and old file will be pointing to a new inode number. This will be completely depending on the implementation.

SymbolicLink-Figure

Note 1:- In many cases symbolic and soft link terminologies are used interchangeably. But one has to be aware of when to use what.

Usage of “ln” command for creating hard links and soft links

Creating Hard Link

1) “man ln” command will provide the following output.

[email protected] ~}$ man ln
ln  - make links between files

2) When “ln” command is passed without any arguments, “missing file operand” error is thrown.

[email protected] ~}$ln
ln: missing file operand
Try 'ln --help' for more information.

3) Creating the hard link between two files. First check for any existing files if any, else create the file/files freshly and then link them. Following  is  the step-by-step approach,

[email protected] ~}$ ls -la
total 24
drwx------  4 root root 4096 Feb  6 15:23 .
drwxr-xr-x 23 root root 4096 Jan 25 16:39 ..
-rw-r--r--  1 root root 3122 Jan 25 16:41 .bashrc
drwx------  2 root root 4096 Feb  6 15:23 .cache
-rw-r--r--  1 root root    0 Jan 25 16:41 .hushlogin
-rw-r--r--  1 root root  148 Aug 17  2015 .profile
drwxr-xr-x  2 root root 4096 Jan 25 16:41 .ssh

i)  Create the file using “touch” command

[email protected] ~}$ touch 123.txt
[email protected] ~}$ ls -l
total 0
-rw-r--r-- 1 root root 0 Feb  6 15:51 123.txt

ii) Enter contents into the file using “cat” command, and hit “ctrl+c” to save and exit.

[email protected] ~}$ cat > 123.txt
Welcome to this World!
^C

iii) Create the hard link between the files “123.txt” and “321.txt”. In this case the “123.txt” already existed with contents “Welcome to this World”.

[email protected] ~}$ ln 123.txt 321.txt
[email protected] ~}$ ls -l
total 8
-rw-r--r-- 2 root root 23 Feb  6 15:52 123.txt
-rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt

iv) Check the inode(index node) numbers of the files. For both the files the inode number is same and which is 794583. Also check the contents of new file “321.txt” which is also same as “123.txt”.

[email protected] ~}$ ls -li
total 8
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 123.txt
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
$ cat 321.txt
Welcome to this World!

Note 2:- Inode number is a unique index number generated for any file that is being created in Linux/unix Operating systems. These inode numbers are stored in directory/file attributes in /proc directory. But in case of links these inode numbers are shared b/w the files and only path is updated in file table.

v) Create one more file called “456.txt” and link this using ln command to “321.txt”. Now all the three files have the same inode numbers. Contents of “456.txt” will be as same as that of original file.

[email protected] ~}$  ls -li
total 12
794583 -rw-r--r-- 3 root root 23 Feb  6 15:52 123.txt
794583 -rw-r--r-- 3 root root 23 Feb  6 15:52 321.txt
794583 -rw-r--r-- 3 root root 23 Feb  6 15:52 456.txt
$ cat 456.txt
Welcome to this World!
[email protected] ~}$   ls -l
total 12
-rw-r--r-- 3 root root 23 Feb  6 15:52 123.txt
-rw-r--r-- 3 root root 23 Feb  6 15:52 321.txt
-rw-r--r-- 3 root root 23 Feb  6 15:52 456.txt

vi)  When the source file or any of these files are removed, it will not impact other files. The source file can be removed using “rm” command. Contents of other file will not have any impact either.

[email protected] ~}$ rm 123.txt
[email protected] ~}$ ls -l
total 8
-rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
-rw-r--r-- 2 root root 23 Feb  6 15:52 456.txt
[email protected] ~}$ ls -li
total 8
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 456.txt
[email protected] ~}$ cat 456.txt
Welcome to this World!

vii) Hard link creation across directories is not allowed.

[email protected] ~}$ls -l
total 8
-rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
-rw-r--r-- 2 root root 23 Feb  6 15:52 456.txt
[email protected] ~}$ mkdir abc
[email protected] ~}$ ln abc def
ln: abc: hard link not allowed for directory

viii) Any changes to the content of one file will impact and change the content of other file accordingly, following are the steps explained,

[email protected] ~}$ vi 321.txt
Welcome to this World!
You are welcomed to this new world
:wq
[email protected] ~}$ ls -l
total 12
-rw-r--r-- 2 root root   59 Feb  6 16:24 321.txt
-rw-r--r-- 2 root root   59 Feb  6 16:24 456.txt
drwxr-xr-x 2 root root 4096 Feb  6 16:18 abc
[email protected] ~}$ cat 456.txt
Welcome to this World!
You are welcomed to this new world

Creating Soft Link:

1)  Create the file “src.txt” using “touch” command and enter contents as “Hello World” using cat command and then hit “ctrl+c” to save and exit.

[email protected] ~}$ touch src.txt
[email protected] ~}$ cat > src.txt
Hello World
^C
[email protected] ~}$ ls -l
total 4
-rw-r--r-- 1 root root 12 Feb  6 16:32 src.txt

2)  Create the destination file as “dst.txt” and using “ln -s” command line options create the symbolic link (also called as soft link). Check the contents of “dst.txt” file and same contents as that of “src.txt” can be seen.

[email protected] ~}$ ln -s src.txt dst.txt
[email protected] ~}$  ls -l
total 4
lrwxrwxrwx 1 root root  7 Feb  6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root 12 Feb  6 16:32 src.txt
[email protected] ~}$  cat dst.txt
Hello World

3)  In case of symbolic links the inode number of source file and destination file differs. Also, in the permissions letter “l” appears indicating that these are links. “dst.txt–>src.txt” will be the new link established now.

[email protected] ~}$  ls -li
total 4
794584 lrwxrwxrwx 1 root root  7 Feb  6 16:33 dst.txt -> src.txt
794583 -rw-r--r-- 1 root root 12 Feb  6 16:32 src.txt

4) Symbolic creation for directories is allowed. This is explained as below in the steps

[email protected] ~}$ mkdir abc
[email protected] ~}$ ln -s abc def
$ ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb  6 16:34 abc
lrwxrwxrwx 1 root root    3 Feb  6 16:34 def -> abc
lrwxrwxrwx 1 root root    7 Feb  6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root   12 Feb  6 16:32 src.txt

5) Inode number for all the files/directories (source and destination are different)

[email protected] ~}$  ls -li
total 8
794585 drwxr-xr-x 2 root root 4096 Feb  6 16:34 abc
794586 lrwxrwxrwx 1 root root    3 Feb  6 16:34 def -> abc
794584 lrwxrwxrwx 1 root root    7 Feb  6 16:33 dst.txt -> src.txt
794583 -rw-r--r-- 1 root root   12 Feb  6 16:32 src.txt

6) Symbolic links can be created for directories as explained earlier. Once these directories with symbolic link are created, inside these directories files can be created. This will make it more interesting to know how this behaves now.  When the files are created in the source directory, the same is reflected in destination directory as well. The following steps explains this clearly.

[email protected] ~}$ $ cd abc
[email protected] ~}$  touch 123.txt
[email protected] ~}$  vi 123.txt
Hello
:wq!
[email protected] ~}$  touch 456.txt
[email protected] ~}$  cd ..
[email protected] ~}$  ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb  6 16:36 abc
lrwxrwxrwx 1 root root    3 Feb  6 16:34 def -> abc
lrwxrwxrwx 1 root root    7 Feb  6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root   12 Feb  6 16:32 src.txt
[email protected] ~}$ cd def
[email protected] ~}$ ls -l
total 4
-rw-r--r-- 1 root root 6 Feb  6 16:37 123.txt
-rw-r--r-- 1 root root 0 Feb  6 16:36 456.txt
[email protected] ~}$ cat 123.txt
Hello

Note 3:- We can have any number of nested links. But the user/admin who creates these symbolic  links should be aware of the fact that these will lead to confusion. Sometimes may be  forgotten and may create unwanted results. So they have to be careful.

Note 4:-  There are some possibilities of “symbolic” or “soft” link pointing to a non-existing link. This is called as “Dangling link”. This will be pointing to no-where.

Note 5:- there are system calls used in linux/unix to create symbolic OR hard links at programming level (using system level C/C++ programs). These are 1) symlink 2) symlinkat.

These should not be confused with command line utilities that I have described above.

Removing Soft Link / Symbolic Links

Soft or symbolic links are removed using the ‘rm’ and unlink commands.

Syntax:

# rm <soft-link-filename>

# unlink <soft-link-filename>

Removing Soft Link directories

Syntax:

# rm <soft-link-directory>

# unlink <soft-link-directory>

Conclusions:

Creating links whether hard or soft will be very helpful for admins and developers. The above reference material will come handy while understanding what type of link we are creating and how it is helpful. Also this article will help in understanding the differences and utilization of links.

3 Responses

  1. Ahmer Mansoor says:

    Hi,
    Very detailed article.
    There is an advice to add procedure to remove hard/soft links as well, for the sake of completeness of your article.

  2. Vitaly says:

    Hi, Nice article, but lacks description about permissions of files for symlinked folder. Could permissions of symlinked folder files could be changed, how to do that if possible?

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest