Is possible to make symlinks that 'follow' the file?

Behavior:

me@local: tree
.
|-- ba
|   `-- file.txt
|-- foo
|   `-- file.txt -> ba/file.txt
`-- foobar

now, let's say i move the file.txt from ba to foobar:

me@local: mv ba/file.txt foobar/file.txt

I noticed that the symlink in foo folder still point the ba/file.txt path.

There is a way to create symlinks that auto-update the paths when the source file is moved?

Or symlinks that being deleted when i delete the source file?

Will be great.


Solution 1:

You could create hard links.

Hard links have their own downsides, e.g., no links to directories, no links across filesystems. And they're not deleted when you delete the "original"; in fact, they prevent the drive space for being freed and you can still access the content through the hard link.

You can't create hard links in the file browser (Nautilus), but there's a bug report about it.

Solution 2:

There's no way to do this with the normal linux filesystems as far as I'm aware.

Symlinks are really just a dumb bit of text that says "look, I'm really called Foo" - there's no checking that Foo exists, as you've found out. You can make symlinks that point to nothing with the same ease you can make symlinks that point to something useful.

Maybe someone with more knowledge of filesystems other than ext2/3/4 (like, say, jfs? or zfs? or zfs?) might be able to say if this is possible under more advanced filesystems than ext2/3/4. Maybe NTFS might support this even? NTFS doesn't have symlinks as such, but there is some other slightly similar thing (the name of which escapes me right now).

Solution 3:

Yes, you can.

In the shell, you must type:

ln ba/file.txt foo/file.txt

this is a strong link, and if you even delete the file in ba/file.txt, it remains in foo/file.txt (you can see how many links there are with ls -l command

If you want to do what you did (create a symbolic link), you must type:

ln -s ba/file.txt foo/file.txt