심볼릭 링크를 대상으로 교체

심볼릭 링크를 대상으로 교체

디렉토리(및 하위 항목)의 모든 심볼릭 링크를 Mac OS X의 대상으로 어떻게 바꿀 수 있습니까? 대상을 사용할 수 없는 경우 소프트 링크를 그대로 두는 것이 좋습니다.

답변1

Mac OSX 별칭을 사용하는 경우 find . -type l아무 것도 나타나지 않습니다.

다음 [Node.js] 스크립트를 사용하여 심볼릭 링크의 대상을 다른 디렉터리로 이동/복사할 수 있습니다.

fs = require('fs')
path = require('path')

sourcePath = 'the path that contains the symlinks'
targetPath = 'the path that contains the targets'
outPath = 'the path that you want the targets to be moved to'

fs.readdir sourcePath, (err,sourceFiles) ->
    throw err if err

    fs.readdir targetPath, (err,targetFiles) ->
        throw err if err

        for sourceFile in sourceFiles
            if sourceFile in targetFiles
                targetFilePath = path.join(targetPath,sourceFile)
                outFilePath = path.join(outPath,sourceFile)

                console.log """
                    Moving: #{targetFilePath}
                        to: #{outFilePath}
                    """
                fs.renameSync(targetFilePath,outFilePath)

                # if you don't want them oved, you can use fs.cpSync instead

답변2

다음 버전은 다음과 같습니다.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

새 파일 이름은 대상 이름과 같습니다.

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

답변3

교체 후 파일 이름이 무엇인지 말하지 않았습니다.

이 스크립트는 대체된 링크가 링크와 동일한 이름을 가져야 한다고 간주합니다.

for link in `find . -type l`
do 
  target=`\ls -ld $link | sed 's/^.* -> \(.*\)/\1/'`
  test -e "$target" && (rm "$link"; cp "$target" "$link")
done

파일의 이름이 대상과 동일해지기를 원한다면 이렇게 해야 합니다.

for link in `find . -type l`
do
  target=`\ls -ld $link | sed 's/^.* -> \(.*\)/\1/'`
  test -e "$target" && (rm $link; cp "$target" `dirname "$link"`/`basename "$target"`)
done

관련 정보