所需实现功能:

把dir目录中所有的链接的路径进行修改,格式为 /data/root/path/to/file -> /newdata/root/path/tofile ,即只修改最顶上两级的路径

 

实现思路:

下面这些要写成一个方法,方便递归

for file in `ls $dir`

{
  file="$dir/$file"
  if ( -h $file ) {          //如果是符号链接

    sourcefile=readlink $file    //符号链接所指向的路径

    sourcefile=`echo $sourcefile | sed "s, preg,replace,g"` //正则替换并重新赋值

    unlink $file          //去除符号链接

    ln -s $sourcefile $file      //重新建立符号链接

  }

  if ( -d $file ) { //如果遇到文件夹则递归

    ...

  }

}

 

 

需要用到的shell知识

if ( -h $file ) {

  command ...

}
-h 表示判断文件是否存在且是否是符号链接
readlink 获取文件所指向的目标路径
unlink 去除符号链接

ls -s sourcefile destfile 创建符号链接

相关文章:

  • 2021-08-02
  • 2021-09-07
  • 2021-08-12
  • 2021-07-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2021-11-18
猜你喜欢
  • 2021-12-24
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2021-07-13
  • 2021-08-18
  • 2022-01-24
相关资源
相似解决方案