The time now is 01/08/09 - 21:36
Log in: Username: Password:
Search forums for:
  
 
H-Desk.com Forum Index

H-Desk.com - PC Security matters - Protect your PC privacy


Jun 11, 2008

How to create or change symbolic link?

by windshell

H-Desk.com Forum Index -> Web related software -> Other web related software

For UNIX or Linux system users, symbolic link is a common feature they use almost daily. Here is the small guide which explains how to create them easily.

Links are special files that point to other files, and symbolic links (aka soft links or symlinks) are a special type of files or file-system objects that serve as a reference or points to another file, UNC or directory, known as target.
This type of links is useful when you have a lot of folders and files are scattered all over the directory tree, and you need to manage them from a single location.
Symlinks also provide a static file path that can be point to a ever changing object without affecting the functionality of the program.

Unlike UNIX and Linux based systems, in Windows Vista, for example, ‘symlink’ feature is added to NTFS file system and can be created by using MKLINK command. In previous Windows operating system, the closest thing to symbolic links is NTFS junction point, or Windows shell shortcut (.lnk) file.
Symlink in Vista, unlike .lnk shortcut in desktop or Windows Explorer, allow user to access files within the symbolic link created itself via Explorer, the console, etc.


Difference between hard and symbolic link


First I’d like to explain a difference between hard links and symbolic or soft links.


A hard link is another name for an existing file, where there is no difference between the link and the original file. So, if you make a hard link from file `foo' to file `bar', and then remove file `bar', file `foo' is also removed. Each file has at least one hard link, which is the original file name itself. Directories always have at least two hard links, which are the directory name itself (appears in its parent directory) and the special file `.' inside the directory. So, when you make a new subdirectory, the parent directory gains a new hard link for the special file `..' inside the new subdirectory.
Removing hard link means removing the original file as well.

A symbolic link (sometimes called a "symlink" or "soft link") passes most operations, such as reading and writing, to the file it points to, just as a hard link does. However, if you remove a symlink, you remove only the symlink itself, and not the original file.



Creation of Symbolic Link in Unix or Linux (with hard link and symbolic link difference example)



To create symlink in UNIX or Linux, you should use ln ("link") to make links between files. Give as arguments the name of the source file to link from and the name of the new file to link to. By default, ln makes hard links.
To create a hard link from `detroit’ to `rock-city’, type:

      $ ln detroit rock-city RET

This command makes a hard link from an existing file, `detroit', to a new file, `rock-city'. You can read and edit file `rock-city' just as you would `detroit'; any changes you make to `rock-city' are also written to `detroit' (and vice versa). If you remove the file `rock-city', file `detroit' is also removed.

To create a symlink instead of a hard link, use the `-s' option.

To create a symbolic link from `detroit' to `rock-city', type:

      $ ln -s detroit rock-city RET


After running this command, you can read and edit `rock-city'; any changes you make to `rock-city' will be written to `detroit' (and vice versa). But if you remove the file `rock-city', the file `detroit' will not be removed.



Creation of Symbolic Link in Windows



As mentioned, to create a symbolic link in Windows, you should use MKLINK command line tool. MKLINK can be used to create a hard link or directory junction as well. Hard link is essentially giving another name or label to the objects, while NTFS directory junction point is used to redirect the whole folder to another folder as if it’s the original folder itself. Junction point is used extensively in Vista to provide support for old directory structures for user profiles (i.e Documents and Settings) to point to the new user folders located inside Users directory.


Examples and Usages of MKLINK


* Note:
Only Administrators can use MKLINK to create symbolic links or else you will get “You do not have sufficient privilege to perform this operation.” error.



To create symbolic link called ‘foo’ to reference to c:WindowsSystem32notepad.exe:


C:test>mklink ‘foo’ c:WindowsSystem32notepad.exe

You will see the following result:

symbolic link created for foo <<===>> c:WindowsSystem32notepad.exe

If you type dir in command prompt, you will see the following listing:

Volume in drive C has no label.
Volume Serial Number is [Number of Volume]

Directory of C:test

06/11/2008 06:24 AM <DIR> .
06/11/2008 06:24 AM <DIR> ..
06/11/2008 06:24 AM <SYMLINK> foo [c:Windowssystem32notepad.exe]
1 File(s) 0 bytes
2 Dir(s) 79,238,722,560 bytes free

To create symbolic link to a folder, simply use /D switch:

C:test>mklink /d bar c:windows

symbolic link created for bar <<===>> c:windows


Results of dir command:

Volume in drive C has no label.
Volume Serial Number is [Number of Volume]

Directory of C:test

06/11/2008 06:24 AM <DIR> .
06/11/2008 06:24 AM <DIR> ..
06/11/2008 06:24 AM <SYMLINKD> bar [c:windows]
06/11/2008 06:24 AM <SYMLINK> foo [c:Windowssystem32notepad.exe]
1 File(s) 0 bytes
3 Dir(s) 79,238,722,560 bytes free

* Note: MKLINK cannot be used to delete symbolic link. To remove a symbolic link, simply delete them as if you’re removing a normal file. For example, to delete the foo symbolic link created above, enter the following command:

C:test>del foo

To remove the bar symbolic link to a folder created above, use the following command:

C:test>rd bar



* Explanation of MKLINK Syntax and Options



/D - Creates a directory symbolic link. Default to 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.
SYNDICATE FEED



ARTICLE RATING


[ 1 voters ]

ARTICLE RATING

bad
average
good
very good
excellent

COMMENTS
Readers posted 0 comments for this article




Links