how do I know if a file has a hard link to it?
ok I know when I create a soft link, it shows a ----> in a long listing and is colored (if ls --color) in aqua.
But hard links don't do this. How can I tell if a file/dir has a hard link to it or if it points to another file? |
'stat -c %h <file>'
-- er, uh, or 'ls -l' and look at the second column. Sorry. I have to make everything complicated. 1's a normal file and 2+ is a hard link for a regular file. |
what is a hard link? and what is the difference between a soft and a hard
soft just point to another place kinda like a sybolic link right? |
I think a hard link is an alternate name pointing to the same inode as the original and, after that, they are indistinguishable. Whereas a symlink is just a small file containing a path, basically - it's only a pointer to the original. 'Symlink' is just another name for a 'soft link'.
|
so if i had a hard link to a dir and i deleted it would it delete the contents of my ACTUAL directory
|
Well, the contents of your actual directory is data on a disk. The system keeps track of it by inode number. The 'filename' you use is kind of like a user-friendly domain name like 'linuxquestions.org' when the system actually finds stuff by IP number. Both filenames are just descriptions of the inode. If you do 'touch foo; ln foo bar; stat foo bar' you'll see they're identical. Then do 'echo baz >> bar' and stat again - still the same - and then 'rm foo' and 'cat bar' and it'll still print 'baz'.
So long story short, no, deleting one won't delete the contents - just remove a reference name. |
i see
so what are the benifits of using a hard link? |
I really don't know. :) I mean, there may be technical issues I'm not aware of. The only benefit I can think of is that you can't have a broken hard link. If you move the original, the symlink will break and, if you create a symlink with a relative path and move it, it'll break. But I've never seen the need to use one. I *think* symlinks were introduced later than hardlinks, so maybe you could just look at them as a replacement mechanism for similar functionality that's maybe a better mechanism. It's no trouble to link across filesystems or to keep straight what's a link and what isn't and so on. So stability of hard links is about all I can think of.
|
sounds cool one more question what is the command to make a hard link?
|
The command for both is 'ln' - kind of indicating which has priority: to make a sym/softlink, you have to add the option '-s', like 'ln -s' and to make a hard link, it's just 'ln'. But it's the same command.
|
thanks man appreciate the help
|
No problem. :)
|
Quote:
Landis. |
Quote:
Some backup utilities (e.g. storebackup) make use of this to create an archive of old backup states, which only consist in hard links. You can e.g. have daily snapshots of your home directory for one week. If nothing changes, they would not consume more space then just one single copy. Still you can delete any one of these snapshots and all the others are still intact. |
All times are GMT -5. The time now is 01:19 AM. |