ln - command line hardlinks
Last Updated March 9th 2009, Version 2.2.0.6
Introduction The NTFS filesystem of NT4/W2K/WXP supports hardlinks functionality. This tool creates hardlinks on NTFS volumes in a convenient way from the command prompt.

Installation copy ln.exe to some directory referenced by your PATH environment variable. %systemroot% is a good place.

Using ln ln.exe is a typical command line utility, which can be found similiar within the resource kit. Its' highlights are as follows:
 
UNC Name suport A local path can be a UNC name, which maybe reached from outside the computer via share. Sometimes it is neccessary to use UNC names to refer to local pathes.
ln resolves local UNC names to local path names and does its job on those resolved path names. e.g.:

ln \\local_computer\ls\sourcefile.txt x:\dir_on_local_computer\destination.txt

In the above example there is a share called ls on your local_computer which conains a file sourcefile.txt. ln resolves the UNC name to a local path if possible, and creates a link between the resolved location and destination.txt in x:\dir_on_local_computer.

Recursive support Hardlinking one file to another file is something ln.exe from resource kit can also do, but this ln.exe can do more.

ln.exe can recursivley hardlink all files found below a given directory location to a to be created directory structure under a different location.

In other words this means, the given directory structure is rebuilt under a different location, and all files from the given directory structure are hardlinked to the newly generated structure. Something like a xcopy but the files are hardlinked and not copied:

Hardlink Clones how to

e.g.:

ln -r x:\dir\dir2 x:\dir\newdir

In the above example the directories below x:\dir\dir2 are rebuilt below x:\dir\newdir, and all files from x:\dir\dir2 are hardlinked to their corresponding locations under x:\dir\newdir.

The Recursive Support or Hardlink Clone functionality is also available via Link Shell Extension


Copy option Once hardlinks are used on a disk, copying a whole directory structure with files, some of them hardlinked together, to another hardisk or path and preserving the inner hardlink structure of this directory structure becomes difficult, because currently there is no copy program, which can do this.

The --copy basically creates a copy of the directory structure from the source location to the destination, but it preserves the inner hardlink structure of the source, and recreates this inner hardlink structure in the destination location:

Drop Smart Copy

By closely looking at the above picture some can find three different types of files:


Normal Files The file B is a normal file. It gets copied as any other copy tool would do.

Saturated Hardlinks The files E and F are hardlinked together. In LSE naming universe they are called Saturated Hardlinks, because the reference count, which is here 2, matches the number of occurrences below 'Folder 1', which is here 2.

In General: A hardlink is called Saturated with respect to a folder F, if the number of occurrences below the folder F matches the reference count.

Saturated Hardlinks can be copied completely via Smart Copy.

Unsaturated Hardlinks The File A, C, D are hardlinked together. In LSE naming universe they are called Unsaturated Hardlinks, because the reference count, which is here 3, does not match the number of occurrences below 'Folder 1', which is here 2. Only C and D are below Folder one.

In General: A hardlink is called Unsaturated with respect to a folder F, if the number of occurrences below the folder F is smaller than the reference count.

Unsaturated Hardlinks can only be partially copied by Smart Copy. In the above example C and D are hardlinked together in the destination location, but the hardlink to A is broken. This means that the reference count of C and D is 2 under the destination.

With junctions it behaves as follows:

Behaviour for inner junctions

By closely looking at the above picture some can find three different types of folders/junctions:

Normal Folders The folder 'Folder 3' is a normal folder. It gets copied with its content as any other copy tool would do.

Inner Junctions The folder 'Inner Junction' is targeted at 'Inner Junction Target'. In LSE naming universe this kind of folder is called Inner Junction, because its target points to a folder, which is below the common anchor 'Folder 1'.

Inner Junctions are restored properly via Smart Copy in the destination location.

Outer Junctions The folder 'Outer Junction' is targeted at the folder 'Outer Junction Target'. In LSE naming universe this kind of folder is called Outer Junction, because its target points to a folder, which is in paralell and thus outside the anchor 'Folder 1'.

Outer Junctions are broken by Smart Copy and the thus the content does not get copied.

e.g.:

ln -c x:\dir\dir2 y:\dir\newdir

In the above example the directories and files below x:\dir\dir2 are copied to y:\dir\newdir, and all hardlinks within x:\dir\dir2 are copied to their new locations under y:\dir\newdir.

If you use ln.exe as a backup tool with the --copy option, please make sure you test that everything is copied. I tested ln --copy many times, and had no problems, but when it comes to backing up data, some has to be careful.

The Copy or Smart Copy functionality is also available via Link Shell Extension


Normal linking Just for completeness: ln.exe can do the normal ln stuff too.

ln sourcefile.txt destination.txt

In the above example sourcefile.txt gets linked to destination.txt

Enum hardlinks Sometimes it is convenient to find out which files are linked together, because hardlinked files might be spread around a NTFS volume. This is somehow the inverse hardlink functionality and it can be invoked as follows

ln -e path

In the above example files below path are searched, and the files which are hardlinked are printed out.

The -e option first prints so called saturated and afterwards unsaturated hardlinks. For saturated hardlinks the number of files found below path matches the hardlink reference count of these files, but unsaturated hardlinks have a reference count greater than the number found below path.

List Hardlink sibblings With Vista finding out the sibblings of a hardlink is very simple and needs no timeconsuming operations

ln -l filename

In the above example the hardlink sibblings for filename are printed out.

