# Directory copying using hard links?

**URL:** <https://www.ruby-forum.com/t/directory-copying-using-hard-links/193677>\
**Category:** Ruby\
**Created:** [August 21, 2010, 9:28pm UTC](https://www.ruby-forum.com/t/directory-copying-using-hard-links/193677 "2010-08-21T21:28:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [August 21, 2010, 9:28pm UTC](https://www.ruby-forum.com/t/directory-copying-using-hard-links/193677/1 "2010-08-21T21:28:19Z")

</div>

Is there an “official” way of recursively copying directories so that  
individual files are hard-linked (if possible)? FileUtils has methods  
for creating single hard links (#ln) as well as for recursive copying  
(#cp\_r). What’s missing is the combination, essentially a call to

cp -al src dest

However, I’m wary of using this instead of FileUtils.cp\_r(src, dest) as  
the semantics are different. #cp\_r makes a copy with path dest of a  
directory with path src. In contrast, cp -a src dest copies src into(!)  
dest. Now, File.dirname(File.expand\_path(dest)) gives me the parent  
directory, but I’m simply not sure whether it works as intended in all  
cases.

Any suggestions?

Michael
