# Equivalent of Linux cp -al ???



## bookem (Sep 8, 2006)

Hi,

Is there any equivalent to the linux command cp -al on Tiger, or any way of addign this functionality.

I am wanting to make a copy of a directory with hardlinks instead of duplicating the data.

thanks,


----------



## wraith (Sep 8, 2006)

Would cp -R -L do what you want?

From the Mac OS X man file :

-L    If the -R option is specified, all symbolic links are followed.


----------



## bookem (Sep 8, 2006)

Hi, thanks for the reply.  Wnat I am trying to do is make an incremental backup with rsync and hard links.  symlinks won't work for this I think.

I think the pax command may do what I need, but can't seem to make it work.  

from the man page:

-r -w   Copy.  Copy the file operands to the destination directory.  When
             no file operands are specified, a list of files to copy with one
             per line is read from the standard input.  When a file operand is
             also a directory the entire file hierarchy rooted at that direc-
             tory will be included.  The effect of the copy is as if the
             copied files were written to an archive file and then subse-
             quently extracted, except that there may be hard links between
             the original and the copied files (see the -l option below).

             Warning: The destination directory must not be one of the file
             operands or a member of a file hierarchy rooted at one of the
             file operands.  The result of a copy under these conditions is
             unpredictable.


----------



## macbri (Sep 11, 2006)

rsync already does this with the '-H' option, with the caveats as explained in the man page entry:



> -H, --hard-links
> 
> This  tells rsync to recreate hard  links  on the  remote system to  be the same as the local system. Without  this  option  hard links are treated like regular files.
> 
> ...


----------



## artov (Sep 21, 2006)

I have never used (actually I think one program setup used hardlinks for source
files) hardlinks as file copy, but it seems that on Linux (OpenSUSE 10.2 Alpha 4
on x86) 

$ mkdir a
$ cd a
$ touch 1 2 3
$ mkdir ../b
$ cp -al 1 2 ../b

does same as on OSX 

$ mkdir a
$ cd a
$ touch 1 2 3 
$ cat > list
1
2
Ctrl-D
$ cpio -p -al ../b < list

On both cases, ls -ls shows that files 1 and 2 has two hardlinks on them.


----------



## macbri (Sep 21, 2006)

artov said:


> I have never used ...[snip]... hardlinks as file copy



Just in case anyone stumbles across this thread isn't familiar with hard links, I would stress that a hardlink is just a second reference to the same file and *isn't* a copy.  If you delete the original, all well and good, the reference count is decremented but your file still exists. But what if your original gets corrupted?  Let's say we create our important file and make a hard link to it:


```
% echo "My Precious File Contents" > original.txt
% cp -al original.txt copy.txt
% cat copy.txt
[I]My Precious File Contents[/I]
```

So far so good.  But if we change to the original, or something else changes it or corrupts it:


```
% echo "This file is corrupted" >> original.txt
% [B]cat copy.txt[/B]
[I]My Precious File Contents
This file is corrupted[/I]
```

So I wouldn't rely on this for a backup or copy of any kind (never mind that hard links must exist on the same filesystem as the originals, which makes sense if you're only going to have one physical copy of the data....)

I just wanted to add that for clarity.


----------



## artov (Sep 22, 2006)

macbri said:


> J
> So I wouldn't rely on this for a backup or copy of any kind (never mind that hard links must exist on the same filesystem as the originals, which makes sense if you're only going to have one physical copy of the data....)
> 
> I just wanted to add that for clarity.



Yes, I know that. About that program I mentioned. It had program sources
on one directory hierarchy. The installation supported several machines, so
instead of copying the files, it created the hard links. That way the compiler
could generate binaries where it wanted (the installation supported also
compilers that cannot generate binaries in different directory), but the original
source directory remained clean.


----------

