Linux 如何移动相对符号链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8523159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:40:40  来源:igfitidea点击:

How do I move a relative symbolic link?

linuxperlshellunixsymlink

提问by anasdox

I have a lot of relative symbolic links that I want to move to another directory.

我有很多我想移动到另一个目录的相对符号链接。

How can I move symbolic links (those with a relative path) while preserving the right path?

如何在保留正确路径的同时移动符号链接(具有相对路径的链接)?

采纳答案by Christopher Neylan

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

您可以使用 将相对路径转换为完整路径readlink -f foo。所以你会做这样的事情:

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

EDIT:

编辑:

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

我注意到您希望保持路径相对。在这种情况下,移动链接后,您可以使用symlinks -c将绝对路径转换回相对路径。

回答by daxim

This is a perlsolution that preserves relative paths:

这是一个perl保留相对路径的解决方案:

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 "
~/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
}

[ "" == "--help" ] || [ ! -L "" ] || [ ! -d "" ] && help

set -e # exit on error

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

ln -r -s "$orig_dest" "$dest_dir/$orig_name"
rm "$orig_link"
-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; }

回答by Tomá? Pospí?ek

Improving on Christopher Neylan's answer:

改进 Christopher Neylan 的回答:

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

This is also part of https://github.com/tpo/little_shell_scripts

这也是https://github.com/tpo/little_shell_scripts 的一部分

回答by ttq

One can use tarto move a folder containing relative symbolic links.

可以tar用来移动包含相对符号链接的文件夹。

For example:

例如:

##代码##