On Thu, Feb 26, 2026 at 10:49:12 -0500, [email protected] wrote:
> Further aside: I also tried to do some more googling (ddg'ing) including
> looking for cp on Greg's Wiki, but my google foo wasn't good enough to really
> get to the point (either on Greg's wiki or anywhere else).
I don't really cover the basics that you'd get in a man page (or info page)
for the core Unix commands. Most of the pages on that wiki assume some
familiarity with these commands.
Both "cp -a" and "rsync -a" are useful for copying whole hierarchies.
Additional options may be desired in some corner cases (ACLs??), but
you can dig those out of the man pages if you need them.
> To me there are two different situations, and I'll try to explain them by
> examples:
>
> Example 1: sometimes I want to copy an entire directory tree (i.e., with
> subdirectories) to a different location but (rooted) at the same level as the
> directory mount point:
>
> source directory tree: /rhk
> destination directory mount point: /back/rhk
> desired destination directory tree path: /back/rhk (and not /back/rhk/rhk)
>
> In this case I would expect to use a cp command as follows:
>
> cp -aux /rhk/* /back/rhk/
The * on the source argument is problematic, because it won't match
"dot files". Also, it'll lead to "too many arguments" in some cases.
So, ideally you want to specify a directory (not a glob pattern) as
your source.
Assuming a one-time interactive copy, what I would normally do would be:
cd /rhk
rsync -a . /back/rhk/ # or cp -a
As this would be interactive, I would look for an error message on the
cd command. If the cd fails, obviously, I would not type the second
command.
If you're scripting this, then you would check the exit status of cd
(using cd || exit, or simply cd && rsync, for example).
> Example 2: other times I want to copy an entire directory tree (i.e., with
> subdirectories) to a different location but (rooted) one level below the
> directory mount point:
>
> source directory tree: /rhk
> destination directory mount point: /back
> desired destination directory tree path: /back/rhk
This really isn't any different from the first example, right? As far
as cp or rsync are concerned, it doesn't matter whether the destination
directory is a mount point.
Or are you saying that the /back/rhk directory doesn't exist yet, and
you want cp or rsync to create it? Let's assume that one.
cd /
rsync -a rhk /back/
That's what I would use to create the /back/rhk directory during the copy.
The obvious alternative would be:
mkdir /back/rhk
cd /rhk
rsync -a . /back/rhk/
> Also, there might be a better command than cp to do this, but I'd like to
> understand how to do it with the cp command. (Aside: there is no need for
> rsync (although it could be used) because the backup disk will be mounted on
> the computer.)
Rsync isn't just for remote copying. It still saves a bunch of time
and wear on locally mounted directories as well, since it'll skip the
content that has already been copied.
In any case, cp -a can be substituted for rsync -a in all of my examples.