How to copy a directory with symbolic links and resolve them?
Asked Answered
N

2

23

I would like to recursively copy the contents of a directory which contains symbolic links (symlinks) as well as normal files with a Bash / Shell Script. I don’t know how to copy the symlink-contents. The pseudocode would look something like this:

for file in directory do
  if is symlink
    resolve symlink and copy its contents
  else 
    copy the file / folder

My directory-structure looks like this:

base/
  dir1/
  symlinkdir1*/ (--> ../somewhere/else/dirA)
    /file1
    /file2
  symlinkdir2*/ (--> ../somewhere/else/dirB)
    /file3
    /file4
  …

After the copy-procedure, I would like to have a directory-structure like this:

base/
  dir1/
  symlinkdir1/ (no symlink, actual directory)
    /file1
    /file2
  symlinkdir2/ (no symlink, actual directory)
    /file3
    /file4
  …
Neill answered 18/5, 2014 at 21:11 Comment(0)
O
61

cp -rL /source /destination

r = recursive L = follow and expand symlinks

Octet answered 9/6, 2019 at 3:1 Comment(3)
Needs a capital -R on MacOSAvouch
what's the difference between using L and not using L? The two other answers don't use it and I'm wondering what's the differencePopcorn
-L will replace the symlinks with the real file. That's why my answer is the correct one and the other are wrong.Octet
R
-5

Just use cp command with -r option to recursively copy. No need of a script all together,

Ramie answered 18/5, 2014 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.