Reemplazar enlace simbólico con destino

Reemplazar enlace simbólico con destino

¿Cómo se pueden reemplazar todos los enlaces simbólicos en un directorio (y los secundarios) con sus destinos en Mac OS X? Si el objetivo no está disponible, prefiero dejar el enlace suave como está.

Respuesta1

Si está utilizando alias de Mac OSX, find . -type lno se le ocurrirá nada.

Puede utilizar el siguiente script [Node.js] para mover/copiar los destinos de sus enlaces simbólicos a otro directorio:

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

Respuesta2

Aquí hay versiones dechmeeerespuesta que usa readlinky funcionará correctamente si hay espacios en cualquier nombre de archivo:

El nuevo nombre de archivo es igual al nombre del enlace anterior:

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

El nuevo nombre de archivo es igual al nombre de 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

Respuesta3

No dijiste qué nombres deberían tener los archivos después del reemplazo.

Este script considera que los enlaces reemplazados deben tener los mismos nombres que tenían como enlaces.

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

Si desea que los archivos tengan el mismo nombre que el destino, esto debería ser suficiente.

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

información relacionada