2011-12-15 21 views
22

que tienen una gran cantidad de enlaces relativos simbólicos que desea mover a otro directorio.¿Cómo muevo un enlace simbólico relativo?

¿Cómo se mueven los enlaces simbólicos (con una ruta relativa) preservando al mismo tiempo el camino correcto?

+0

¿Todavía quieres los enlaces sean simbólica cuando se mueve? Esto tiene una influencia en las respuestas que obtendrá. – fge

+0

Y también ¿Todavía quieres los enlaces a estar a rutas relativas, o les está cambiando a rutas absolutas bien? –

+0

Creo que esto pertenece a unix.stackexchange.com, aunque superuser.com también funcionaría. –

Respuesta

26

Puede activar las rutas relativas en las rutas completas utilizando readlink -f foo. Por lo que deberías hacer algo como:

ln -s $(readlink -f $origlink) $newlink 
rm $origlink 

EDIT:

Noté que desea mantener las rutas relativas. En este caso, después de mover el enlace, se puede utilizar para convertir symlinks -c las rutas absolutas de nuevo en las rutas relativas.

+0

confirmada en rhel5 – reconbot

8

Esta es una solución perl que conserva las rutas relativas:

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; 
} 
0

Mejora de la respuesta de Christopher Neylan:

~/bin $ cat mv_ln 
#!/bin/bash 
# 
# inspired by https://stackoverflow.com/questions/8523159/how-do-i-move-a-relative-symbolic-link#8523293 
#   by Christopher Neylan 

help() { 
    echo 'usage: mv_ln src_ln dest_dir' 
    echo '  mv_ln --help' 
    echo 
    echo ' Move the symbolic link src_ln into dest_dir while' 
    echo ' keeping it relative' 
    exit 1 
} 

[ "$1" == "--help" ] || [ ! -L "$1" ] || [ ! -d "$2" ] && help 

set -e # exit on error 

orig_link="$1" 
orig_name=$(basename "$orig_link") 
orig_dest=$(readlink -f "$orig_link") 
dest_dir="$2" 

ln -r -s "$orig_dest" "$dest_dir/$orig_name" 
rm "$orig_link" 

Esto también es parte de https://github.com/tpo/little_shell_scripts

1

Uno puede utilizar tar para mover una carpeta que contiene enlaces simbólicos relativos.

Por ejemplo:

cd folder_to_move/.. 
tar czvf files.tgz folder_to_move 
cd dest_folder/.. 
tar xzvf /absolute/path/to/folder_to_move/../files.tgz 

# If all is fine, clean-up 
rm /absolute/path/to/folder_to_move/../files.tgz 
rm -rf /absolute/path/to/folder_to_move 
+1

siquiera podía usar un tubo de alquitrán cadena para hacerlo todo en un comando '' cd folder_to_move; tar cf -. | (cd/usr/local/dest_folder; tar xf -) '' – Vorsprung

Cuestiones relacionadas