Copy an entire directory tree

4,107 views
Skip to first unread message

Jaybill McCarthy

unread,
Jun 5, 2012, 2:21:51 PM6/5/12
to golan...@googlegroups.com
I recently came across a situation where I needed to copy an entire directory tree from one place to another. I figure I'm not the only person who has run across this problem, so I thought I'd share my solution. 

Here's the blog post:


And here's a gist of the code, if you just want to get straight to that:


I hope someone finds this useful! Please feel free to reply with suggested improvements. 

--Jaybill McCarthy

minux

unread,
Jun 5, 2012, 2:27:58 PM6/5/12
to Jaybill McCarthy, golan...@googlegroups.com
you just re-invented a specific case of path/filepath.Walk

Jaybill McCarthy

unread,
Jun 5, 2012, 2:38:57 PM6/5/12
to golan...@googlegroups.com, Jaybill McCarthy
Wow, yeah. I totally did. I searched the package documentation before writing this and somehow managed to miss an entire library devoted to doing what I was trying to do. Dang it!

Can you tell that I'm relatively new to Go yet? ;)

--Jaybill

On Tuesday, June 5, 2012 11:27:58 AM UTC-7, minux wrote:

Dmitry Chestnykh

unread,
Jun 5, 2012, 3:07:44 PM6/5/12
to golan...@googlegroups.com
Don't forget that with just io.Copy and os.Chmod you're not copying extended attributes and other crap (metadata / ACL) that's attached to files.

For example, there's a copyfile() function in Mac OS X's libc that copies everything:
(I think it's practically impossible to copy files exactly this without using it.)

Also, your code follows symlinks instead of copying them as is.

Just a reminder, not a critique.

-Dmitry

Jaybill McCarthy

unread,
Jun 5, 2012, 3:32:18 PM6/5/12
to golan...@googlegroups.com
That's a really good point. In my particular use case, I don't actually need the rest of the metadata, but that's an excellent point about symlinks. Thanks!
Reply all
Reply to author
Forward
0 new messages