Substitua o link simbólico pelo destino

Substitua o link simbólico pelo destino

Como você pode substituir todos os links simbólicos em um diretório (e filhos) por seus destinos no Mac OS X? Se o destino não estiver disponível, prefiro deixar o link simbólico em paz.

Responder1

Se você estiver usando aliases do Mac OSX, find . -type lnão surgirá nada.

Você pode usar o seguinte script [Node.js] para mover/copiar os destinos de seus links simbólicos para outro diretório:

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

Responder2

Aqui estão versões dechmeee'sresposta que usa readlinke funcionará corretamente se houver espaços em qualquer nome de arquivo:

O novo nome do arquivo é igual ao nome do link antigo:

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

O novo nome do arquivo é igual ao nome do destino:

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

Responder3

Você não disse quais nomes os arquivos deveriam ter após a substituição.

Este script considera que os links substituídos devem ter os mesmos nomes dos links.

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

Se você deseja que os arquivos tenham o mesmo nome do destino, isso deve servir.

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

informação relacionada