7

I'm a little bit confused with hard/softlinks here. I'm on OSX, have a folder '4' and two links pointing to it:

drwxr-xr-x  4 imm  staff  136 14 Jun 21:24 4
lrwxr-xr-x  1 imm  staff    1 14 Jun 16:56 4.0 -> 4
lrwxr-xr-x  1 507  wheel    1 14 Jun 16:56 Current -> 4

When I follow 'Current' in folder it leads to a completely different location (the path I copied all the stuff from). I'd like 'Current' to point on the folder '4' at the new location.

How can I achieve this?

2 Answers 2

13

You need to remove and recreate the link. You can do this with

rm Current

And then recreate it:

ln -s /path/to/the/new/location Current

You can to use an absolute path (for simplicity).

If you want the path to move when you move the link, then you can use a relative path. Just bear in mind that it stores the relative path and only resolves it from the link's current location when you try to access it.

Edit: since you mention hardlinks, I'll go ahead and introduce them here.

A hardlink is similar to a symlink, in that it still points to a file, but it is limited to the current file system, but also it is faster. A hardlink points to a specific inode on the disk, instead of another location in the filesystem. You cannot have relative hardlinks.

You can consider a basic filesystem as a collection of hardlinks to locations on disk, one per file. Creating additional hardlinks just creates additional pointers to the same file. They are considerably faster because the system knows exactly where on disk the file it needs is (a symlink has to be resolved, and then that new file has to be looked up and resolved), but because of this it's limited only to inodes on the current filesystem.

The filesystem automatically tracks the number of hardlinks pointing to a particular inode, and simply deletes the inode when that count reaches 0.

6
  • Can I use a relative path as well?
    – Hedge
    Jun 15, 2011 at 12:33
  • 2
    You can, though it gets somewhat interesting. The relative path is stored, so you have to be mindful if you move the link (say, by moving the parent folder). It will point relative to it's new location, and will not update to continue pointing to the same spot it was before. This might be what you want, or it might not, but I know it confused me a few times at first because I didn't realize it actually stored the relative path, I thought it just resolved it into an absolute path and stored that. Jun 15, 2011 at 12:37
  • That is exactly what I want :)
    – Hedge
    Jun 15, 2011 at 12:40
  • Moved that information into the post, since that was actually what you were after. Jun 15, 2011 at 12:44
  • 1
    That's a shame. As long as it's clear, it should get a good response. Jun 15, 2011 at 12:55
10

You can just do this:

ln -nfs /path/to/the/new/location Current

where 'Current' is the current symbolic link. The -n and -f options unlink and remove the original 'Current' and allow creation of the new one (-s for soft link).

I realise that the question has been answered and original problem solved a long time ago, but the process of repointing a symbolic link without going through the separate deletion step was bothering me again, and a Google search for the solution threw up this q&a. Hope it helps someone else looking for the answer!

1
  • I've found by painful experience that the unlinking and removing aren't entirely reliable, and doing rm separately is more reliable, if somewhat verbose. Feb 5, 2019 at 15:45

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.