A symbolic link is usually referred to as soft link. To be more precise a symbolic link is file that link to another file or directory using it’s path.
Symbolic link is extremely useful in a multi-user environment where you need to give people access but not control.
For example a library of music files on a family computer, a link is created in your personal “My Music” directory of the common set of music, no duplication.
In Linux command line symbolic links are created using the ln command, and in the Windows command line the symbolic links are created using the mklink command.
The work difference between a Linux symlink and a Windows shortcut is that a shortcut takes you to the destination location whereas the symlink brings the destination to where the link is.
A symbolic link is a pointer that works at the file-system level as it opposed to a shortcut in windows. The shortcut is a pointer designed to work within explorer.exe.
It is possible to create symbolic link to a file or folder in Windows and linux. The link will look to be the simular as the file/folder to Windows evenif it is just a link pointing at the specific file or folder.
To be precise, a symbolic link is a file system object which points to another file system object. It can be either a file or a folder.
Symbolic links are translucent to the users, since the links appear as normal files or folder. It can be accessed by any applications as well as users in exactly the same ways.
Symbolic links are of two typles namely Hard links and soft links. Soft symbolic links work similar to shortcut in Windows. In Windows when we click on a soft link to a folder, it will be redirected to the corresponding folder where the files are stored.
But, in case of a hard link it will bea act as the file/folder is actually exists at the location of the symbolic link. The applications using the links will believe that the file/folder is existing in the path.
This speciality of the hard symbolic links make it more useful in many situations.
Create Symbolic Links in Windows :
We can create symbolic links in Windows by using the mklink command from the Command Prompt. Need to open Command Prompt as Administrator.
The following command creates a symbolic(soft) link at Link pointing to the file Test :
mklink Link Test
Need to use /D when you want to create a soft link pointing to a directory:
mklink /D Link Targetdirectory
Need to use /H when you want to create a hard link pointing to a file:
mklink /H Link Targetfile
Need to use /J to create a hard link pointing to a directory, also known as a directory junction:
mklink /J Link Targetdirectory
For example, we need to create a hard link at C:\LinkToFolder that pointed to C:\Users\Name\OriginalFolder, you’d run the following command:
mklink /J C:\LinkToFolder C:\Users\Name\OriginalFolder
We need to put quotation marks around paths with if the folder/file name have spaces in it. For example, if the folder is named as C:\Link To Folder and C:\Users\Name\Original Folder, we need to use the following command:
mklink /J “C:\Link To Folder” “C:\Users\Name\Original”
Soft link and hard link are the two types of links in available in Linux.
Hard links have actual file contents and have same inode number and it acts like a mirror copy of the original file. The “ls -l” command list all the contents in the current location as well as the link column shows number of links. If we removing any link then, it just reduces the link count and it will not affect other links.
If the original file is removed then also the link will still show you the content of the file. The changes made to the original or hard linked file will reflect on the others too. Hard links can’t used across file systems.
That is, it cannot be able to create across partitions. Also, it is not possible to create hard link to a directory eventhough we have the root previlage.
Let us try to see some experimental differences. Make a new directory called Test and then move into it and create a new file. Simply follow below steps.
[root@server~]# mkdir Test
[root@server~]# cd Test
[root@server~]# touch test1
Now, create a hard link to test1. Name the hard link as test2.
[root@server~]# ln test1 test2
Display inodes for both files using ‘I’ argument of the ls command.
[root@server~]# ls -il test1 test2
3482256 -rw-r–r– 2 root root 21 May 5 15:55 test1
3482256 -rw-r–r– 2 root root 21 May 5 15:55 test2
From the output we can notice that both test1 and test2 have the same inode number (3482256). Also, both files have the same file permissions and the same size.
Now Remove the original test1.
[root@server~]# rm test1
After removing hard link just have a look at the content of the “link” test2.
[root@server~]# cat test2
We will still be able to see the content of the file.
What are soft links(symbolic link)?
It is simular shortcut in Windows. Soft Link is the actual link to the original file and have different inode numbers.
Soft Link contains the only cantains the path for original file. Removing soft link doesn’t make any troubles. But by removing original file, the link becomes failed and all the symlinks become failed because of the face that the link are pointing to nonexistent file.
A soft link can link to a directory and can be uses across the file systems.
Create soft link for the file test2:
[root@server~]# ln -s test2 test3
Display inodes for both using i argument of ls command.
[root@server~]# ls -il test2 test3
This is what you’ll get:
3482256 -rw-r–r– 1 root root 21 May 5 15:55 test2
3482226 lrwxrwxrwx 1 root root 5 May 5 16:22 test3 -> test2
From the output it is evident that the inode numbers are different and the symbolic link got a “l” before the file permissions. Thus permissions are different for the link and the original file because it is just a symbolic link.