With XP, W2K or W2K3 finding the hardlink siblings now also works, but it takes more time, because in the worst case the whole logical volume must be searched.


Symbolic Links When used under Vista ln.exe can create symbolic links

ln -s sourcefile.txt destination.txt

In the above example sourcefile.txt gets symbolycally linked to destination.txt.

Please make sure that symbolic links can just be successfuly created from an administrative command prompt. ln.exe will fail on symbolic links from normal command prompts!


Junctions ln.exe can also create junctions, and print the target of junctions

ln -j JunctionName TargetDirectory

In the above example a junctionJunctionName is created, which point to TargetDirectory

ln -j JunctionName

In the above example the junctionJunctionName is searched for its Target and the target is printed out.

rd JunctionName

In the above example the junctionJunctionName removed. This is accomplished with an simple built in command prompt command.


Long path support ln.exe can handle path longer than 256 characters. This is usefully especially in the --copy and --recursive mode


Return Values As a typical command line utility ln.exe exits with return values, so that it can be conveniently used from .bat files:
 
0 Success. Everything is fine.

-1 The source directory given via command line parameters, does not exist.
This happens in recursive mode if the first command line argument is not there

-2 The first command line argument in recursive mode specifies a directory, and it is not a directory, but some other kind of file.

-3 The second command line argument specifies a file, or in recursive mode a directory, which already exists.

-4 The destination directory could not be created in recursive mode.

-5 The source file aka first command line parameter does not exist in normal mode.

-6 The filesystem is not NTFS and does not support the creation of hardlinks.

-7 For some unknown reason the creation of a hardlink failed.

-8 Less arguments were given via command line.

-9 Ln was not able to enumerate the files in a directory. This might happen in recursive or in hardlink eunumeration mode if the file permissions for a directory are insufficient.

Backgrounders Hardlinks are a feature which are common to every Unix system, but are not directly available with NT4/W2K/WXP. It is a feature, which must be basically supported by the file system of the operating system.

So what are hardlinks in detail. It is common knowledge, that a file has a name and data associated to it. By opening the explorer, the NTFS filesystem of NT4/W2K/WXP can be browsed comfortably. There is a 1:1 relationship between the filename and the filedata, but this assumption does not hold for every filesystem.

Some filesystems, such as UFS, XFS, or NTFS have a N:1 relationship between filename and the filedata. So this means there is more than one directory entry for a file.

But how does one create those multiple entries? There is a command under Unix called ln, which creates many file entries for a file, so that there are many filenames or also called hardlinks for one filedata.

For each hardlink created, the filesystem increments a reference count stored with the filedata, so that the filedata stores how many filenames point to it. If a filename pointing to a hardlinked filedata gets deleted, the reference count stored with the filedata gets decremented by one. The filedata gets deleted when the reference count of the filedata decreases to zero.


Limitations Ln.exe can only be used with NT4/W2K/WXP
HardLinks can only be created on NTFS volumes, under NT4/W2K/WXP.
HardLinks can only be created within one NTFS volumes, and can not span accross NTFS volumes.
volumes.

History
March 9th 2009 Version 2.2.0.6 Enumerate hardlink siblings for Vista & Wxp, Symbolic link support vista
Under W2K it turned out, that CreateHardlink() from kernel32.dll with long pathnames (e.g. \\?\) was broken.

September 12th 2008 Version 1.997 Introduced the --copy option
Enabled the --symbolic option for Vista
Introduced the --list option under Vista
Added long (32k) path support

February 17th 2006 Version 1.810 Various small fixes related to commandline parsing.

February 18th 2005 Version 1.803 Fixed NT4 compatibility issues.

February 7th 2005 Version 1.800 Fixed a few minor internal flaw related to hardlink enumeration. Improved html documenation and added description for return values.

November 26th 2004 Version 1.711 decides between saturated and unsaturated hardlinks when used with the -e option.

November 18th 2004 Version 1.600 supports the enumerate functionality via -e switch

June 24th 2004 Version 1.502 released

Status The 2.2.0 version is stable enough to satisfy most needs.

Acknowledgements I wish to thank those who have contributed significantly to the development of ln. Those include:.

Felix Kasza for the hardlink basics.


Open Issues Maybe bugs?
Feature requests?
Links Jos Branders has a tool called Link Magic 1.0, which deals in depth with NTFS junctions.

License This program is provided as is. See license.txt from this distribution for legal issues.

Contact / Donations Bug reports, or feature requests send to Hermann Schinagl..

ln.exe is and will be freeware, but if ln.exe was really helpful for you and saved lots of your time please think of donations either via PayPal



or by sending me a gift certificate from

amazon.de .
Download
Windows 2000
Windows XP
Windows Server 2003
Windows Vista
Please make sure that the necessary runtime .dlls are installed on your system. This prerequisites package can be downloaded from Microsoft:

vcredist_x86.exe for Vs2005 Sp1 (2.6 MB)

Afterwards install the
ln.zip (26KB)

 
Vista64
Windows XP64
Please make sure that the necessary runtime .dlls are installed on your system. This prerequisites package can be downloaded from Microsoft:

vcredist_x64.exe for Vs2005 Sp1 (3.1 MB)

Afterwards install the
ln64.zip (31KB)



Windows Itanium
Please make sure that the necessary runtime .dlls are installed on your system. This prerequisites package can be downloaded from Microsoft:

vcredist_IA64.exe for Vs2005 Sp1 (6.1 mb)

Afterwards install the
lnItanium.zip (50KB)