Linking Files: Hard Link vs. Soft Link?

Tim Frost timfrost at xtra.co.nz
Tue Apr 14 07:45:13 UTC 2009


On Tue, 2009-04-14 at 09:24 +0300, Amichai Rotman wrote:
> Hello,
> 
> I have an alphabetized file hierarchy I'd like to organize into
> categories. These are all my media files - documents, video, audio
> etc.
> 
> I created another hierarchy containing the categories I want and
> linked the files from the main hierarchy to the relevant category,
> using a soft link (ln -s).
> 
> I realized it's a waste of space (two inodes for each file), so I
> thought re-creating the links as hard links.
> 
> My dilemmas:
> 
> Is it really better? - I understand that if I hard link a file, it is
> actually the same file, and if I delete the so called link I actually
> delete the real file - might be dangerous... I know I am stating the
> obvious, but I'd like your input on this (pros and cons).

Hard links don't work for files in different file systems, which imposes
restrictions on their use if you have your music/video collection spread
over more than one disk.

In your situation, if you use hard links for the files, and have a hard
link in each hierarchy for EVERY file, then you don't lose anything if
you remove the hierarchy that you don't want.  In fact, you can have
multiple hierarchies (eg, composer, artist, genre, ...), and only
physically have one copy of the file on disk.

If you have soft links in the second hierarchy, then as soon as you
remove a file from the original hierarchy, you have broken the link in
the second hierarchy. 

So, with hard links, the data (video,audio/...) is still accessible
until ALL the hard links to that data have been removed (that count is
the second field of 'ls -l ....', but isn't available as a column in
nautilus)

> 
> Is there a way to create hard links with a GUI? - I am using KDE
> 3.5.10. Till now I've created half a dozen hard links using the ln
> command in CLI, but it will take me forever to create all those I
> need....
No.  And if you could, I suspect that it would take longer than
scripting the task in the CLI.
> 
> Is it possible to create a hard link to a directory somehow?

No. In general, a directory is a container, and as such it defines a
place in the tree which holds files (and other directories).  That
container only makes sense in that one place in the tree.

>  - when I use the ln command to link a directory, I get an error
> saying it is not possible. If that's true, the whole thing is
> pointless:
Creating a directory is NOT the same as linking it into the structure. 

 
>  I'd like to create a directory under the category hierarchy and then
> hard link the files under it.

What you describe here does make sense, if the directory is created in
the new place.

If you have a way of automatically classifying the files in the existing
directory structure, and identifying the placement and name in the new
structure, I would write a script that processes each file, and, for
each file,
 1: Creates new directories as needed 
    ('mkdir -p $NEWDIR' will create the target directory and any missing
parents)

 2: link the file into the new location with a command like
     'ln $existing $new'

The script should be smart enough to report name clashes in the new
hierarchy, and other errors that may occur, but the above is a minimal
skeleton.

>  It will still mean less inodes, but it seems to me there has to be a
> way....
> 

Total disk requirement will be the space for the new directories.

Tim

-- 
Tim Frost <timfrost at xtra.co.nz>





More information about the ubuntu-users mailing list