Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a folder and within it there are many links.

I make those links absolute, so that I can move them about within the folder and they still point to the same data. Relative links would break if I moved them within the folder.

But the problem arises when i move the folder to another system, say an external hard drive or server. Then all the absolute links have the wrong base in their address.

If it were possible to have the links have a variable within their address pointing, then that would solve this problem.

Is that possible?

share|improve this question
    
Could hardlinks (also note the answer linked in the comment) help in this situation? –  sr_ Jul 24 '12 at 13:01
    
I don't see how. –  Hermann Ingjaldsson Jul 24 '12 at 13:12
add comment

3 Answers 3

up vote 2 down vote accepted

I guess the answer to your problem are variant symbolic links, though they only exist on DragonFlyBSD to the best of my knowledge (see that previous question: Dynamic Symlinks )

share|improve this answer
add comment

The command

symlinks -cr folder

will convert all absolute symlinks under folder to relative. So you can then move the entire folder with the links pointing to their correct relative location (assuming they all point to other files within folder)

Then something like...

find folder -type l -exec echo 'LNK=$(readlink -f "{}");rm -fv "{}";ln -sv "$LNK" "{}"' ';' > xlinks
chmod +x xlinks
./xlinks

... will convert all the links from relative to absolute.

(Note: I gave up trying to execute the command directly from find. Anyway, it gives you a chance to check what it's going to do!)

share|improve this answer
add comment

You can't have your cake and eat it. Relative links are generally better, because you can move them around and copy all or part of the tree to another location.

If you move a file within the hierarchy, it's not clear what should happen: if there are links a -> b and c -> d, and you run mv b d, should a now point to d? It's up to you to decide. Symbolic links point to a location; if you want to point to a file, use a hard link (this is not possible for directories).

If you move a file but still want to find it under its old location, add a symbolic link from the old location to the new location.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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