From: TP on
Hi everybody,

When using "cp", the hard links are not preserved.
I have found the following discussion:

http://jeremy.zawodny.com/blog/archives/010037.html

Some comments in this page claim that "cp -av --preserve=all" works. I do
not agree with that (see the example below). Can you confirm that cp does
not work? What is the best solution for my problem?

Thanks in advance

#########
#!/bin/sh
alias ls='ls --color=auto'
mkdir test_hardlink
cd test_hardlink
touch foo
ln foo bar
ls -l
cp foo not_a_hard_link
ls -l
cp -d foo not_a_hard_link2
ls -l
cp -av --preserve=all foo not_a_hard_link3
ls -l
From: Ersek, Laszlo on
In article <4b7e8e3f$0$23910$426a74cc(a)news.free.fr>, TP <TP(a)frenoespam.fr.invalid> writes:
> Hi everybody,
>
> When using "cp", the hard links are not preserved.
> I have found the following discussion:
>
> http://jeremy.zawodny.com/blog/archives/010037.html
>
> Some comments in this page claim that "cp -av --preserve=all" works. I do
> not agree with that (see the example below). Can you confirm that cp does
> not work? What is the best solution for my problem?

( cd dir1 && tar -c ... ) | (cd dir2 && tar -x)

For various definitions of tar, cpio and pax. Pax even supports a copy
mode (-r -w).

http://www.opengroup.org/onlinepubs/007908775/xcu/pax.html

Cheers,
lacos
From: jellybean stonerfish on
On Fri, 19 Feb 2010 14:12:35 +0100, TP wrote:

> Hi everybody,
>
> When using "cp", the hard links are not preserved. I have found the
> following discussion:
>
> http://jeremy.zawodny.com/blog/archives/010037.html
>
> Some comments in this page claim that "cp -av --preserve=all" works. I
> do not agree with that (see the example below). Can you confirm that cp
> does not work? What is the best solution for my problem?
>
> Thanks in advance
>
> #########
> #!/bin/sh
> alias ls='ls --color=auto'
> mkdir test_hardlink
> cd test_hardlink
> touch foo
> ln foo bar
> ls -l
> cp foo not_a_hard_link
> ls -l
> cp -d foo not_a_hard_link2
> ls -l
> cp -av --preserve=all foo not_a_hard_link3 ls -l

Maybe I don't fully understand you?

I made a directory and create a file and a hard link to the file.
js(a)blackbox:~$ mkdir test_hardlink
js(a)blackbox:~$ cd test_hardlink/
js(a)blackbox:~/test_hardlink$ touch foo
js(a)blackbox:~/test_hardlink$ ln foo bar

Then I cp the directory using -d

js(a)blackbox:~/test_hardlink$ cd ..
js(a)blackbox:~$ cp -adv test_hardlink/ test_hardlink_copy
`test_hardlink/' -> `test_hardlink_copy'
`test_hardlink/bar' -> `test_hardlink_copy/bar'
`test_hardlink/foo' -> `test_hardlink_copy/foo'

Confirm the files copied.

js(a)blackbox:~$ cd test_hardlink_copy/
js(a)blackbox:~/test_hardlink_copy$ ls
bar foo

Enter data into one of the copied files.

js(a)blackbox:~/test_hardlink_copy$ echo "data" > foo
js(a)blackbox:~/test_hardlink_copy$ cat foo
data

And then confirm that the copy preserved the hard link between foo
and bar.

js(a)blackbox:~/test_hardlink_copy$ cat bar
data

From: TP on
jellybean stonerfish wrote:

>> #########
>> #!/bin/sh
>> alias ls='ls --color=auto'
>> mkdir test_hardlink
>> cd test_hardlink
>> touch foo
>> ln foo bar
>> ls -l
>> cp foo not_a_hard_link
>> ls -l
>> cp -d foo not_a_hard_link2
>> ls -l
>> cp -av --preserve=all foo not_a_hard_link3 ls -l
>
> Maybe I don't fully understand you?

Ok! Thanks.

1/ cp -a preserves all hard links in a directory when copying this directory
recursively (-a includes "R" option, in fact -a is "-dR --preserve=all"
according to the manual page).

2/ But cp -a does not work to copy a single hard link (as I have tried in my
example).

In fact I do not need this feature, but by curiosity: why is it possible to
preserve a hard link in the first case, but not in the second?
From: Kaz Kylheku on
On 2010-02-19, TP <TP(a)frenoespam.fr.invalid> wrote:
> Hi everybody,
>
> When using "cp", the hard links are not preserved.

That's because you didn't RTFM.

> #!/bin/sh
> alias ls='ls --color=auto'

Ah, color ls. You are on GNU coreutils, right?
GNU ls, GNU cp, etc.

GNU cp has the -l option for copying hard links,
described in the info manual and man page.

You can find it by looking for occurences of
``link'' in the output of ``man ls''.

With regard to portability, cp -l is not described by the Single Unix
Specification, so it's an extension. The SUS has a little discussion
section in which it recommends the pax utility for situations where a
subtree is to be copied such that the hard link structure is preserved.