# Net::SFTP, copying directory recursively

**URL:** <https://rubytalk.org/t/net-sftp-copying-directory-recursively/37640>\
**Category:** ruby-talk\
**Created:** [16 May 2007 16:48 UTC](https://rubytalk.org/t/net-sftp-copying-directory-recursively/37640 "2007-05-16T16:48:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manish\_Sapariya](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Manish\_Sapariya](https://rubytalk.org/u/Manish_Sapariya)\
**Post date:** [16 May 2007 16:48 UTC](https://rubytalk.org/t/net-sftp-copying-directory-recursively/37640/1 "2007-05-16T16:48:34Z")

</div>

Hi,  
I am trying to copy a remote directroy recursively using Net::sftp.

I could not find any direct api given by this module and decided to  
copy the entire directory file by file.

However i am not able to figure out, how to tell between file or directory.

I am trying to follow simple method,

for every file in directory  
if file is direcory  
&nbsp;&nbsp;create directory  
else  
&nbsp;&nbsp;get/put directory  
end

However I am not able to find out any method to figure out the type of  
file on remote machine.

Any ideas?  
Thanks and Regards,  
Manish

---

<div class="post-metadata">

**Author:** ![Andy\_Koch](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Andy\_Koch](https://rubytalk.org/u/Andy_Koch)\
**Post date:** [25 June 2007 20:06 UTC](https://rubytalk.org/t/net-sftp-copying-directory-recursively/37640/2 "2007-06-25T20:06:44Z")

</div>

Manish Sapariya schrieb:

> Hi,  
> I am trying to copy a remote directroy recursively using Net::sftp.
> 
> I could not find any direct api given by this module and decided to  
> copy the entire directory file by file.
> 
> However i am not able to figure out, how to tell between file or directory.
> 
> I am trying to follow simple method,
> 
> for every file in directory  
> if file is direcory  
> create directory  
> else  
> get/put directory  
> end
> 
> However I am not able to find out any method to figure out the type of  
> file on remote machine.
> 
> Any ideas?  
> Thanks and Regards,  
> Manish

I just had this same problem, while I didn't solve your precise issue; the following code will recursively download the contents of a folder.

It's rather crude, if anyone has a refactor suggestion I'm quite open to hear it.

> **···**
>
> #################################################  
> require 'rubygems'  
> require 'net/sftp'
> 
> def open\_or\_get\_all(sftp, open\_dir, local\_dir)  
> &nbsp;&nbsp;&nbsp;handle = sftp.opendir(open\_dir)  
> &nbsp;&nbsp;&nbsp;items = sftp.readdir(handle)  
> &nbsp;&nbsp;&nbsp;items.each do |item|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if item.filename != '.' && item.filename != '..'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if item.longname[0...1] == 'd'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# mkdir locally  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.mkdir(local\_dir + item.filename, 0777)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# open dir and download all  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;open\_or\_get\_all(sftp, open\_dir + item.filename + '/', local\_dir + item.filename + '/')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#puts local\_dir+item.filename  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#puts open\_dir+item.filename  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sftp.get\_file open\_dir+item.filename, local\_dir+item.filename  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;sftp.close\_handle(handle)  
> end
> 
> Net::SFTP.start('ext\_server', 'user\_name', 'password') do |sftp|  
> &nbsp;&nbsp;&nbsp;open\_or\_get\_all(sftp, "/home/user\_name/dir\_to\_dnld/", "/lcl\_dir/")  
> end
> 
> #################################################
> 
> maybe that will save someone else a few minutes...
