有没有一种简单的方法来批量重命名符号链接目标?

时间:2012-07-12 16:41:38

标签: linux shell

rename之类的内容适用于文件,但适用于符号链接目标。

所以,如果我有

link1 -> /usr/share/lib-1.2.2/file1
link2 -> /usr/share/lib-1.2.2/file2

我能做到

rename 1.2.2 2.0.3 link*

并且

link1 -> /usr/share/lib-2.0.3/file1
link2 -> /usr/share/lib-2.0.3/file2

2 个答案:

答案 0 :(得分:6)

使用此脚本:

#!/bin/sh
from="$1"
to="$2"
shift 2
for i
do
  a=$(readlink "$i") && ln -sf "$(echo $a | sed "s@$from@$to@")" "$i"
done

用法示例:

$ ls -l
total 0
lrwxrwxrwx 1 igor igor 6 Jul 12 18:56 1 -> /bin/1
lrwxrwxrwx 1 igor igor 7 Jul 12 18:56 10 -> /bin/10
lrwxrwxrwx 1 igor igor 6 Jul 12 18:56 2 -> /bin/2
lrwxrwxrwx 1 igor igor 6 Jul 12 18:56 3 -> /bin/3
lrwxrwxrwx 1 igor igor 6 Jul 12 18:56 4 -> /bin/4
lrwxrwxrwx 1 igor igor 6 Jul 12 18:56 5 -> /bin/5
lrwxrwxrwx 1 igor igor 6 Jul 12 18:56 6 -> /bin/6
lrwxrwxrwx 1 igor igor 6 Jul 12 18:56 7 -> /bin/7
lrwxrwxrwx 1 igor igor 6 Jul 12 18:56 8 -> /bin/8
lrwxrwxrwx 1 igor igor 6 Jul 12 18:56 9 -> /bin/9
<links>$ bash ../rename-links /bin/ /usr/bin/ *
<links>$ ls -l
total 0
lrwxrwxrwx 1 igor igor 10 Jul 12 18:57 1 -> /usr/bin/1
lrwxrwxrwx 1 igor igor 11 Jul 12 18:57 10 -> /usr/bin/10
lrwxrwxrwx 1 igor igor 10 Jul 12 18:57 2 -> /usr/bin/2
lrwxrwxrwx 1 igor igor 10 Jul 12 18:57 3 -> /usr/bin/3
lrwxrwxrwx 1 igor igor 10 Jul 12 18:57 4 -> /usr/bin/4
lrwxrwxrwx 1 igor igor 10 Jul 12 18:57 5 -> /usr/bin/5
lrwxrwxrwx 1 igor igor 10 Jul 12 18:57 6 -> /usr/bin/6
lrwxrwxrwx 1 igor igor 10 Jul 12 18:57 7 -> /usr/bin/7
lrwxrwxrwx 1 igor igor 10 Jul 12 18:57 8 -> /usr/bin/8
lrwxrwxrwx 1 igor igor 10 Jul 12 18:57 9 -> /usr/bin/9

答案 1 :(得分:3)

util的-Linux的2.23

重命名(1):    - 支持新命令行选项--symlink重命名符号链接目标

ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.23/v2.23-ReleaseNotes