# Net::FTP

**URL:** <https://www.ruby-forum.com/t/net-ftp/109669>\
**Category:** Ruby\
**Created:** [July 17, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/net-ftp/109669 "2007-07-17T18:48:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 17, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/net-ftp/109669/1 "2007-07-17T18:48:48Z")

</div>

Hey all,  
I’m having trouble with Net::FTP’s dir command. I’m looking to get a  
list of all the directories, but instead I get a list of every file  
IN every directory, and the permissions and timestamps as well.

Is there any way to fix this?  
What method SHOULD I use to get a list of all of the directories?

For Clarification…

ftp.dir(’\*/’)

# =\> Every file in every directory and all the file permissions to

accompany them.

Thanks,  
-------------------------------------------------------|  
~ Ari  
crap my sig won’t fit

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 17, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/net-ftp/109669/2 "2007-07-17T19:56:58Z")

</div>

On 7/17/07, Ari B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ftp.dir(‘\*/’)
> 
> # =\> Every file in every directory and all the file permissions to
> 
> accompany them.

Leave off the forward slash after the asterisk, and then with your list:

1. select out the ones starting with ‘d’ since those will be your  
directories
2. pull out the last token of each entry which will be the directory  
name.

You may or may not be concerned with linked directories, which will  
start with an ‘l’ instead.

hth,  
Todd

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 18, 2007, 2:11am UTC](https://www.ruby-forum.com/t/net-ftp/109669/3 "2007-07-18T02:11:56Z")

</div>

On Jul 17, 2007, at 1:54 PM, Todd B. wrote:

> directories  
> 2. pull out the last token of each entry which will be the  
> directory name.

Thanks! But is there a different command I can use to get just the  
names? I happen to know that where I’m working, what I first collect  
from my ftp.dir(’\*’) will be all directories. How can I use that to  
my advantage?

aRi  
--------------------------------------------|  
If you’re not living on the edge,  
then you’re just wasting space.

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 18, 2007, 3:01am UTC](https://www.ruby-forum.com/t/net-ftp/109669/4 "2007-07-18T03:01:03Z")

</div>

On 7/17/07, Ari B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks! But is there a different command I can use to get just the  
> names? I happen to know that where I’m working, what I first collect  
> from my ftp.dir(‘\*’) will be all directories. How can I use that to  
> my advantage?
> 
> aRi

Well there’s #nlst method (using openbsd site as an example):

require ‘net/ftp’  
names = nil  
Net::FTP.open(‘[ftp.openbsd.org](http://ftp.openbsd.org)’,‘anonymous’,‘blah’) do |ftp|  
ftp.chdir(‘/pub/OpenBSD/4.1’)  
names = ftp.nlst(‘\*’)  
end  
p names

That will give you the names of directories _and_ files _and_ links in  
the 4.1 directory.

If you want just the directories in a directory, you can do this:

require ‘net/ftp’  
dirs = []  
Net::FTP.open(‘[ftp.openbsd.org](http://ftp.openbsd.org)’, ‘anonymous’, ‘blah’) do |ftp|  
ftp.chdir(‘/pub/OpenBSD/4.1’)  
dirs = ftp.ls(‘\*’).select { |item| item[0,1]=~/^[l|d]/ }  
end  
dirs.map! { |item| item.split.last }  
p dirs

That will give you just the directory and link names in the 4.1  
directory (but not if they have spaces in the name).

Does that make sense at all?  
Todd

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [July 18, 2007, 4:15am UTC](https://www.ruby-forum.com/t/net-ftp/109669/5 "2007-07-18T04:15:12Z")

</div>

On Jul 17, 2007, at 8:59 PM, Todd B. wrote:

> end  
> dirs.map! { |item| item.split.last }  
> p dirs
> 
> That will give you just the directory and link names in the 4.1  
> directory (but not if they have spaces in the name).

mmmmmm yes it finally makes sense! But when you did your ls… Did  
you get the listing of every file IN every directory? Because thats  
what I get when I use dir.

aRi  
-------------------------------------------|  
Nietzsche is my copilot
