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,
Best Answer
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:If
someFolder
is a folder (i.e. a directory — and not a symbolic link to a directory) andotherFolder
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
. IfsomeFolder
is a directory butotherFolder
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
ifotherFolder
is an existing directory or a symbolic link to one, or to/Users/me/someFolder
otherwise (regular file, other special file, or non-existent).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 whetherotherFolder
existed or not. (Except ifotherFolder
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
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 tocp -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.