1

我有几百个符号链接要转移位置。现在例如:

randomfile -> /home/me/randomfile
randomfile2 -> /home/me2/randomfile2

有很多这样的文件。假设我想将符号链接全部转换为链接

randomfile -> ../me/randomfile
randomfile2 -> ../me2/randomfile2

批处理这个最快的方法是什么?

4

1 回答 1

3
for f in *; do
  ## if not a symlink, ignore this file
  [[ -L "$f" ]] || continue
  ## determine where it points
  tgt=$(readlink "$f")
  ## if not pointing to /home/*, ignore this file
  [[ $tgt = /home/* ]] || continue
  ## calculate the new target
  new_tgt=../${tgt#/home/}
  ## actually create the new link
  ln -T -sf "$new_tgt" "$f"
done

这使用 bash(在模式下不起作用/bin/sh)和 GNU ln - 对于其他实现,在ln -T不可用的情况下,当目标是目录时可能需要额外的逻辑。

于 2012-06-05T00:20:27.820 回答