Find the answer to your Linux question:
Results 1 to 4 of 4
So if there are several links in the system pointing to a particular file and the file is then moved what happens?...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Location
    STL, USA
    Posts
    8

    Question Hard and Soft Links?


    So if there are several links in the system pointing to a particular file and the file is then moved what happens?

  2. #2
    Linux Guru
    Join Date
    Oct 2007
    Location
    Tucson AZ
    Posts
    3,190
    Nothing, I expect. You will probably get an error message like file not found or couldn't open file.
    Never actually tried this myself.

  3. #3
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,134
    It depends on what you move. If foo1 foo2 foo3 and foo4 are links to the same data,
    if you move foo1, the data has not moved, only the link has moved. You assume that
    when you move a file to another place you are moving the data, but that is not necessarily
    true.

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    So, it depends on what sort of link you have.

    The way that UNIX filesystems work is that the filesystem contains a link to an inode. The inode contains information like the permissions of the file, where it is actually stored on disk, etc. When I say link here, I refer to what is sometimes called a "hard link". So there can be multiple "hard links" to a single inode.

    The "mv" command is basically a wrapper around "create a new link, remove the old link". So when you say "mv foo bar", it actually runs:
    Code:
    ln foo bar # Create a new link "bar" that points to the inode pointed at by "foo"
    rm foo # Remove the link "foo"
    So if we have this situation:
    Code:
    link1            link2
     |                |
     |                |
     |--> inode <------
    And we run the command "mv link2 link3", we have this:
    So if we have this situation:
    Code:
    link1            link3
     |                |
     |                |
     |--> inode <------
    As you see, link1 is entirely unaffected.


    A symbolic link (aka soft link) is a totally different story. A symbolic link points to a specific filepath. So, we might have a situation like this:
    Code:
    symlink ---> /home/user/file
    /home/user/file ---> inode
    Now, if we move /home/user/file with "mv /home/user/file /home/user/different_file", we have:
    Code:
    symlink ---> /home/user/file
    /home/user/different_file ---> inode
    Because symlink now points to a file that doesn't exist, it doesn't point to anything unless /home/user/file is recreated.

Posting Permissions

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