Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error copying directories at command line using cp

Mac OS X Yosemite v.10.10.5.

I am trying to use the cp command to copy one Git directory to another.

This command-line statement:
cp -r /path/to/dir/from/ /path/to/dir/to/
Returns this error:
cp: /path/to/dir/to/.git/objects/00/00ad2afeb304e18870d4509efc89fedcb3f128: Permission denied

This error is returned one time each for (what I believe, but haven't verified, is) every file in the directory.

The first time I ran the command it worked properly, as expected, without error. But, without making any changes to any files, the second (and subsequent) times I ran the command, I got the error.

What's going on? And how can I fix this?

Edit:

In response to a question in the comment:

What does ls -l /path/to/dir/to/.git/objects/00/00ad2afeb304e18870d4509efc89fedcb3f128 show?

The answer is it shows:

-r--r--r-- 1 myusername staff 6151 May 6 00:45 /path/to/dir/to/.git/objects/00/00ad2afeb304e18870d4509efc89fedcb3f128
like image 853
Let Me Tink About It Avatar asked Sep 15 '25 16:09

Let Me Tink About It


1 Answers

The reason you are getting Permission Denied is because you are trying to overwrite a file that already exists in the destination directory that has read only permissions set on it. Since it appears you're trying to overwrite it you could just remove the destination directory if it exists before the copy operation. Also you should use -R, not -r ...

Historic versions of the cp utility had a -r option. This implementation supports that option; however, its use is strongly discouraged, as it does not correctly copy special files, symbolic links, or fifo's.

Using a command such as this should resolve your issue:

[[ ! -d dest ]] || rm -rf dest ; cp -R src dest

The above checks if dest exists; if it does recursively remove it, then copy the source to dest,

like image 154
l'L'l Avatar answered Sep 17 '25 08:09

l'L'l