LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how do I know if a file has a hard link to it? (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-know-if-a-file-has-a-hard-link-to-it-151364/)

granny 02-27-2004 08:13 PM

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?

slakmagik 02-27-2004 09:02 PM

'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.

citrus 02-28-2004 10:10 AM

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?

slakmagik 02-28-2004 06:56 PM

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'.

citrus 03-02-2004 01:06 PM

so if i had a hard link to a dir and i deleted it would it delete the contents of my ACTUAL directory

slakmagik 03-02-2004 04:50 PM

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.

citrus 03-02-2004 06:46 PM

i see
so what are the benifits of using a hard link?

slakmagik 03-02-2004 07:58 PM

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.

citrus 03-02-2004 08:11 PM

sounds cool one more question what is the command to make a hard link?

slakmagik 03-02-2004 08:19 PM

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.

citrus 03-02-2004 10:59 PM

thanks man appreciate the help

slakmagik 03-02-2004 11:56 PM

No problem. :)

Lunar 04-03-2015 10:35 PM

Quote:

Originally Posted by slakmagik (Post 785264)
'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.

3 is a hard link on all my opensuse systems...
Landis.

joe_2000 04-04-2015 05:02 AM

Quote:

Originally Posted by citrus (Post 792088)
so what are the benifits of using a hard link?

One obvious benefit is that when you delete the original file, the hard link still allows you to access the file. A soft link would just be broken and the file would be gone.

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.