-   Linux - Software (
-   -   Making a symlink follow its target when the target moves (

pwabrahams 03-08-2011 05:44 PM

Making a symlink follow its target when the target moves
Suppose I have a symlink S pointing to a file F in a directory A, and I want to move F from A to another directory B. That move, by itself, will invalidate the symlink. What would be the best way to update S, other than explicitly rewriting it? If it would help, I could arrange for A to be a subdirectory of B or B to be a subdirectory of A. I'd prefer to do it within Dolphin, but I'll resort to the shell if necessary.

The old symlink S might be a full pathname rather than a path using ".." for navigation, which makes the problem harder. I want to do a bunch of these in a single action if possible. I expect it could be done using a shell procedure and iterating over the files to be moved, but that doesn't seem very simple.

The directory B has hundreds of entries, so I want to be sure not to lose track of F during the modification.

gilead 03-08-2011 06:10 PM

You can use find to identify the broken links in A with this:

find -L /path/to/A -type l -exec ls -ld {} \;
You could also change the exec statement to run a script that creates a link in the new location each time it finds a broken link. I haven't tried writing the script myself - I'm about to go into a meeting here...

anomie 03-08-2011 06:22 PM

@pwabrahams: dumb question - must it be a symlink? (i.e. Would a hard link do? Is the link across filesystems?)

pwabrahams 03-08-2011 07:04 PM

Broken links, hard links
I'm not so concerned about inadvertent broken links because the only broken links would be those I know about.

Hard links are a possibility I need to think about. Originally the directories were on different filesystems but for reasons unrelated to this question, I've relocated one of them so they now are on the same filesystem. I need to look at the behavior of hard links when I'm doing mv, rm, cp, or even other ln operations, both hard and soft. I've been using soft links so regularly that I never really thought about hard links, though I certainly know about them. Is it possible to create them in Dolphin?

All times are GMT -5. The time now is 08:22 PM.