1

I'm trying to clone a directory which holds a git repo.

i.e., I have /Users/me/someFolder - someFolder has ALOT of files, like over 250k. It's also a git repo, with a .git directory, and many other hidden files.

Additionally it contains a number of other sub-directories (folders).

So, to clone this, preserving all permissions, copying recursively, and including ALL files/directories in ZSH on Big Sur, do I simply do:

cp -a /Users/me/someFolder /Users/me/otherFolder?

I ask as I've seen other syntax such as:

cp -a /Users/me/someFolder/. /Users/me/otherFolder? (note the .).

Thanks,

1
  • 1
    What's the difference between Zsh and Bash, or any other shell in this particular case ? Both cp and rsync are not a shell builtin in either of the shells. Both of them are commands.
    – Rakib Fiha
    Commented Jan 7, 2021 at 8:13

2 Answers 2

2

The second method also works if otherFolder already exists. Other than that, there is no difference (assuming that /Users/me/someFolder is an existing folder). In more detail:

cp -a /Users/me/someFolder /Users/me/otherFolder

If someFolder is a folder (i.e. a directory — and not a symbolic link to a directory) and otherFolder already exists (and is a directory or a symbolic link to one), this copies /Users/me/someFolder/some/file to /Users/me/otherFolder/someFolder/some/file. If someFolder is a directory but otherFolder doesn't exist, this copies /Users/me/someFolder/some/file to /Users/me/otherFolder/some/file.

If someFolder exists but is not a directory (it's a regular file, a symbolic link, etc.), it is copied to either /Users/me/otherFolder/someFolder if otherFolder is an existing directory or a symbolic link to one, or to /Users/me/someFolder otherwise (regular file, other special file, or non-existent).

cp -a /Users/me/someFolder/. /Users/me/otherFolder

If someFolder is a directory or a symbolic link to one, this always copies /Users/me/someFolder/some/file to /Users/me/otherFolder/some/file, regardless of whether otherFolder existed or not. (Except if otherFolder is an existing file that isn't a symbolic link or a file, in which case the command will fail.)

In all cases, if the thing to copy is a directory, all of its contents are copied recursively, preserving permissions and modification times. (Access times are also preserved, but they're updated in the source.) That comes from the -a option.

An equivalent command is

rsync -a /Users/me/someFolder/ /Users/me/otherFolder

Note the trailing / on the source so that /Users/me/someFolder/some/file is copied to /Users/me/otherFolder/some/file. If the source was /Users/me/someFolder, it would be copied to /Users/me/otherFolder/someFolder.

rsync is equivalent to cp -R in simple cases, but it's cleverer about not copying files that are already present in the destination folder, so it's good for resuming an interrupted copy or for doing incremental backups. Rsync also has a lot of options to do things like select which files to copy.

0

The first option will create something like /Users/me/otherFolder/someFolder, while the second option will leave /Users/me/otherFolder with the contents of someFolder, but not with otherFolder as a subfolder.

1
  • Hi, I think you've misread, the first option doesn't do that. It seems to clone correctly, despite the internet suggesting the syntax is incomplete, in that it should have the extra . of the second command I showed.
    – Woodstock
    Commented Jan 6, 2021 at 17:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .