answersLogoWhite

0


Best Answer
Linux reference-counts each file and will only reclaim its blocks when the reference-count goes to zero, that is, when the last hard link is severed.

Consider this example:
$ date > foo
$ rm foo


Typically, you create a file, and it has a ref-count of 1 for its entire lifetime, and eventually we delete the directory entry, decrement the ref-count, and reclaim the inode and data blocks.

Now consider this example:
$ date > foo1
$ ln foo1 foo2
$ rm foo1
$ cat foo2

In this case, the 1st name disappears, but the original data are still available via the 2nd name. This will continue to be true even if we add and delete many names, and even if we move those names up and down the directory hierarchy within the same filesystem, as long as at least one reference remains. Deleting that last name will reclaim the inode and data blocks, too. If you are familiar with RDBMs, you might think of it in terms of referential integrity.


Soft links, on the other hand, have very little to do with all that. Here are two commands which bear some similarity to each other:

$ echo 'Go look at foo2' > bar
$ ln -s foo2 baz

The first offers human-readable instructions, while the second phrases those instructions in terms the filesystem understands. In both cases, if someone rm's foo2 we won't know about it until we "cat baz" or consult bar and notice that foo2 has been pulled out from under us. Symlinks are simple. There is no ref-counting and no guarantees of integrity when it comes to symlinks. That said, they are still enormously useful. Note that symlinks can cross filesystems, a feat which hard links were not designed to handle.
User Avatar

Wiki User

βˆ™ 17y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 12y ago

Hard Link is a mirror copy of the original file. Hard links share the same inode.

Any changes made to the original or Hard linked file will reflect the other.

Even if you delete any one of the files, nothing will happen to the other.

Hard links can't cross file systems.

Soft Link is a symbolic link to the original file. Soft Links will have a different Inode value.

A soft link points to the original file. If you delete the original file, the soft link fails. If you delete the soft link, nothing will happen.

Hard links can cross file systems.

Example: Create a file " original-file.txt " :~/test$ echo "Learning about Hard and Soft Link" > original-file.txt

:~/test$ cat original-file.txt

Learning about Hard and Soft Link

Now lets create a Hard Link

Example: HardLink-file.txt :~/test$ ln original-file.txt HardLink-file.txt

:~/test$ ls -il

total 8

840388 -rw-r--r-- 2 telson telson 33 2009-05-18 09:16 HardLink-file.txt

840388 -rw-r--r-- 2 telson telson 33 2009-05-18 09:16 original-file.txt

Now lets create a Soft Link

Example: SoftLink-file.txt :~/test$ ln -s original-file.txt SoftLink-file.txt

:~/test$ ls -il

total 8

840388 -rw-r--r-- 2 telson telson 33 2009-05-18 09:16 HardLink-file.txt

840388 -rw-r--r-- 2 telson telson 33 2009-05-18 09:16 original-file.txt

840186 lrwxrwxrwx 1 telson telson 17 2009-05-18 09:23 SoftLink-file.txt -> original-file.txt

From the above ls -il result, you find the same inode for " HardLink-file.txt " and " original-file.txt ".

Inode value is different for the soft link " SoftLink-file.txt ".

Now lets try editing the original file:

Example: :~/test$ cat >> original-file.txt Editing this file to learn more!! :~/test$ cat original-file.txt Learning about Hard and Soft Link Editing this file to learn more!! :~/test$ cat HardLink-file.txt Learning about Hard and Soft Link Editing this file to learn more!! :~/test$ cat SoftLink-file.txt Learning about Hard and Soft Link Editing this file to learn more!!

Now lets try changing the permissions:

Example: :~/test$ chmod 700 original-file.txt :~/test$ ls -il total 8 840388 -rwx------ 2 telson telson 67 2009-05-18 09:34 HardLink-file.txt 840388 -rwx------ 2 telson telson 67 2009-05-18 09:34 original-file.txt 840186 lrwxrwxrwx 1 telson telson 17 2009-05-18 09:23 SoftLink-file.txt -> original-file.txt

From the above example its clear that changing the permission of " original-file.txt " will update the permission set of " HardLink-file.txt ".

The soft link remains unchanged.

Now lets try deleting the original file.

Example: :~/test$ rm original-file.txt :~/test$ ls -il total 4 840388 -rwx------ 1 telson telson 67 2009-05-18 09:34 HardLink-file.txt 840186 lrwxrwxrwx 1 telson telson 17 2009-05-18 09:23 SoftLink-file.txt -> original-file.txt :~/test$ cat HardLink-file.txt Learning about Hard and Soft Link Editing this file to learn more!! :~/test$ cat SoftLink-file.txt cat: SoftLink-file.txt: No such file or directory

So removing the original file will affect the Soft link. The Soft link fails.

Hard link is unaffected.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 13y ago

To create soft link(symbolic link) for particular file "f1" command is 'ln -s f1 sf1'. For hard link command is 'ln f2 hf2'. Soft link having different inode no from it's original file f1,hard link having same inode no as it's original file f2. Soft link can create directories but hard link can't. Hard links created within that particular file system but soft link cross that file system .

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 14y ago

A hard link usually links a file to another file or directory on the same partition. A soft link can point to any file or directory anywhere on the system.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between soft links and hard links in UNIX?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Every unix directory has at least two hard links why?

The two links are for the current directory (.) and the parent directory (..). This is allow for easy directory traversal.


Why Unix does not allow ordinary users to create hard links?

You don't have to be a privileged user to create a hard link; most of the time they aren't as desirable as soft links because with a hard link the file must be present to create the link. In addition, there are some restrictions about going across file systems with a hard link that do not exist as a restriction with a soft link.


What is the difference between hard disk and hard drive?

absolutely nothing


What is difference between hard salami and regular salami?

one is hard


What is the Difference between hard disk and cassette tape?

what is the defference between cassette tape and hard disk


What is the difference between symbolic link and hard link?

A hard link is an actual physical entity representing the link. i.e.--data file on a server.A symbolic link is a pointer to a physical entity. The purpose of a symbolic link is, if the object moves, the symbolic link is coded so no matter where the object physically resides, the database can find it. It requires less administration not having to update locations in files pointing to files that have moved. Symbolic links function similar to the short-cuts in a DOS based operating systemAnswerIn Unix and Unix-like systems, a hard link is a directory entry (filename) that points to the file itself. The operating system keeps track of how many hard links point to a file, so when the last hard link to a file is removed, the file is deleted. A symbolic link is a special file that only contains a filename. The file that a symbolic link refers to may not necessarily exist at any given time.


Difference between hexadecimal and octal number?

to hard


What is the difference between uranium hard and soft ore?

It is a difference between the hardness, generally expressed in the Mohs scale.


What is the difference between a folder and a hard disk?

The hard disk is the device that stores the folders.


What is difference between a hill and a pill?

A hill is hard to get up and a pill is hard to get down


What is the main difference Between hard and soft corals?

The difference is in the name; soft corals are soft have body ; with no bony or hard Parts


What is the difference between light and hard?

You can go to sleep with the light on...