In my directory I have 2 files: a text file and a symbolic link to that text file.

base_v21.txt -> base_v001.txt

When I try to copy base_v001.txt using the following command:

# cp base_v001.txt /targetdir

Instead of base_v001.txt getting copied, base_v21.txt is copied.

# ls /targetdir

So even though I specify the file I want to copy by the full filename the symbolic link gets copied instead.

I've seen posts about using -L or -H to dereference symlinks, but that doesn't really apply to my situation, because I'm trying to copy the target file itself; not the symlink.

As for why the symlink and the target file are in the same directory, I have no control over that.

  • 4
    You shuold edit your question to make it understandable. Check the "base" names....
    – schily
    Aug 9, 2018 at 6:21
  • 5
    giving the output of ls -l instead of only ls would make things more clear for all the answerers
    – A.B
    Aug 9, 2018 at 6:32
  • 1
    Show the command that you used to create the symbolic link and also correct the filenames (you reference a filename that is base_v21.txt in the text, but not in the commands).
    – Kusalananda
    Aug 9, 2018 at 7:00
  • 2
    cp base_v001.txt /targetdir produces /targetdir/base_v21.txt? Are you absolutely sure? There's no way for the system to know what symlinks are pointing to that file. I would understand this if you used the symlink as the copy source and got the name from the symlink destination instead, or something like that.
    – ilkkachu
    Aug 14, 2018 at 18:48
  • @ilkkachu Yes, I understand it's a odd result. I've searched the web for a similar occurrence, but so far have not seen any. It forced me to make an account and post this question.
    – ETD
    Aug 14, 2018 at 21:57

4 Answers 4


Try this,

let the destination be /tmp/

If you want to copy the target file but not the symlink, :

readlink -ne base.txt | xargs -0 cp -t /tmp/

Copying to a directory creates a new file with the same name as the source file in the target directory, if a new file name is not given. So the following commands will give the same output:

> cp base_v21.txt /targetdir
> cp base_v21.txt /targetdir/base_v21.txt

These create a new file called "base_v21.txt" with the contents of the file in the source directory. In this case, these are the contents of "base_v001.txt" because of the symbolic link. Essentially, you are doing the following:

> cat base_v21.txt >> /targetdir/base_v21.txt

Thus it is to be expected that the following commands give the same result after copying:

> cat base_v21.txt
> cat base_v001.txt
> cat /targetdir/base_v21.txt

You can see that this occurs with either a hard link or symbolic link in the following examples:

> touch base_v001.txt
> ln -s base_v001.txt base_v21.txt #create soft link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls

>cat base_v21.txt

> ln base_v001.txt base_v21.txt #create hard link
> cat "test" >> base_v001.txt
> mkdir dir
> cd dir
> cp ../base_v21.txt .
> ls

>cat base_v21.txt

Indeed, that is a very strange behavior.

Check and see what conflicting arguments (if any) are aliased to the cp command. To do this, run the type command. (i.e. type cp)

Other than that, you can try to perform your operation with an alternative utility such as rsync which also has support for handling symlinks (with the arguments --links --copy-links and --hard-links)

  • It seems the OP describes a situation in which running the command cp base_v001.txt /targetdir returns with the file (symbolic link) named /targetdir/base_v21.txt. You don't find that strange?
    – Eminent
    Aug 14, 2018 at 18:39
  • Wait, I read the file names wrong. The way it's actually written is so weird I must have mentally blocked the idea, or something... Sorry.
    – ilkkachu
    Aug 14, 2018 at 18:45

The simplest way to get the target of the symlink is to use the -L option:

-L, --dereference always follow symbolic links in SOURCE

this command achieves your aim:

cp -L base_v001.txt /targetdir
  • The problem seems to be that base_v001.txt isn't even a link in there, so there's no link to (not) follow...
    – ilkkachu
    May 4 at 9:17

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