1

I have created a shared object of 1.2 M and created 4 soft links for that SO. Size of all the links is 20B and the size of the main so is 1.2M

20         May 23 10:56 libAbc.so -> libAbc.so.2.0.11.0
20         May 23 10:56 libAbc.so.1 -> libAbc.so.2.0.11.0
20         May 23 10:56 libAbc.so.1.0 -> libAbc.so.2.0.11.0
1.2M        May 23 10:56 libAbc.so.2.0.11.0

While i tried to copy all these files to another folder using cp , the size of the links is equal to the main file.

1.2M May 24 08:07 libABC.so
1.2M May 24 08:07 libABC.so.1
1.2M May 24 08:07 libABC.so.1.0
1.2M May 24 08:07 libABC.so.2.0.11.0

I have also used

cp -s libAgent.so* src/ 

which is also failing with an error

cp: `src/libAgent.so': can make relative symbolic links only in current directory

Why is it failing to copy the softlinks with their original size(20B)

user2256825
  • 594
  • 6
  • 22

2 Answers2

3

Copying a soft link usually copies the file the soft link refers to and does not replicate the soft link.

If you wish to use cp to copy soft links as new soft links, specify the -a option to cp.

cp -a libAbc.so* /path/to/another/folder

The info page for cp says:

`-a'
`--archive'
     Preserve as much as possible of the structure and attributes of the
     original files in the copy (but do not attempt to preserve internal
     directory structure; i.e., `ls -U' may list the entries in a copied
     directory in a different order).  Equivalent to `-dpR'.

The key is to select the "archive" option of the command used to copy the link. rsync is an effective alternative as Maxim Egorushkin points out, and rsync has the capability of copying extended attributes and ACLs if the command includes the appropriate command-line arguments.

rsync -aAX libAbc.so* /path/to/another/folder

The rsync man page says:

-a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
    --no-OPTION             turn off an implied OPTION (e.g. --no-D)
-A, --acls                  preserve ACLs (implies -p)
-X, --xattrs                preserve extended attributes

In the OP's use case, the -A and -X are not needed.

A caveat is that if the soft link is a relative link, copying it to a new location may make the link inoperative because it does not point to an absolute path.

For example:

$ ls -al /usr/lib/rpmpopt
lrwxrwxrwx 1 root root 19 2012-05-02 12:40 /usr/lib/rpmpopt -> rpm/rpmpopt-4.4.2.3

cp -a /usr/lib/rpmpopt ${HOME}/tmp and rsync -av /usr/lib/rpmpopt ${HOME}/tmp on my machine both create a broken link rpmpopt -> rpm/rpmpopt-4.4.2.3 because my local copy has no rpm sub-folder. One needs to consider this fact when deciding what to copy.

This link is broken because ${HOME}/tmp/rpm is not present:

$ ls -nl ${HOME}/tmp/rpmpopt
lrwxrwxrwx 1 505 700 19 2016-05-24 10:04 /home/kbulgrien/tmp/rpmpopt -> rpm/rpmpopt-4.4.2.3
kbulgrien
  • 4,384
  • 2
  • 26
  • 43
2

rsync is less error-prone then cp:

rsync -av libAgent.so* src/
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • Note that rsync is vulnerable to the same issue that the soft link in the new location is not guaranteed to work. I.E. `rsync -av /usr/lib/rpmpopt ${HOME}/tmp` on my machine creates a broken link `rpmpopt -> rpm/rpmpopt-4.4.2.3` because my local copy has no *rpm* sub-folder. One needs to consider this fact when deciding what to copy. – kbulgrien May 24 '16 at 15:07
  • @kbulgrien This is true, but for a use case of copying a directory where symlinks point only within that directory, this works pretty well. Alternatively there are `--safe-links` and `--copy-unsafe-links` command line options. – Maxim Egorushkin May 24 '16 at 15:13
  • Yes, it is true that `rsync -a` resolves the OP's use case, so forgive the nitpick, but I believe that `-L` aka `--safe-links` or `--copy-unsafe-links` makes a copy of the referenced file instead of a symbolic link. (Kudo's for suggesting rsync, BTW). – kbulgrien May 24 '16 at 15:33
  • @kbulgrien Well, versioned `.so` symlinks should stay symlinks. – Maxim Egorushkin May 24 '16 at 15:36