Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have several symlinks to other files in a directory. I want to convert these links into independent files.

Is there a command that does this?

share|improve this question
    
are they hard or soft (symbolic) links? –  swatso33 May 4 '12 at 17:11
    
They're soft links - editing the question. –  Utkarsh Sinha May 4 '12 at 17:16
    
I'm interested to know how to do this for hard links. –  Craig McQueen Oct 19 '12 at 2:16

4 Answers 4

up vote 9 down vote accepted
cp --remove-destination "$(readlink <symlink>)" <symlink>
share|improve this answer
    
That felt good! –  Utkarsh Sinha May 4 '12 at 17:35

While Ignacio's is a good reply, I wanted to automate the process for every file that is a symlink in the current directory and subdirectories.

This does the trick:

find . -type l -exec cp \"{}\" \"{}.tmp$$\" \; -exec mv \"{}.tmp$$\" \"{}\" \;

Hope this helps!

share|improve this answer

The first way that comes to my mind would be to copy all of the links to new files then delete all of the links.

cp <link> <link>.new
rm <link>

Hopefully the files have some sort of common naming structure so you can use wildcarding and only run the commands once otherwise you might want to write a simple shell script

share|improve this answer
    
That's what I've been doing till now. I was hoping unix would have some command that does this automatically. –  Utkarsh Sinha May 4 '12 at 17:21

FYI: Mac OS X does not support the --remove-destination flag so I wrote a simple script to put in your /usr/bin directory:

FILE=$1
TEMP=$1".tmp"

mv $FILE $TEMP

if [ -e $TEMP ]
then
    cp "$(readlink $TEMP)" $FILE

    if [ -f $FILE ]
    then
        rm $TEMP
    else
        echo "unln failed."
    fi
fi

This one let's you use wildcards:

for FILE in "$@"
do
    TEMP=$FILE".tmp"

    if [ -h $FILE ]
    then
        mv $FILE $TEMP

        if [ -e $TEMP ]
        then
            cp "$(readlink $TEMP)" $FILE

            if [ -f $FILE ]
            then
                rm $TEMP
            else
                echo "unln failed for link: $FILE"
            fi
        fi
    fi
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.