6

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.

11

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.

5
  • 2
    symlinks are the new kid on the block, and I think other operating systems that only allowed symlinks have caused people to never know that ln has been known to work without -s ;) – msw Sep 28 '10 at 13:34
  • msw is right, for some reason not using -s with ln scared me for a while, I never thought it was appropriate to do. In reality, it's not an issue unless you have crazy partitioning schemes and move files across them alot. – TJ L Sep 28 '10 at 16:32
  • I don't think hardlinks actually do either of the things you asked? They don't "auto-update the paths when the source file is moved", and they don't get "deleted when i delete the source file". – Ben Williams Sep 30 '10 at 7:27
  • @Ben: yes they do what i need, if i move a file hard links follow it – Strae Oct 1 '10 at 11:08
  • Personally, I think hardlinks are a bad idea in the general case. I found a lot of pitfalls to using hardlinks and I'm finding more every day. See superuser.com/questions/402295/… – MountainX Mar 29 '12 at 22:16
1

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).

1
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.