MS Windows Linux
In this tutorial, we will understand symbolic/hard links by showing examples on windows 10 using mklink command.
d:>mklink Creates a symbolic link.
MKLINK [[/D] | [/H] | [/J]] Link Target
/D Creates a directory symbolic link. Default is a file symbolic link. /H Creates a hard link instead of a symbolic link. /J Creates a Directory Junction. Link Specifies the new symbolic link name. Target Specifies the path (relative or absolute) that the new link refers to.
Symbolic link and hard link, both point to some other file/folder at the different locations.
We can read/edit the linked file just like the original one.
Soft (Symbolic) Links.
It's like a shortcut link to the original file.
Creating Symbolic link: Our example src file: d:\symTest>dir Volume in drive D is Data Volume Serial Number is 68F9-EDFA
Directory of d:\symTest
06/16/2017 12:03 AM <DIR> . 06/16/2017 12:03 AM <DIR> .. 06/16/2017 12:03 AM 9 src.txt 1 File(s) 9 bytes 2 Dir(s) 63,305,269,248 bytes free
The file content: d:\symTest>type src.txt some text
To create a symbolic link you must start the cmd as administrator. After I did the same: d:\symTest>mkLink dest.txt src.txt symbolic link created for dest.txt <<===>> src.txt
d:\symTest>dir Volume in drive D is Data Volume Serial Number is 68F9-EDFA
Directory of d:\symTest
06/16/2017 07:32 PM <DIR> . 06/16/2017 07:32 PM <DIR> .. 06/16/2017 07:32 PM <SYMLINK> dest.txt [src.txt] 06/16/2017 12:03 AM 9 src.txt 2 File(s) 9 bytes 2 Dir(s) 63,305,105,408 bytes free
As seen above the symlink shows no file size, whereas, the original file shows as 9 bytes. That's because symlink is just a shortcut of the original file. We can do anything (read/write) with symlink. Let's read it: d:\symTest>type dest.txt some text
Let's write to it. The changes will reflect to the original file. (opposite is also possible) d:\symTest>echo some new text > dest.txt
d:\symTest>type dest.txt some new text
Reading the original: d:\symTest>type src.txt some new text
Removing original file will make symlink unreadable: If the original file is moved/removed/renamed the symlink will still exist. In our example, let's delete the original file. d:\symTest>del src.txt
d:\symTest>dir Volume in drive D is Data Volume Serial Number is 68F9-EDFA
Directory of d:\symTest
06/16/2017 07:52 PM <DIR> . 06/16/2017 07:52 PM <DIR> .. 06/16/2017 07:32 PM <SYMLINK> dest.txt [src.txt] 1 File(s) 0 bytes 2 Dir(s) 63,304,261,632 bytes free
The symlink still exits, also it still remembers the original file as seen in the square brackets but it cannot be read anymore: d:\symTest>type dest.txt The system cannot find the file specified.
Let's try to write it: d:\symTest>echo some more text>dest.txt
The above action will also recreate the original file. d:\symTest>dir Volume in drive D is Data Volume Serial Number is 68F9-EDFA
Directory of d:\symTest
06/16/2017 07:52 PM <DIR> . 06/16/2017 07:52 PM <DIR> .. 06/16/2017 07:32 PM <SYMLINK> dest.txt [src.txt] 06/16/2017 07:52 PM 16 src.txt 2 File(s) 16 bytes 2 Dir(s) 63,304,261,632 bytes free
d:\symTest>type dest.txt some more text
d:\symTest>type src.txt some more text
Directory symlinks: A directory can also have a symlink. d:\symTest>md srcDir
d:\symTest>cd srcDir d:\symTest\srcDir>echo some text> src.txt
d:\symTest\srcDir>type src.txt some text
d:\symTest\srcDir>cd.. To create a directory symlink, we need to use /D flag: d:\symTest>mkLink /D destDir srcDir symbolic link created for destDir <<===>> srcDir
d:\symTest>dir /s /b | sort d:\symTest\destDir d:\symTest\destDir\src.txt d:\symTest\srcDir d:\symTest\srcDir\src.txt
Modifying any file under symLink/original directory will reflect the changes in other. Even if we create a new file in the original srcDir, it will show up in desDir too (and vice versa): d:\symTest>echo some other text> srcDir/src2.txt
d:\symTest>dir /s /b | sort d:\symTest\destDir d:\symTest\destDir\src.txt d:\symTest\destDir\src2.txt d:\symTest\srcDir d:\symTest\srcDir\src.txt d:\symTest\srcDir\src2.txt
Similarly deleting any file will delete the other corresponding file too.
Hard Links
A hard link is different from symlink in that, hard link can keep track of the original file even if it is moved to
some other location. A hard linked file keeps reference of all attributes of the original file including it's location, size, date created, permissions etc. A hard link, in fact, is just like the original file pointing to the same hard disk location as original one. In that sense, a newly created file is also a hard link. But when we create a hard link of an existing file, then we are effectively creating a second hard link of the same data.
Only a file can have hard link, not a directory. Also it cannot be created across different disk/volume.
Creating Hard link: Before creating the hard link: d:\hardLinkTest>dir Volume in drive D is Data Volume Serial Number is 68F9-EDFA
Directory of d:\hardLinkTest
06/16/2017 11:11 PM <DIR> . 06/16/2017 11:11 PM <DIR> .. 06/16/2017 11:10 PM 11 src.txt 1 File(s) 11 bytes 2 Dir(s) 63,115,132,928 bytes free
d:\hardLinkTest>type src.txt some text
To create hard link, we need to use /H flag: d:\hardLinkTest>mkLink /H dest.txt src.txt Hardlink created for dest.txt <<===>> src.txt
d:\hardLinkTest>dir Volume in drive D is Data Volume Serial Number is 68F9-EDFA
Directory of d:\hardLinkTest
06/16/2017 11:27 PM <DIR> . 06/16/2017 11:27 PM <DIR> .. 06/16/2017 11:10 PM 11 dest.txt 06/16/2017 11:10 PM 11 src.txt 2 File(s) 22 bytes 2 Dir(s) 63,115,067,392 bytes free
As seen above both src and dest have the same file size, also both have same date created. File system actually cannot differentiate between the original file and the hard link (that's the whole point of a hard link). But actually as both original and the link, pointing to the same disk location, they together will not take the twice total space as shown in above output. To find the real space: d:>du -v hardLinkTest
DU v1.6 - Directory disk usage reporter Copyright (C) 2005-2016 Mark Russinovich Sysinternals - www.sysinternals.com
Processing...
Totals: Files: 1 Directories: 1 Size: 15 bytes Size on disk: 4,096 bytes
du command is not available by default, you have to download it from Windows Sysinternals. Modifying hardlink will reflect the changes in the original and vice-versa.
Removing original file will not affect the hard link in anyways: Removing/renaming/moving the original will not effect the hard link. d:\hardLinkTest>del /Q src.txt
d:\hardLinkTest>dir Volume in drive D is Data Volume Serial Number is 68F9-EDFA
Directory of d:\hardLinkTest
06/17/2017 08:56 AM <DIR> . 06/17/2017 08:56 AM <DIR> .. 06/17/2017 01:51 AM 11 dest.txt 1 File(s) 11 bytes 2 Dir(s) 63,113,945,088 bytes free
As seen above, a hard link doesn't keep information of the original file. d:\hardLinkTest>type dest.txt some text
d:\hardLinkTest>echo some new text>dest.txt
d:\hardLinkTest>dir Volume in drive D is Data Volume Serial Number is 68F9-EDFA
Directory of d:\hardLinkTest
06/17/2017 08:56 AM <DIR> . 06/17/2017 08:56 AM <DIR> .. 06/17/2017 09:03 AM 15 dest.txt 1 File(s) 15 bytes 2 Dir(s) 63,113,875,456 bytes free
d:\hardLinkTest>type dest.txt some new text
|
|