如何复制带有符号链接的目录并解析它们?

Pwd*_*wdr 7 bash shell terminal symlink copy

我想递归复制包含符号链接(符号链接)以及带有 Bash/Shell 脚本的普通文件的目录的内容。我不知道如何复制符号链接内容。伪代码如下所示:

for file in directory do
  if is symlink
    resolve symlink and copy its contents
  else 
    copy the file / folder
Run Code Online (Sandbox Code Playgroud)

我的目录结构如下所示:

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

在复制过程之后,我想要一个这样的目录结构:

base/
  dir1/
  symlinkdir1/ (no symlink, actual directory)
    /file1
    /file2
  symlinkdir2/ (no symlink, actual directory)
    /file3
    /file4
  …
Run Code Online (Sandbox Code Playgroud)

Dan*_*sta 23

cp -rL /source /destination

r = 递归 L = 跟随和扩展符号链接

  • MacOS 上需要大写 -R (3认同)
  • -L 将用真实文件替换符号链接。这就是为什么我的答案是正确的而另一个是错误的。 (2认同)