Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a UNIX command to dereference a symbolic link? I would like to replace the link by a copy of the file it points to.

Example:

$ ls
a
b -> a

$ deref b
$ ls 
a
b

Now, a and b have the same content but are independent of each other. My question is if there is such a deref command. Important: I don't have to know where b points to; the command should figure that out.

share|improve this question

migrated from stackoverflow.com Jul 15 '11 at 0:59

This question came from our site for professional and enthusiast programmers.

up vote 9 down vote accepted

You can use readlink to find out the filename, but you don't have to!

cp b c
mv c b

It's that simple. If you are writing a script to do that, you should use the output of mktemp instead of c to make sure you don't override already existing file c.

share|improve this answer

It is possible by copying whatever the symlink points to...

see readlink(1) and cp(1). Update: and while you're at it: readlink(2).

BUT a symlink is really just that, a symbolic (read: by means of the name of the target) link to another file - it does not share the content of the other file. (hardlink, anyone?).

share|improve this answer

Combining both answers leads to the following:

#!/bin/bash

if [ -h "$1" ] ; then
  target=`readlink $1`
  rm "$1"
  cp "$target" "$1"
fi
share|improve this answer

There's no such command, but you can do the following:

rm b
cp a b
share|improve this answer
    
That's not ideal because I have to know where b points to. I'd like to avoid having to find that out myself. – dehmann Jul 14 '11 at 19:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .