用目标替换符号链接

Geo*_*kos 4 mac-osx symbolic-link

在 Mac OS X 上,如何将目录(和子目录)中的所有符号链接替换为它们的目标?如果目标不可用,我宁愿不理会软链接。

Den*_*son 5

以下是chmeee答案的版本,readlink如果任何文件名中有空格,则使用并将正常工作:

新文件名等于旧链接名:

find . -type l | while read -r link
do 
    target=$(readlink "$link")
    if [ -e "$target" ]
    then
        rm "$link" && cp "$target" "$link" || echo "ERROR: Unable to change $link to $target"
    else
        # remove the ": # " from the following line to enable the error message
        : # echo "ERROR: Broken symlink"
    fi
done
Run Code Online (Sandbox Code Playgroud)

新文件名等于目标名称:

find . -type l | while read -r link
do
    target=$(readlink "$link")
    # using readlink here along with the extra test in the if prevents
    # attempts to copy files on top of themselves
    new=$(readlink -f "$(dirname "$link")/$(basename "$target")")
    if [ -e "$target" -a "$new" != "$target" ]
    then
        rm "$link" && cp "$target" "$new" || echo "ERROR: Unable to change $link to $new"
    else
        # remove the ": # " from the following line to enable the error message
        : # echo "ERROR: Broken symlink or destination file already exists"
    fi
done
Run Code Online (Sandbox Code Playgroud)