LinuxQuestions.org
Help answer threads with 0 replies.
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 03-08-2012, 01:24 PM   #1
svenxix
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian, Fedora
Posts: 24

Rep: Reputation: 0
Why use hard links?


I understand why you want to use links. It saves time and space. But it seems like you would always want to use symbolic links. They overcome all the limitations that hard links have. So what do hard links have that soft links don't?
 
Old 03-08-2012, 01:30 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,592
Blog Entries: 2

Rep: Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047
If you remove the file a soft link points to the soft link will be broken and not work anymore. This doesn't happen with a hard link, the file will still be accessible through the link. Only if any hard link pointing to a file (in fact pointing to an inode) is removed the file (in fact the inode) will be removed.
 
Old 03-08-2012, 02:19 PM   #3
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,887

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
You can also rename either branch of the hard link without breaking anything.

With a hard link, the link points to the inode directly. With a symbolic link, the link points to the object (which then in-turn points to the inode). Hard links are a way of bypassing the middle-man, it's like creating a copy of the original file, while only using the disk space of one file.

That said, I almost never use them. They do have their advantages though.

Last edited by suicidaleggroll; 03-08-2012 at 02:21 PM.
 
Old 03-08-2012, 02:23 PM   #4
devilboy09
Member
 
Registered: Nov 2011
Location: Iran
Distribution: Debian, CentOS, LFS
Posts: 351

Rep: Reputation: 7
hard links are like copies and soft links are like shortvuts.why do want to use either of them ?
 
Old 03-08-2012, 03:29 PM   #5
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,592
Blog Entries: 2

Rep: Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047
Quote:
Originally Posted by devilboy09 View Post
hard links are like copies and soft links are like shortvuts.why do want to use either of them ?
Because they are useful. Simple answer.
 
Old 03-08-2012, 03:51 PM   #6
svenxix
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian, Fedora
Posts: 24

Original Poster
Rep: Reputation: 0
I don't have any application in mind, I'm just trying to understand it.

A symbolic link can span file systems or reference directories, which a hard link can't do.

A hard link can reference deleted files, but I can't think of a reason why you would want this behavior.

In other words, a symbolic link seems more desirable in all circumstances. Why use a hard link instead of a symbolic link then?
 
Old 03-08-2012, 04:05 PM   #7
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,887

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
Hard links are useful for backups. It allows you to make a "copy" of the file, without actually copying the file. For example, say you have a very important, very large set of data, multiple people have access to it and use it regularly. Now you want to make a backup of this data, just in case some nimrod deletes something or renames the directory and breaks your system, however you can't afford to actually copy the data (say it's 20TB), what do you do?

A symbolic link is useless, but a hard link will allow you to make a "backup" of the data elsewhere on the filesystem, without using any additional disk space. If some user accidentally moves, renames, or deletes one of these critical files, it disappears from the primary location, but the backup location still works, so you can just hard link it back into where it's supposed to go, no worse for the wear. If you want to actually delete the file, you just delete it in both the primary and backup locations.

The Mac timemachine backup system uses hard links like they're going out of style. Quite the surprise when the backup drive only contains 100GB of data, but when you cp it onto another machine (without setting the necessary hard link flags) it explodes into a terabyte or more.

Last edited by suicidaleggroll; 03-08-2012 at 04:08 PM.
 
Old 03-08-2012, 04:11 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,592
Blog Entries: 2

Rep: Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047
It is not only about deletion of files. Hard links can make your life much easier when moving files. Imagine you have a file and 10 soft-links pointing to it. If you now move the file all the soft-links will be broken and you have to adapt all of them. That isn't necessary with hard-links, they still would be working.

EDIT: suicidaleggroll was faster.

Last edited by TobiSGD; 03-08-2012 at 04:13 PM.
 
Old 03-08-2012, 04:27 PM   #9
svenxix
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian, Fedora
Posts: 24

Original Poster
Rep: Reputation: 0
Thats a great explanation. Thanks
 
Old 03-08-2012, 04:34 PM   #10
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,151

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Ah so! Is that what's happening when btrfs or LVM creates a "snapshot?" I did wonder why the snapshot was so small.

Of course, since the hard link is "pointing to" the inode, that inode remains in the active storage, using space on the drive(s), even when the link target is no longer needed. Is there some easy way to find all the hard links to some object so the space used by the object can be released when you really want to delete it?
 
