Linux 'cp' copies symlink instead of target file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












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



base_v21.txt -> base_v001.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
base_v21.txt


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.










share|improve this question



















  • 3




    You shuold edit your question to make it understandable. Check the "base" names....
    – schily
    Aug 9 at 6:21






  • 4




    giving the output of ls -l instead of only ls would make things more clear for all the answerers
    – A.B
    Aug 9 at 6:32











  • 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 at 7:00










  • 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 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 at 21:57














up vote
1
down vote

favorite












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



base_v21.txt -> base_v001.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
base_v21.txt


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.










share|improve this question



















  • 3




    You shuold edit your question to make it understandable. Check the "base" names....
    – schily
    Aug 9 at 6:21






  • 4




    giving the output of ls -l instead of only ls would make things more clear for all the answerers
    – A.B
    Aug 9 at 6:32











  • 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 at 7:00










  • 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 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 at 21:57












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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



base_v21.txt -> base_v001.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
base_v21.txt


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.










share|improve this question















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



base_v21.txt -> base_v001.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
base_v21.txt


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.







linux symlink cp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 14 at 19:01









ilkkachu

50.9k678140




50.9k678140










asked Aug 9 at 0:08









ETD

142




142







  • 3




    You shuold edit your question to make it understandable. Check the "base" names....
    – schily
    Aug 9 at 6:21






  • 4




    giving the output of ls -l instead of only ls would make things more clear for all the answerers
    – A.B
    Aug 9 at 6:32











  • 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 at 7:00










  • 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 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 at 21:57












  • 3




    You shuold edit your question to make it understandable. Check the "base" names....
    – schily
    Aug 9 at 6:21






  • 4




    giving the output of ls -l instead of only ls would make things more clear for all the answerers
    – A.B
    Aug 9 at 6:32











  • 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 at 7:00










  • 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 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 at 21:57







3




3




You shuold edit your question to make it understandable. Check the "base" names....
– schily
Aug 9 at 6:21




You shuold edit your question to make it understandable. Check the "base" names....
– schily
Aug 9 at 6:21




4




4




giving the output of ls -l instead of only ls would make things more clear for all the answerers
– A.B
Aug 9 at 6:32





giving the output of ls -l instead of only ls would make things more clear for all the answerers
– A.B
Aug 9 at 6:32













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 at 7:00




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 at 7:00












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 at 18:48




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 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 at 21:57




@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 at 21:57










3 Answers
3






active

oldest

votes

















up vote
2
down vote













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
base_v21.txt

>cat base_v21.txt
test

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

>cat base_v21.txt
test





share|improve this answer





























    up vote
    0
    down vote













    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/





    share|improve this answer





























      up vote
      0
      down vote













      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)






      share|improve this answer






















      • 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 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 at 18:45










      • That's alright. It is weird to imagine cp behaving as described, but that is how the question is worded.
        – Eminent
        Aug 14 at 20:12










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461405%2flinux-cp-copies-symlink-instead-of-target-file%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      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
      base_v21.txt

      >cat base_v21.txt
      test

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

      >cat base_v21.txt
      test





      share|improve this answer


























        up vote
        2
        down vote













        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
        base_v21.txt

        >cat base_v21.txt
        test

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

        >cat base_v21.txt
        test





        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          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
          base_v21.txt

          >cat base_v21.txt
          test

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

          >cat base_v21.txt
          test





          share|improve this answer














          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
          base_v21.txt

          >cat base_v21.txt
          test

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

          >cat base_v21.txt
          test






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 14 at 17:41









          ilkkachu

          50.9k678140




          50.9k678140










          answered Aug 9 at 5:02









          Tom Kelly

          31726




          31726






















              up vote
              0
              down vote













              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/





              share|improve this answer


























                up vote
                0
                down vote













                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/





                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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/





                  share|improve this answer














                  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/






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 9 at 6:33

























                  answered Aug 9 at 6:27









                  sai sasanka

                  54019




                  54019




















                      up vote
                      0
                      down vote













                      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)






                      share|improve this answer






















                      • 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 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 at 18:45










                      • That's alright. It is weird to imagine cp behaving as described, but that is how the question is worded.
                        – Eminent
                        Aug 14 at 20:12














                      up vote
                      0
                      down vote













                      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)






                      share|improve this answer






















                      • 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 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 at 18:45










                      • That's alright. It is weird to imagine cp behaving as described, but that is how the question is worded.
                        – Eminent
                        Aug 14 at 20:12












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      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)






                      share|improve this answer














                      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)







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 14 at 18:46









                      ilkkachu

                      50.9k678140




                      50.9k678140










                      answered Aug 9 at 1:31









                      Eminent

                      112




                      112











                      • 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 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 at 18:45










                      • That's alright. It is weird to imagine cp behaving as described, but that is how the question is worded.
                        – Eminent
                        Aug 14 at 20:12
















                      • 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 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 at 18:45










                      • That's alright. It is weird to imagine cp behaving as described, but that is how the question is worded.
                        – Eminent
                        Aug 14 at 20:12















                      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 at 18:39




                      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 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 at 18:45




                      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 at 18:45












                      That's alright. It is weird to imagine cp behaving as described, but that is how the question is worded.
                      – Eminent
                      Aug 14 at 20:12




                      That's alright. It is weird to imagine cp behaving as described, but that is how the question is worded.
                      – Eminent
                      Aug 14 at 20:12

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461405%2flinux-cp-copies-symlink-instead-of-target-file%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      The Forum (Inglewood, California)

                      Palaiologos