Welcome to graduate2professional.blogspot.com

Wednesday, June 3, 2009

Difference between Symbolic link and Hard link?

Linux has two kinds of file system links: symbolic links andhard links.

A symbolic link — 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 thefilesystem: a file, a directory, a socket, and so on —possibly even the pathname of another link. This pathnamecan be absolute or relative. To make a symlink, use ln withthe -s option. Give the name of the target first, then thename of the link.

# ln –s existing-file-name link-name

We can still edit the original file by opening the symboliclink, and changes we make doing that will "stick." But if wedelete the symbolic link, it has no impact on the originalfile at all. If we move or rename the original file, the symbolic link is "broken," it continues to exist but it points at nothing.

What happens if I edit the link?
Any modifications to the linked file will be changed on the original file.
What happens if I delete the link?
If you delete the link the original file is unchanged. It will still exist.
What happens if I delete the original file but not the link?
The link will remain but will point to a file that does not exist. This is called an orphaned or dangling link.



A hard link isn’t itself a file. Instead, it’s a directoryentry. It points to another file using the file’s inodenumber. Means both have same inode number. Any changes tothe original file will get reflected in the link file alsoas both are same.

# ln existing-file-name link-name

To give a file more than one name or to make the same fileappear in multiple directories, you can make links to thatfile instead of copying it. One advantage of this is that alink takes little or even no disk space. Another is that, ifyou edit the target of the link, those changes can be seenimmediately through the link.


The Difference Between Soft and Hard Links
Hard links
Only link to a file not a directory
Can not reference a file on a different disk/volume
Links will reference a file even if it is moved
Links reference inode/physical locations on the disk

Symbolic (soft) links
Can link to directories
Can reference a file/folder on a different hard disk/volume
Links remain if the original file is deleted
Links will NOT reference the file anymore if it is moved
Links reference abstract filenames/directories and NOT physical locations. They are given their own inode

No comments:

Post a Comment