如何移动相对符号链接?

发布于 2024-12-21 05:00:25 字数 71 浏览 1 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慈悲佛祖 2024-12-28 05:00:25

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

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

编辑:

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

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

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 -c to convert the absolute paths back into relative paths.

别挽留 2024-12-28 05:00:25

这是一个保留相对路径的 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 "$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;
}

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;
}
撕心裂肺的伤痛 2024-12-28 05:00:25

当然使用 ln

for i in *; do # or whatever pattern you're wanting to match
    ln -sr "$(readlink "$i")" newdir/"$i";
done;

我很惊讶它能起作用,但是 LN(1) 必须足够聪明才能注意到正在发生的事情并帮助您!我什至尝试使用 ../somethingelse (这应该是链接重写中的无操作)和 .. (这将删除来自链接目标的 ..),效果非常好。

Use ln of course:

for i in *; do # or whatever pattern you're wanting to match
    ln -sr "$(readlink "$i")" newdir/"$i";
done;

I was surprised this works, but LN(1) must be smart enough to take note of what's going on and help you out! I even tried it with a "newdir" of ../somethingelse (which should be a no-op in the link re-writing) and .. (which will remove a .. from the link target), and it worked wonderfully.

墨落画卷 2024-12-28 05:00:25

改进 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"

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

Improving on Christopher Neylan's answer:

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

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

南风几经秋 2024-12-28 05:00:25

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

例如:

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

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

For example:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文