How to fix a symbolic link pointing to a wrong location?

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?


Solution 1:

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.

Solution 2:

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!