vendredi 6 mars 2015

Copy entire contents of directory including hidden and symlinks


I've looked around and I haven't found a clear answer for how to do this correctly and I've spent way too much time trying to figure it out.


I have actually figured out how to make it work but since it was such a chore, this question is more of a documentation of the problem and solution than a request for assistance, but I'd still be interested to know the correct way to accomplish this. It seems that it should be simpler.


Problem:


Trying to copy the entire content of one directory to another which includes hidden files/directories and symlinks.


This is fairly straightforward cp -a ${src} ${dst}


The problem is that it fails to copy the symlinks if the link target comes later in the directory listing.


Considering that the original link targets work, are device local and I'm copying on the same device, what is the correct way to make sure that the entire directory structure is copied exactly?


It took me quite a while but I figured out that all I actually need to do is run the copy command twice and the second pass adds the missing links. Is there a better way than this?


Working but Hack-ish Solution:



src=".default/.";
dst="tld.domain/";
cp -a ${src} ${dst};
cp -a ${src} ${dst};


I shouldn't need to double up the command to ensure that everything copied correctly.


I tried tar as well, same issue. I didn't try rsync but both seem like an overkill solution for something that should be a pretty trivial thing.


Scenario:


For additional perspective in case it matters or I haven't be clear.


I have created a template directory structure for website domains. After creating a new sub-domain on my web-host, I copy the template over to the sub-domain's document root.


This is my subdomain and template (.default/) directory structure:



sites/
.default/
.htaccess
.www -> production/
production/
tld.domain.x/
tld.domain.y/
tld.domain.z/


So I create a new subdomain and then populate it's document root with the contents of the template directory. The issue here is that .www/ is a symlink to production/ but cp et al create everything in sorted order so it tries to create .www/ before creating production/ and consequently fails.


My web-host is CentOS6.



Aucun commentaire:

Enregistrer un commentaire