116

i have a folder with symlinks:

marek@marek$ ls -al /usr/share/solr/
razem 36
drwxr-xr-x   5 root root  4096 2010-11-30 08:25 .
drwxr-xr-x 358 root root 12288 2010-11-26 12:25 ..
drwxr-xr-x   3 root root  4096 2010-11-24 14:29 admin
lrwxrwxrwx   1 root root    14 2010-11-24 14:29 conf -> /etc/solr/conf

i want to copy it to ~/solrTest but i want to copy files from symlink as well

when i try to cp -r /usr/share/solr/ ~/solrTest

i will have symlink here:

marek@marek$ ls -al ~/solrTest
razem 36
drwxr-xr-x   5 root root  4096 2010-11-30 08:25 .
drwxr-xr-x 358 root root 12288 2010-11-26 12:25 ..
drwxr-xr-x   3 root root  4096 2010-11-24 14:29 admin
lrwxrwxrwx   1 root root    14 2010-11-24 14:29 conf -> /etc/solr/conf

4 Answers 4

149
cp -Lr /usr/share/solr/ ~/solrTest

Check the man page for unix commands with man cp

   -L, --dereference
          always follow symbolic links in SOURCE
3
  • 6
    I get "cp: the -H, -L, and -P options may not be specified with the -r option."
    – balupton
    Apr 22, 2012 at 13:45
  • 5
    @balupton: try -LR
    – ySgPjx
    May 30, 2013 at 22:12
  • 1
    if you are on a mac and have the coreutils package installed (brew list coreutils), you may use -Lr. However, -LR works universally, so is a better choice for scripts. You can check which cp you have with man cp | tail -n1, e.g. a system without GNU coreutils: BSD February 23, 2005 BSD and a system with GNU coreutils: macOS 13.3 February 23, 2022 macOS 13.3 Apr 21, 2023 at 21:48
15

From man page:

‘-L’ ,‘--dereference’ - Follow symbolic links when copying from them. With this option, cp cannot create a symbolic link. For example, a symlink (to regular file) in the source tree will be copied to a regular file in the destination tree.

So this is the option you should try.

7
cp -r -L /usr/share/solr/ ~/solrTest

From the cp(1) man page:

-L, --dereference

always follow symbolic links in SOURCE

0

One quick solution is to:

$ mkdir dest_dir
$ cp symlink_dir/* dest_dir/

the drawback is that you have to create the destination directory first

1
  • In some shells, this won't copy hidden files/folders from symlink_dir.
    – vmrob
    Jan 31, 2017 at 3:50

You must log in to answer this question.