Bash脚本在目录中创建符号链接

时间:2017-08-26 08:50:32

标签: linux bash shell unix symlink

我有一个bash脚本:

    for file in `find -mindepth 1 -maxdepth 1`
    do
      old=`pwd`
      new=".${file##*/}"
      newfile="$old/${file##*/}"
      cd
      wd=`pwd`
      link="$wd/$new"
      ln -s -f $newfile $link
      cd $old
    done

这是为了在用户的主目录中用'创建一个符号链接。'预先添加到当前工作目录中的所有文件和目录。这样可以正常工作,但是,它还会在同一目录的任何子目录中创建一个符号链接。例如。 foo现在会在〜/ .foo和foo / foo上有一个链接。

有什么方法可以解决这个问题吗?

编辑:感谢Sameer Naik和dave_thompson_085我稍微改了一下,但即使将一个替代目录作为参数给出,问题仍然存在。它不是子目录的问题,它是两个链接,一个是〜/ .foo123,另一个是foo123 / foo123,而不是链接到〜/ for foo1和foo1 / foo2。

for file in `ls -l | grep -v total | awk '{print  $NF;}'`
do
    old=`pwd`
    wd=`cd $1 && pwd`
    new=".${file}"
    newfile="$wd/${file}"
    cd
    wd=`pwd`
    link="$wd/$new"
    ln -s -f $newfile $link
    cd $old
done

1 个答案:

答案 0 :(得分:0)

由于您不想进入子目录,请尝试使用

for file in `ls -l | grep -v ^d | awk '{print $NF;}'`

或在查找中使用-type f排除子目录

相关问题