imoing

导航

 

所需实现功能:

把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 创建符号链接
posted on 2013-10-08 14:12  艾莫明  阅读(516)  评论(0编辑  收藏  举报