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?...
- 10-22-2011 #1Just Joined!
- Join Date
- Oct 2011
- Location
- STL, USA
- Posts
- 8
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?
- 10-22-2011 #2Linux Guru
- Join Date
- Oct 2007
- Location
- Tucson AZ
- Posts
- 1,939
Nothing, I expect. You will probably get an error message like file not found or couldn't open file.
Never actually tried this myself.
- 10-22-2011 #3
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.
- 10-23-2011 #4
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:
So if we have this situation:Code:ln foo bar # Create a new link "bar" that points to the inode pointed at by "foo" rm foo # Remove the link "foo"
And we run the command "mv link2 link3", we have this:Code:link1 link2 | | | | |--> inode <------
So if we have this situation:
As you see, link1 is entirely unaffected.Code:link1 link3 | | | | |--> inode <------
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:
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/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.Code:symlink ---> /home/user/file /home/user/different_file ---> inode
DISTRO=Arch
Registered Linux User #388732


Reply With Quote