How do I move a relative symbolic link? How do I move a relative symbolic link? unix unix

How do I move a relative symbolic link?


You can turn relative paths into full paths using readlink -f foo. So you would do something like:

ln -s $(readlink -f $origlink) $newlinkrm $origlink

EDIT:

I noticed that you wish to keep the paths relative. In this case, after you move the link, you can use symlinks -c to convert the absolute paths back into relative paths.


This is a perl solution that preserves relative paths:

use strictures;use File::Copy qw(mv);use Getopt::Long qw(GetOptions);use Path::Class qw(file);use autodie qw(:all GetOptions mv);my $target;GetOptions('target-directory=s' => \$target);die "$0 -t target_dir symlink1 symlink2 symlink3\n" unless $target && -d $target;for (@ARGV) {    unless (-l $_) {        warn "$_ is not a symlink\n";        next;    }    my $newlink = file(readlink $_)->relative($target)->stringify;    unlink $_;    symlink $newlink, $_;    mv $_, $target;}


One can use tar to move a folder containing relative symbolic links.

For example:

cd folder_to_move/..tar czvf files.tgz folder_to_movecd dest_folder/..tar xzvf /absolute/path/to/folder_to_move/../files.tgz# If all is fine, clean-uprm /absolute/path/to/folder_to_move/../files.tgzrm -rf /absolute/path/to/folder_to_move