Linux 用于更改符号链接指向的位置的脚本

Linux 用于更改符号链接指向的位置的脚本,linux,bash,Linux,Bash,我有几百个符号链接,我想转移位置。例如,现在: randomfile -> /home/me/randomfile randomfile2 -> /home/me2/randomfile2 有很多这样的文件。假设我想将符号链接全部转换为某种链接 randomfile -> ../me/randomfile randomfile2 -> ../me2/randomfile2 最快的批处理方法是什么 for f in *; do ## if not a symlink

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

randomfile -> /home/me/randomfile
randomfile2 -> /home/me2/randomfile2
有很多这样的文件。假设我想将符号链接全部转换为某种链接

randomfile -> ../me/randomfile
randomfile2 -> ../me2/randomfile2
最快的批处理方法是什么

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
不可用,当目标是目录时,可能需要额外的逻辑。

你说的“快速”是什么意思?你的意思是“原子”吗?如中所述,如果它们有不一致的引用,就会导致bug,而这一点非常重要,足以花费额外的时间编写代码来维护该约束?你的意思是最小化开发时间吗?这是一项一次性任务。数百个需要更改符号链接的文件。对于每个文件/主/将成为的文件,更改非常一致。。