Sunday, March 11, 2007

Symbolic and hard links on Windows

This post is not directly related to Oracle, but it often bothered me, that you cannot create hard links and symbolic links on the windows platform. For example, you could move log files or data files to a different disk or even network drive and the application would not be able to notice it.

At least this is what I believed so far ;).

There is an excellent article explaining the way, the folder links work and how to create hard and symlinks on windows.

Symbolic links were introduced in Windows 2000, more specifically with NTFS 5.0. They are called "junction points".

In this article there are different tools mentioned, I have chosen the command line tool Junction by Mark Russovich, there is more information here.

Using junction.exe, creating a symlink is really easy:

C:\TEMP\links
Datenträger in Laufwerk C: ist VAIO
Volumeseriennummer: 54A5-8EF0

Verzeichnis von C:\TEMP\links

11.03.2007 12:42 .
11.03.2007 12:42 ..
01.11.2006 13:06 158.520 junction.exe
1 Datei(en) 158.520 Bytes
2 Verzeichnis(se), 7.896.629.248 Bytes frei

C:\TEMP\links

Junction v1.04 - Windows junction creator and reparse point viewer
Copyright (C) 2000-2005 Mark Russinovich
Systems Internals - http://www.sysinternals.com

The first usage is for displaying reparse point information, and the
second usage is for creating or deleting a NTFS junction point:

usage: junction [-s] [-q] or directory
-q Don't print error messages (quiet)

-s Recurse subdirectories

usage: junction [-d] directory [ target
-d Delete the specified junction
example: junction d:\link c:\winnt


C:\TEMP\links link2 c:\temp\link2

Junction v1.04 - Windows junction creator and reparse point viewer
Copyright (C) 2000-2005 Mark Russinovich
Systems Internals - http://www.sysinternals.com

Created: C:\TEMP\links\link2
Targetted at: c:\temp\link2

C:\TEMP\links
Datenträger in Laufwerk C: ist VAIO
Volumeseriennummer: 54A5-8EF0

Verzeichnis von C:\TEMP\links

11.03.2007 12:47 .
11.03.2007 12:47 ..
01.11.2006 13:06 158.520 junction.exe
11.03.2007 12:47 link2
1 Datei(en) 158.520 Bytes
3 Verzeichnis(se), 7.896.629.248 Bytes frei

C:\TEMP\links link2
Datenträger in Laufwerk C: ist VAIO
Volumeseriennummer: 54A5-8EF0

Verzeichnis von C:\TEMP\links\link2

11.03.2007 12:47 .
11.03.2007 12:47 ..
09.03.2007 20:54 49 debug.txt
1 Datei(en) 49 Bytes
2 Verzeichnis(se), 7.896.629.248 Bytes frei

C:\TEMP\links


!!! CAUTION !!!

There is a tricky thing to be aware of. On *nix (Linux, Unix, etc.) systems, when you delete a symlink, only the link is gone, but not not the files / directories the link points to.

Under Windows, these junction points work differently. If you use the Windows Explorer to delete the link, all files and subdirectories of the linked folder are gone, too. But only at the time, you empty the recycle bin. So be aware of that behaviour.

Always make sure, that you use the tool again, to remove the junction point, then it works fine.


C:\TEMP\links -d link2

Junction v1.04 - Windows junction creator and reparse point viewer
Copyright (C) 2000-2005 Mark Russinovich
Systems Internals - http://www.sysinternals.com

Deleted link2.

C:\TEMP\links c:\temp\link2
Datenträger in Laufwerk C: ist VAIO
Volumeseriennummer: 54A5-8EF0

Verzeichnis von c:\temp\link2

11.03.2007 12:47 .
11.03.2007 12:47 ..
09.03.2007 20:54 49 debug.txt
1 Datei(en) 49 Bytes
2 Verzeichnis(se), 7.897.178.112 Bytes frei


If you use the functionality carefully, it is a really good thing.

Regards,
~Dietmar.

2 comments:

Lucius said...

"For example, you could move log files or data files to a different disk or even network drive and the application would not be able to notice it.

At least this is what I believed so far ;). "

Not sure what you mean by this, junction points only work when the target is a local resource, NOT a network folder/file

Unknown said...

Hi Lucius,

you are right, junction points are restricted to local devices. At least you can use a different internal disk or even an external USB drive. Works fine. It is still useful in certain cases.

But yes, you cannot link to directories on remote shares. Thanks for catching this one.

~Dietmar.