3

I am trying to copy a link on Solaris OS but find that it does not simply copy the link instead copies the whole contents of the directory/file the link is poinitng to? Which is not in other OSes like AIX,HP-UX,Linux.

Is this a normal behaviour of Solaris OS?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
kadeshpa
  • 1,607
  • 5
  • 18
  • 23

4 Answers4

8

Charlie was close, you want the -L, -H or -P flags with the -R flag (probably just -R -P). Similar flags exist for chmod(1) and chgrp(1). I've pasted an excerpt from the man-page below.

Example:

$ touch x
$ ln -s x y 
$ ls -l x y 
-rw-r--r--   1 mjc      mjc            0 Mar 31 18:58 x
lrwxrwxrwx   1 mjc      mjc            1 Mar 31 18:58 y -> x
$ cp -R -P y z
$ ls -l z
lrwxrwxrwx   1 mjc      mjc            1 Mar 31 18:58 z -> x
$ 

Alternatively, plain old tar will happily work with symbolic links by default, even the venerable version that ships with Solaris:

tar -cf foo | ( cd bar && tar -xf - )

(where foo is a symlink or a directory containing symlinks).

 /usr/bin/cp -r | -R [-H | -L | -P] [-fip@] source_dir... target

 ...

 -H    Takes actions based on the type and  contents  of  the
       file  referenced  by  any symbolic link specified as a
       source_file operand.

       If the source_file operand is a symbolic link, then cp
       copies  the  file  referenced by the symbolic link for
       the source_file  operand.  All  other  symbolic  links
       encountered  during  traversal of a file hierarchy are
       preserved.


 -L    Takes actions based on the type and  contents  of  the
       file  referenced  by  any symbolic link specified as a
       source_file operand or any symbolic links  encountered
       during traversal of a file hierarchy.

       Copies files referenced by  symbolic  links.  Symbolic
       links encountered during traversal of a file hierarchy
       are not preserved.


 -P    Takes actions on any  symbolic  link  specified  as  a
       source_file  operand  or any symbolic link encountered
       during traversal of a file hierarchy.

       Copies symbolic links. Symbolic links encountered dur-
       ing traversal of a file hierarchy are preserved.
Martin Carpenter
  • 5,893
  • 1
  • 28
  • 32
  • There is still a problem with 'cp -rP'. If a symbolic link to be copied points to non-existing file, cp will not copy it. – shura Feb 09 '10 at 22:56
  • Yes, `cp -RP` worked for me on Oracle Solaris 10 1/13 s10s_u11wos_24a SPARC: `# cp -RP c2t240048FD8E70ED10d10s0 c4t0d0s0`; `# ls -l c4t0d0s0 lrwxrwxrwx 1 root root 91 Nov 15 22:39 c4t0d0s0 -> ../../devices/pci@0/pci@0/pci@8/pci@0/pci@9/SUNW,qlc@0/fp@0,0/ssd@w240048fd8e70ed10,a:a,raw` – saulius2 Nov 15 '19 at 20:39
1

You want cp -P I believe (check the man page, as I don't have a solaris box handy right now.) I faintly suspect that's a System V-ism, but wouldn't swear to it.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • Well there is no such -P option.I tried and this is how the output looks: $ cp -P /opt/standard_perl/link /opt/standard_perl/link_new Usage: cp [-f] [-i] [-p] [-@] f1 f2 cp [-f] [-i] [-p] [-@] f1 ... fn d1 cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] d1 ... dn-1 dn – kadeshpa Mar 31 '09 at 16:16
  • 1
    Yes there is on Solris. See http://docs.sun.com/app/docs/doc/816-5165/cp-1?l=en&a=view You sure you're using Solaris? – Charlie Martin Apr 01 '09 at 00:05
0

Will cpio do the trick for you?

Celada
  • 21,627
  • 4
  • 64
  • 78
NoahD
  • 8,092
  • 4
  • 27
  • 28
0

It sounds like you're trying to duplicate a single symlink.

You might want to just do:


link_destination=`/bin/ls -l /opt/standard_perl/link|awk '{print $10}'`
ln -s $link_destination /opt/standard_perl/link_new

If you are trying to copy a directory hierarchy, this can be very difficult to do in general without the GNU tools (or rsync). While there are solutions that often work, there is no solution that works on every "standard" unix with every type of filename you might encounter. If you're going to be doing this regularly, you should install the GNU coreutils, find, cpio, and tar, and also rsync as well.

chuck
  • 922
  • 6
  • 6