LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-27-2004, 09:13 PM   #1
granny
Member
 
Registered: Nov 2002
Distribution: RH
Posts: 141

Rep: Reputation: 15
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?
 
Old 02-27-2004, 10:02 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
'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.

Last edited by slakmagik; 02-27-2004 at 10:08 PM.
 
1 members found this post helpful.
Old 02-28-2004, 11:10 AM   #3
citrus
Member
 
Registered: Dec 2003
Location: California
Distribution: Kubuntu 6.1
Posts: 548

Rep: Reputation: 30
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?
 
Old 02-28-2004, 07:56 PM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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'.
 
Old 03-02-2004, 02:06 PM   #5
citrus
Member
 
Registered: Dec 2003
Location: California
Distribution: Kubuntu 6.1
Posts: 548

Rep: Reputation: 30
so if i had a hard link to a dir and i deleted it would it delete the contents of my ACTUAL directory
 
Old 03-02-2004, 05:50 PM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.
 
Old 03-02-2004, 07:46 PM   #7
citrus
Member
 
Registered: Dec 2003
Location: California
Distribution: Kubuntu 6.1
Posts: 548

Rep: Reputation: 30
i see
so what are the benifits of using a hard link?
 
Old 03-02-2004, 08:58 PM   #8
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.
 
Old 03-02-2004, 09:11 PM   #9
citrus
Member
 
Registered: Dec 2003
Location: California
Distribution: Kubuntu 6.1
Posts: 548

Rep: Reputation: 30
sounds cool one more question what is the command to make a hard link?
 
Old 03-02-2004, 09:19 PM   #10
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.
 
Old 03-02-2004, 11:59 PM   #11
citrus
Member
 
Registered: Dec 2003
Location: California
Distribution: Kubuntu 6.1
Posts: 548

Rep: Reputation: 30
thanks man appreciate the help
 
Old 03-03-2004, 12:56 AM   #12
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
No problem.
 
Old 04-03-2015, 11:35 PM   #13
Lunar
Member
 
Registered: Feb 2005
Location: Texas, USA
Distribution: opensuse
Posts: 94

Rep: Reputation: 3
Quote:
Originally Posted by slakmagik View Post
'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.
 
Old 04-04-2015, 06:02 AM   #14
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Quote:
Originally Posted by citrus View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to remove a hard link... trey85stang Linux - Newbie 17 07-14-2008 01:17 PM
cp: will not create hard link ????? taiwf Linux - Newbie 3 09-20-2005 07:56 PM
hard link not working ghee22 Linux - General 5 08-31-2005 01:46 PM
Soft Link and Hard Link Moataz Red Hat 1 04-25-2005 07:30 AM
hard link? linuxzouk Linux - Newbie 5 05-15-2004 01:20 AM


All times are GMT -5. The time now is 10:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration