-   Linux - Newbie (
-   -   softlinks vs hardlinks (

irfanhab 02-05-2007 01:07 PM

softlinks vs hardlinks

I read form a source that if hardlinks to a file exist, the file can not be deleted until the hardlinks are removed? I tried this in my openSUSE 10.2 installation, and to my surprise I could remove the file with hardlinks, but the hardlinks were not behaving as links at all, rather when I created a hardlink like this:


ln filename linkname
Then removed filename, the file named linkname was an exact copy of the original file, and rm gave no errors removing the original file. Rather symbolic links behaved as links, and if the original file was removed the link became invalid, rather the hardlink retained the content? So what is the difference b/w simple copy and a hardlink then?

acid_kewpie 02-05-2007 01:11 PM

a hard link is *NOT* a copy of a file, you clearly haven't done your reading. it is *THE* same file, but with a different name in the file table. a normal file is a file with one hard link. if you use ln to add a hard link, then it is a file with two hard links. both names are equal and one does not depend on the other.

irfanhab 02-05-2007 01:18 PM

But if I were to delete the original file, theoretically the other file must be deleted aswell? But that was the not the behaviour I was getting

acid_kewpie 02-05-2007 01:30 PM

you do not delete "files", you delete references (hardlinks) to spaces of disk. all the time something in the fat table references a piece of disk space, then the contents of that space is reachable as a file.

timmeke 02-06-2007 03:05 AM

The number of links to the contents of your file, stored on disk, is stored.
Each time a new hard link is created, this link count is increased by one.
Each time you delete a hard link, the link count is decreased by one. It's only when the link count reaches
0, that the file contents are actually marked for deletion/overwriting on the disk itself.

I suggest you also read up on inodes, if this interests you.

All times are GMT -5. The time now is 08:39 PM.