I have this folder /full/path/sourceFolder with the following files:

full/path/sourceFolder/fileA /full/path/sourceFolder/copyA -> fileA /full/path/sourceFolder/copyB

You can see that the symlink point to the file in the same folder and doesn't use full-path but relies on relative path to link.

Now, I need a copy command that would copy all files named copy* in the sourceFolder to a destinationFolder. When I copy file copyA, it should copy the source of symlink i.e. fileA but keep the name copyA. The copy command should also work for copying file copyB.

Can this be done?

I tried these commands but it doesn't work, mostly because my reference is relative:

cp -rf --preserve=links /full/path/sourceFolder/copy* /target/path/ cp -RP /full/path/sourceFolder/copy* /target/path/ cp -Hrf /full/path/sourceFolder/copy* /target/path/ cp -Lrf /full/path/sourceFolder/copy* /target/path/

I'm running out of ideas, What is the most elegant way to achieve this?

  • Can you correct your example? Sometimes you include sourceFolder, sometimes not. And also show what result you want? cp path1/* path2/ results in symlinks in path1 being copied to regular files in path2, for me. – Joe P Jun 15 '17 at 21:57
  • 1
    Using 4 spaces at the beginning of the lines is easier for blocks of coding than using lots of ` characters. – Joe P Jun 15 '17 at 21:58
  • This seems an odd thing to want to do. I would expect a requirement that a copy creates a broken link, which is then resolved when the target is copied. If you want this sort of behaviour, use hard links. – AFH Jun 15 '17 at 21:59
  • fixed the paths... – rrlamichhane Jun 15 '17 at 22:04
  • I cannot recreate the issue. For me the plain cp /full/path/sourceFolder/copy* /target/path/ works, unless I misunderstood something. The resulting files are regular files with names copyA and copyB. My testbed is Ubuntu 16.04.2 LTS. – Kamil Maciorowski Jun 15 '17 at 22:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.