Old 03-08-2012, 04:37 PM   #11
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,887

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
Quote:
Originally Posted by PTrenholme View Post
Of course, since the hard link is "pointing to" the inode, that inode remains in the active storage, using space on the drive(s), even when the link target is no longer needed.
That is correct. Every inode keeps track of the number of hard links pointing to it. Any time you create any file, this number is initialized to 1. Whenever you add a hard link to the same inode, that number will increment by 1. The file is only considered "erased" when the number of hard links to it drops to 0.

Quote:
Originally Posted by PTrenholme View Post
Is there some easy way to find all the hard links to some object so the space used by the object can be released when you really want to delete it?
I don't know if there's a faster way, but you can use find to locate any other files on the filesystem that point to the same inode as the file you're interested in (aka: hard links to the file you want to delete). There probably is a way to print out the actual path/filename for any hard links pointing to a given inode without having to use find to search the entire filesystem, but I don't know what it is.

Last edited by suicidaleggroll; 03-08-2012 at 04:39 PM.
 
Old 03-08-2012, 04:47 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by suicidaleggroll View Post
Hard links are useful for backups. It allows you to make a "copy" of the file, without actually copying the file. For example, say you have a very important, very large set of data, multiple people have access to it and use it regularly. Now you want to make a backup of this data, just in case some nimrod deletes something or renames the directory and breaks your system, however you can't afford to actually copy the data (say it's 20TB), what do you do?
Of course, thinking of a hard link as a back-up is a tad dangerous if the nimrod
doesn't move or delete a file, but modifies its content ... :|
 
Old 03-08-2012, 04:50 PM   #13
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,887

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
Quote:
Originally Posted by Tinkster View Post
Of course, thinking of a hard link as a back-up is a tad dangerous if the nimrod
doesn't move or delete a file, but modifies its content ... :|
True, it only protects against deletion, not modification.

If you wanted to get fancy, you could implement a versioned backup system using hard links though. Copy the original directory into a dated backup directory. From then on you create a new dated backup directory and start cycling through the files to be backed up. Any file that has been added or differs from the version in the previous backup, you copy into the current backup, any file that's the same you just hardlink from the previous backup. That way all of your dated backup directories contain every backed up file, but only take up the hard drive space of the files that changed since the last backup. You could do the same thing with soft links, however with hard links you could go back and clean up old backups, without worrying about deleting the target from your newer backups for any files that haven't changed in a while. The file will only actually be deleted when all backups that reference it have been deleted.

Last edited by suicidaleggroll; 03-08-2012 at 05:07 PM.
 
Old 06-20-2012, 08:30 PM   #14
merlinblack
LQ Newbie
 
Registered: Dec 2007
Location: Sydney, Australia
Distribution: Ubuntu, Ubuntu Server, Linux From Scratch, Fedora, Mac OSX, and Windows sometimes.
Posts: 20

Rep: Reputation: 0
Wink

If you're interested in incremental back ups with rsync (using hard links) the basic operation is:
Code:
mv backup.6 backup.7
mv backup.5 backup.6
.....
mv backup.0 backup.1
rsync -a --delete --link-dest=../backup.1 source_directory/  backup.0/
Have a look at http://www.mikerubel.org/computers/rsync_snapshots/ for some more detailed info.
 
Old 12-14-2012, 11:34 PM   #15
DarktechJRock
LQ Newbie
 
Registered: Dec 2012
Posts: 1

Rep: Reputation: Disabled
music collection

I'm using hardlinks for music playlists..
my NAS stores entire albums for artists, then I hardlink my favorite tracks into another directory to create a sort of playlist.
Then I share that directory out to other devices multiple ways; CIFS, Plex Media Server (with DLNA enabled for PS3 / XBox360), Logitech Media Server (for whole house song syncing).
This saves space instead of having multiple copies, the con is, it takes a while to build the library at first... but it's worth the space savings to me.
 
  


Reply

Tags
links, newb


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
Symbolic links Vs Hard links sulekha Linux - General 2 10-02-2008 07:03 AM
one way hard links? x_terminat_or_3 Linux - General 7 10-25-2007 04:03 PM
technical question about hard links v soft links 7stud Linux - Newbie 2 02-23-2007 06:57 PM
links (hard links and soft links..) sachitha Programming 1 08-10-2005 12:10 PM
Sym links and hard links akudewan Linux - Newbie 4 02-09-2005 05:08 AM


All times are GMT -5. The time now is 01:17 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration