LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-02-2013, 08:13 PM   #1
tekra
Member
 
Registered: Aug 2010
Location: Brisbane, Australia
Distribution: Gentoo, Slackware, PCLOS
Posts: 66
Blog Entries: 1

Rep: Reputation: 2
Choosing between soft links and hard links.


I'm writing a Perl script to serve as a Data Archiving and Storage Manager. It use three levels of classification, Categories, Subjects and Topics, that define three levels of subdirectories, with items (i.e. data files) entered appropriately. So one might have Mathematics > Calculus > Integration and Mathematics > Calculus > Differentiation. A file on Isaac Newton might go in Mathematics > Calculus > History, but also in Science > Physics > History and in Metaphysics > Alchemy > Alchemists. Two specifications enter here:

1. Multiple classifications are allowed - sc. each file can be entered under two or more headings if required.
2. No file may be duplicated.

The solution adopted, since I'm using an ext3 filesystem, is to use links. The question arises as to whether soft or hard links are most appropriate. Soft links are "pointers" to files and directories, whereas hard links are alternative names for the same file. I understand that directories should not be hard-linked, since this creates multiple parents, breaking the directory tree structure and rendering it unusable.

Are there any guidelines or usage recommendations that differentiate soft and hard links to allow an optimum selection, or is the choice simply a preference?
 
Old 01-02-2013, 09:01 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,601

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Symbolic links make the most sense when each file has one classification that you consider "primary" and that is where the actual file will always be stored. Otherwise you run the risk of, for example, deleting or renaming the "Metaphysics" hierarchy and thus breaking any symlinks to files that were actually stored there. With hard links, all the links are co-equal, and deleting or renaming one of the links would not affect the others. Hard links also have the minor advantage that a simple "ls -l" for any of the links tells you how many links that file has. (With either type of link you have to scan the filesystem to find out where those other links are.)

The down side of any type of link is that accidentally breaking a link and creating an unwanted second, and perhaps slightly different, copy of a file is pretty easy to do. (Just use an editor that isn't careful about how it handles symlinks or files with multiple hard links.) In my experience, that sort of problem seems to happen more often with hard links.
 
2 members found this post helpful.
Old 01-02-2013, 10:52 PM   #3
tekra
Member
 
Registered: Aug 2010
Location: Brisbane, Australia
Distribution: Gentoo, Slackware, PCLOS
Posts: 66
Blog Entries: 1

Original Poster
Rep: Reputation: 2
> each file has one classification that you consider "primary"

Yes; I gave this some thought but decided that it was at best ill-defined, and superfluous in most cases. This led me to prefer hard links, since all are peers.

> a simple "ls -l" for any of the links tells you how many links that file has.

Didn't know this, and it's certainly a plus - thanks.

> The down side of any type of link ... multiple hard links.)

Have never used hard links, so this is an interesting caveat - thanks again.
 
Old 01-03-2013, 12:45 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,601

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Quote:
Originally Posted by tekra View Post
> The down side of any type of link ... multiple hard links.)

Have never used hard links, so this is an interesting caveat - thanks again.
The problem occurs because the safest way to update a file with the typical single link is to create a new file and then, once the new file has been written successfully, rename it over the old file. If this is done for a file with more than one link, the linking is broken and all of the other links are left pointing to the old content. Most editors will recognize that a file with multiple links should not be updated that way and revert to directly writing over the existing file. An example of an editor that does not avoid breaking the linking is sed with the "-i" (--in-place) option. (It is a stream editor that writes its output while reading its input. Those two cannot be the same file.)
 
2 members found this post helpful.
Old 01-03-2013, 02:34 PM   #5
tekra
Member
 
Registered: Aug 2010
Location: Brisbane, Australia
Distribution: Gentoo, Slackware, PCLOS
Posts: 66
Blog Entries: 1

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by rknichols View Post
The problem occurs because ... Those two cannot be the same file.)
Invaluable. I mostly use nedit for this sort of thing, so will check its operation. I'll leave this for a day and then mark it solved in case of further addenda, but I guess now that preference is the only real criterion. BTW, could you confirm that my understanding about hard-linking directories is correct?
 
Old 01-03-2013, 06:01 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,601

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Quote:
Originally Posted by tekra View Post
BTW, could you confirm that my understanding about hard-linking directories is correct?
Confirmed. Even root cannot create hard links to a directory. Quite a few pieces of software make the tacit assumption that the directory structure is a tree, and making extra hard links to a directory would break that.

FWIW, a long time ago in a galaxy quite close to this one, actually, SunOS did allow root to make hard links to a directory. I tried doing it. Recovery from that was not easy (took some low-level fiddling with the fsdb file system debugger). No, I don't recall why I thought it might be a good idea.
 
Old 01-03-2013, 08:36 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Here's a lateral approach (given you specified Perl as the front-end) and is probably what I would do.
Have only the 'primary' path exist in reality and create a tied hash in Perl to store that path string and all the 'alternate path' strings and have them all resolve (eg HoHoH...) to the one true path
No links requited at all.
How you specify the Hash structure depends on how you intend to access the files and whether they will have guaranteed unique names.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
technical question about hard links v soft links 7stud Linux - Newbie 2 02-23-2007 07:57 PM
Is this how links (soft/hard) work? RHLinuxGUY Linux - General 1 10-14-2006 11:06 PM
links (hard links and soft links..) sachitha Programming 1 08-10-2005 01:10 PM
Soft vs. Hard Links jrdioko Linux - Newbie 2 06-13-2004 03:41 PM
Hard/Soft Links nodger Linux - General 1 05-15-2004 11:36 AM


All times are GMT -5. The time now is 03:19 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration