Re: Automatic updating links?
On Tue, Jun 06, 2017 at 12:27:06PM +0100, Rodolfo Medina wrote:
> I wish to create a link to a file that wouldn't break when that file is moved
> on elsewhere in the filesystem or renamed, nay the link would automatically
> `update' pointing at the new name/address of the file. Is that possible?
> Besides, I wish that form of automatic update also when moving or renaming the
> directory where the link itself lives.
There are two types of links in Unix: "hard" links, and "soft" (symbolic)
links.
A symbolic link contains the name of its "target". When you open the
symlink, the underlying file system drivers see that it's a symlink,
read the target, and rewrite the filename to open the target that the
symlink points to. For example:
wooledg:~$ ls -ld file
-rw-r--r-- 1 wooledg 563 24 Jul 2 2015 file
wooledg:~$ cat file
line one
line two
match
wooledg:~$ ln -s file slink
wooledg:~$ ls -ld slink
lrwxrwxrwx 1 wooledg wooledg 4 Jun 6 08:18 slink -> file
wooledg:~$ cat slink
line one
line two
match
Now, if I rename "file", the symlink "slink" will no longer point to
the right place:
wooledg:~$ mv file xfile
wooledg:~$ cat slink
cat: slink: No such file or directory
I believe that's what you already knew before asking this question,
but I had to include this information to be sure, because you didn't
actually say what you had already tried.
Now, the other type of link is older and much more subtle. Hard links
work at the inode level. To really understand what a hard link is, you
need to understand how directories and inodes work in Unix.
A directory in Unix is (conceptually) just a list of filenames and
inode numbers. Imagine a telephone book. You can look up a person's
name, and next to their name is their telephone number. This is how
Unix directories work. This is where the name "directory" actually
comes from. (Phone books are formally called "directories".)
(If you're too young to know what a phone book is... then I don't know
how to help you with that.)
So. You have a directory, and inside the directory is a file. Let's
revert back to our original state:
wooledg:~$ rm slink; mv xfile file
wooledg:~$ ls -ld file
-rw-r--r-- 1 wooledg 563 24 Jul 2 2015 file
Now let's create a "hard" link to the file:
wooledg:~$ ln file hlink
wooledg:~$ ls -ld file hlink
-rw-r--r-- 2 wooledg 563 24 Jul 2 2015 file
-rw-r--r-- 2 wooledg 563 24 Jul 2 2015 hlink
See the "2" between the permissions and the owner? That's the link
count. That tells us how many hard links there are to the file. If
we inspect the inode numbers for these two directory entries, we'll
see that they are the same:
wooledg:~$ ls -li file hlink
1573283 -rw-r--r-- 2 wooledg 563 24 Jul 2 2015 file
1573283 -rw-r--r-- 2 wooledg 563 24 Jul 2 2015 hlink
Same inode number. They are in fact two different names for the same
*file*. Both directory entries contain the same inode number, which
is the only thing a directory can tell you.
Now, if I rename the file, let's observe what happens:
wooledg:~$ mv file xfile
wooledg:~$ ls -li xfile hlink
1573283 -rw-r--r-- 2 wooledg 563 24 Jul 2 2015 hlink
1573283 -rw-r--r-- 2 wooledg 563 24 Jul 2 2015 xfile
wooledg:~$ cat hlink
line one
line two
match
In this case, the renaming of *one* of the hard links did not matter.
The *other* hard link still "points to" the inode that contains the
actual contents. You can move the hard link ("file") to a subdirectory,
or any other directory inside the file system, and it won't matter.
The hard links still point to the same inode number.
Now, this part is very important: inode numbers are only meaningful
within a single file system. If you move one of the "files" *outside*
of this file system, then the hard links will no longer reference
the same file. If you modify one of them, the other will still contain
a copy of the original content.
wooledg:~$ mkdir /run/user/1000/tmp
wooledg:~$ mv xfile file
wooledg:~$ mv hlink /run/user/1000/tmp
wooledg:~$ ls -lid file /run/user/1000/tmp/hlink
1573283 -rw-r--r-- 1 wooledg 563 24 Jul 2 2015 file
319746 -rw-r--r-- 1 wooledg wooledg 24 Jul 2 2015 /run/user/1000/tmp/hlink
wooledg:~$ echo "another line" >> /run/user/1000/tmp/hlink
wooledg:~$ cat file
line one
line two
match
At this point, even though the two "files" were orignally hard linked,
that changed when we moved one of them to a different file system.
Now they are simply two files with no relationship to each other.
So... to answer your question, as long as you only have *ONE* file
system to worry about, you can make hard links, and I believe they
will satisfy your stated requirements.
Reply to: