You are not logged in.
Hi everyone!
I'm trying to figure out what is the point of creating hard links to files instead of just doing regular copies. I found out that once you remove the original file that was linked to a hard link, that the hard link would still point to the content of the original file that was removed? That seems kind of confusing, is there any reason why it's that way?
Offline
copy:
- you have two different versions of the file.
- if you edit one, the other one stays the same.
- if you delete one, the other one stays there, but it may not be identical if it was edited
- twice as much disk space used (two different files)
hard link:
- you have one file with two different filenames.
- If you edit one, it gets edited in all filename locations
- if you delete one, it still exists in other places
- only one file on disk
soft link:
- you have one file with one filename and a pointer to that file with the other filename.
- if you edit the link its really editing the original file
- if you delete the file the link is broken
- if you remove the link the file stays in place.
- only one file on disk
Hope it helps.
Dusty
Offline
don't you see the difference between 2 files (2 names, 2 content - even if the same but in different place on HDD) and 1 file with 2 names in 1 place on HDD ?
Zygfryd Homonto
Offline
To be a bit more technical, (and this is my understanding, which may not be perfect).
A hard link is two dentry's pointing to the same inode (This is why you may not hardlink across partitions)
A copy is two unrelated dentry's pointing to two unrelated inodes
[git] | [AURpkgs] | [arch-games]
Offline
copy:
- you have two different versions of the file.
- if you edit one, the other one stays the same.
- if you delete one, the other one stays there, but it may not be identical if it was edited
- twice as much disk space used (two different files)
hard link:
- you have one file with two different filenames.
- If you edit one, it gets edited in all filename locations
- if you delete one, it still exists in other places
- only one file on disk
soft link:
- you have one file with one filename and a pointer to that file with the other filename.
- if you edit the link its really editing the original file
- if you delete the file the link is broken
- if you remove the link the file stays in place.
- only one file on diskHope it helps.
Dusty
You know I think that did help, from reading that, I think I can see it like this:
Having copies of a file has a complete indirect reference between those files.
Having hard links of a file is like copying but with a permanent connection between those files.
Having soft links of a file allows a non-permanent connection between those files.
I think now, I'm starting to see the usefulness of hard links.
Thanks Dusty!
don't you see the difference between 2 files (2 names, 2 content - even if the same but in different place on HDD) and 1 file with 2 names in 1 place on HDD ?
don't you see the point of my first post? It's not just that, I'm just afraid of confusing myself in the future because it's hard to tell the difference between a copy and a hard link, where as with soft links, you can see the difference between them with ls -l for example:
-rw-r--r-- 2 user user 0 2008-08-14 16:20 file
-rw-r--r-- 2 user user 0 2008-08-14 16:20 hardfile
lrwxrwxrwx 1 user user 4 2008-08-14 16:20 softfile -> file
If you know some other way, that would be appreciated...
To be a bit more technical, (and this is my understanding, which may not be perfect).
A hard link is two dentry's pointing to the same inode (This is why you may not hardlink across partitions)
A copy is two unrelated dentry's pointing to two unrelated inodes
That's another way of pointing it out, that also helped broaden my understanding better.
Thank you Daenyth.
Last edited by Acecero (2008-08-14 23:47:57)
Offline
ls -l tells you how many links a file has. Here I create a file:
dusty:~ $ touch foo
dusty:~ $ ls -l foo
-rw-r--r-- 1 dusty users 0 2008-08-15 08:47 foo
The '1' in the second column tells me its the only link to that file.
Now I create a link to that file:
dusty:~ $ ln foo bar
dusty:~ $ ls -l foo
-rw-r--r-- 2 dusty users 0 2008-08-15 08:47 foo
dusty:~ $ ls -i bar
-rw-r--r-- 2 dusty users 0 2008-08-15 08:47 bar
the '2' in the second column tells me there are now two links to that file. Note that the the two references are "equal". You can rm foo or rm bar and the other file will now act like a 'normal' file with no links:
dusty:~ $ rm foo
dusty:~ $ ls -l bar
-rw-r--r-- 1 dusty users 0 2008-08-15 08:47 bar
Note how the two files have the same inum:
dusty:~ $ ln bar baz
dusty:~ $ ls -i bar
3178712 bar
dusty:~ $ ls -i baz
3178712 baz
inum is the reference to the physical contents of the file on disk (at least, in ext[23]
Finally, you can find all links to a file using the inum:
dusty:~ $ find /home -xdev -inum 3178712
/home/dusty/bar
/home/dusty/baz
Dusty
Offline
Great, that's to be expected with hard links, no worries for me. Thanks for the advice in finding link files.
Also the soft link from my example above, I notice it saids "1" in the second column while other files are "2," are soft links so different then hard links that they can't add up the number of links for the original file or is it just for the amount of soft links available to the file it points?
Offline
Great, that's to be expected with hard links, no worries for me. Thanks for the advice in finding link files.
Also the soft link from my example above, I notice it saids "1" in the second column while other files are "2," are soft links so different then hard links that they can't add up the number of links for the original file or is it just for the amount of soft links available to the file it points?
The soft link itself is actually a totally different file that holds some information that 'points at' the other file. So the number of physical references to the file is still 1. Basically, a hard link happens at the filesystem level, but the soft link is above (or within, if you prefer) the filesystem. Therefore the filesystem has no idea that the symbolic (soft) link is a reference to that file; all it sees is two totally different files.
I can't remember the exact command to find all symbolic links to a file off the top of my head, but google will tell you.
Dusty
Offline