norman's blog

Notes of an amnesiac.
Never stop thinking.
Find an aesthetic description.

Monday, June 1, 2009

Hard Link and Symbolic Link

Symbolic link

# ln –s existing-file-name link-name
Also called a soft link or symlink — resembles a Windows shortcut. A symlink is a little file that contains the pathname of another object on the filesystem: a file, a directory, a socket, and so on — possibly even the pathname of another link. This pathname can be absolute or relative.
We can still edit the original file by opening the symbolic link, and changes we make doing that will "stick." But if we delete the symbolic link, it has no impact on the original file at all. If we move or rename the original file, the symbolic link is "broken," it continues to exist but it points at nothing.
Symlinks can point to files that are on different filesystems and can also point to directories.



Hard link

# ln existing-file-name link-name
It isn’t itself a file. Instead, it’s a directory entry. It points to another file using the file’s inode number. Means both have same inode number. Any changes to the original file will get reflected in the link file also as both are same.
Hard links should be on the same filesystem, and can not be pointed to directories.



To better understand the difference lets see how Linux manage files Each file is composed by two parts:

  • Filename:inode
  • Data
The inode contains some info like permissions and other info and also the address where the Data is.
Ok two different file names may point to the same inode, therefore to the same Data, this is know as hardlink. With the symbolic link, the filename points to a different inode and to a different data, but that data is actually a path to the referenced file, the OS recognize that this is a special file so instead of using the data of this file, it follows to the referenced file and acts on that file.
As you can see symbolic links needs an extra step to reach the pointed file.


NOTE:
1. Link can be created absolutely or relatively, depending on the target file path referenced.
2. Without broken link error, an absolute link can be moved solely, while a relative link can be moved together with target.
3. When creating a relative link, be ware that target-path(existing-file-name) should be regard standing at the path of the link, not the current path. To avoid error, just cd the path where you want to put the link.

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home