Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I just read that linux has a hard link and a soft link. I know that a soft link can link to a directory and link to a file ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2004
    Posts
    2

    Hard and Soft links


    Hi,
    I just read that linux has a hard link and a soft link. I know that a soft link can link to a directory and link to a file in another filesystem and the hard link can't, but I haven't understood whats the differences between them by definition that makes the soft link more flexible. They are both small files that hold only the name of another file right? Why is there a hard link if a soft link can do the job and more? I’ll appreciate any relevant information. Thanks a lot for helping!!
    Ofer

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    The difference between a hard link and a soft link is quite enormous. A "hard link" is the process of creating a new name for a file. Both names will point to the same physical file, and if you delete one name (using "rm" or similar), the file will still remain under the other name. Names point to an i-node number, which is why they cannot span different filesystems. They could technically point to a directory, but that would break the garbage collector and the filesystem tree semantics, so it's simply forbidden, in order not to damage the system. For example, imagine that you created you created a hard link to a directory inside itself, and then removed the link to that directory from its parent directory. The garbage collector wouldn't see that the directory can't be accessed, since it still has links to it, but you'll be unable to remove it, since you can't access it by any name.

    When you create a "soft link" (or symbolic link (or symlink for short), as it's really called), you just create a small file that contains the name of the file that it points to. It has the obvious advantage of being able to link across filesystems, but on the other hand, the symlink is a file in its own right and isn't strictly linked, which means that if you delete the original name of the file (ie. the name that the symlink points to), the file will be deleted and the symlink will be "dangling", ie. it will point to a file that doesn't exist.

    The symbolic link is really a hack, that doesn't really do its job very well. It's just that it would be extremely hard to make a hard link able to do the same thing.

  3. #3
    Just Joined!
    Join Date
    Jan 2004
    Posts
    2

    Thanks!

    Thanks for helping me with that!
    Do you mean that if I'll create a hard link to a file, there will not be any differences between the link and the original file? Does linux remove the physical file only when there isn't any hard links linking to it?
    Thanks again,
    Ofer

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    The thing is, that Linux filesystems don't have two-way mappings between files and filenames. A file does not have a name, it's only a name that has a file, if you see what I mean. Therefore, it's also possible to create more names to a specific file, and that is what is often referred to as hard-linking. Therefore, there is no difference at all between the original name and the new name, as you correctly stated. To know when to actually remove a file, the filesystem only keeps a record of the number of names a file has. If you run "ls -l", you'll see that link number before the owner.

    What you say is basically correct, only it's not certain that the file will be removed when there are no names for it anymore. If a process still has the file open, it will remain on disk until that process closes the file. For example, if you start playing an MP3 file with XMMS, you can remove the file name while it's still playing, unlike Windows for example, which will just give you an error message that the file still is in use. Once you stop the playback and XMMS closes the file descriptor, it will be removed by the system.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •