[sf-lug] question about cp -al

Michael Paoli Michael.Paoli at cal.berkeley.edu
Sun Sep 18 22:41:05 PDT 2016


> From: "Alex Kleider" <akleider at sonic.net>
> Subject: [sf-lug] question about cp -al
> Date: Sun, 18 Sep 2016 16:31:19 -0700

> I'm curious as to what exactly the cp -al command does with regard to
> directories.
> Rather than make another copy of each regular file, which it would do
> if not for the -l option, hard links are created instead.  What
> puzzles me is how are directories 'copied?'  I assume that they are
> copied in the same way that would happen without the -l option.  Is
> that correct?

Let's see ...
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html
... those options don't look standard, well, let's see ... guessing here
we're talkin' Linux, probably GNU cp ...
https://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation

...
`-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
...
`-l'
`--link'
Make hard links instead of copies of non-directories.
...

Directories are "just" another type of file - of type directory.
But hardlinking directories is generally a no no (Unix allows it, but
it's strongly discouraged, Linux doesn't let it happen - the kernel
won't let one do it).  Anyway, that way, madness lies ;-), hence it's
discouraged.  And yes, I've hard linked directories before ... but not
in a long time - really no need or reason to do that anymore, and it was
pretty much always a bad idea to begin with (but made for some cool
tests/demos, to see how well one *really* understood Unix filesystems).

So, yeah, hard link wouldn't apply to directory ... certainly not from
GNU cp -l option in the land of Linux.

> In other words: could the cp -al command be replaced by a routine
> that follows the source directory structure and with in a destination
> directory creates (mkdir) directories and populates them using the ln
> command?  Would the result be the same?

Probably same or similar, yes.  Let's see ...
$ find s -exec ls -dino \{\} \;
213496530 drwxr-xr-x 3 1003 140 Sep 18 20:00 s
213498082 -rw-r--r-- 2 1003 0 Sep 18 20:00 s/hard_link
213498082 -rw-r--r-- 2 1003 0 Sep 18 20:00 s/l
213496560 lrwxrwxrwx 1 1003 1 Sep 18 19:59 s/ln2d -> d
213497320 -rw-r--r-- 1 1003 0 Sep 18 19:59 s/f
213497304 drwxr-xr-x 2 1003 40 Sep 18 19:59 s/d
$ cp -al s t
$ find [st] -exec ls -dino \{\} \;
213496530 drwxr-xr-x 3 1003 140 Sep 18 20:00 s
213498082 -rw-r--r-- 4 1003 0 Sep 18 20:00 s/hard_link
213498082 -rw-r--r-- 4 1003 0 Sep 18 20:00 s/l
213496560 lrwxrwxrwx 2 1003 1 Sep 18 19:59 s/ln2d -> d
213497320 -rw-r--r-- 2 1003 0 Sep 18 19:59 s/f
213497304 drwxr-xr-x 2 1003 40 Sep 18 19:59 s/d
213498964 drwxr-xr-x 3 1003 140 Sep 18 20:00 t
213498082 -rw-r--r-- 4 1003 0 Sep 18 20:00 t/l
213498082 -rw-r--r-- 4 1003 0 Sep 18 20:00 t/hard_link
213497320 -rw-r--r-- 2 1003 0 Sep 18 19:59 t/f
213498965 drwxr-xr-x 2 1003 40 Sep 18 19:59 t/d
213496560 lrwxrwxrwx 2 1003 1 Sep 18 19:59 t/ln2d -> d
$ rm -rf t
$ ed a_routine
a_routine: No such file or directory
0a
mkdir t && cd s && find . -depth -print0 | pax -rw -0dl ../t
.
w
61
q
$ chmod u+x a_routine
$ ./a_routine
$ find [st] -exec ls -dino \{\} \;
213496530 drwxr-xr-x 3 1003 140 Sep 18 20:00 s
213498082 -rw-r--r-- 4 1003 0 Sep 18 20:00 s/hard_link
213498082 -rw-r--r-- 4 1003 0 Sep 18 20:00 s/l
213496560 lrwxrwxrwx 2 1003 1 Sep 18 19:59 s/ln2d -> d
213497320 -rw-r--r-- 2 1003 0 Sep 18 19:59 s/f
213497304 drwxr-xr-x 2 1003 40 Sep 18 19:59 s/d
213496785 drwxr-xr-x 3 1003 140 Sep 18 20:00 t
213496786 drwxr-xr-x 2 1003 40 Sep 18 19:59 t/d
213497320 -rw-r--r-- 2 1003 0 Sep 18 19:59 t/f
213496560 lrwxrwxrwx 2 1003 1 Sep 18 19:59 t/ln2d -> d
213498082 -rw-r--r-- 4 1003 0 Sep 18 20:00 t/l
213498082 -rw-r--r-- 4 1003 0 Sep 18 20:00 t/hard_link
$

Yup, sure looks to me like we could have a_routine that does that.

And yes, the -0 option to find and pax is also a GNU extension.

> ps by way of explanation: I've written a little script for doing
> backups- it relies on the cp -la command but the -l option is not
> available on the MacOS so my goal is to create a "work around."

MacOS isn't Linux, it's Unix.  So in many cases GNU stuff commonly
found on Linux that's not specified by POSIX, etc., won't be found on
Unix - e.g. MacOS X.

Of course if you're doing hard links for "backup", you're not really
backing up the file data, but merely creating additional hard links -
so, if they become unlinked, you still have another link, but if the
contents is overwritten, there is no backup, there's just one file with
multiple hard links.





More information about the sf-lug mailing list