# Newbie question - how to get current directory into a variab

**URL:** <https://rubytalk.org/t/newbie-question-how-to-get-current-directory-into-a-variab/41411>\
**Category:** ruby-talk\
**Created:** [15 October 2007 23:53 UTC](https://rubytalk.org/t/newbie-question-how-to-get-current-directory-into-a-variab/41411 "2007-10-15T23:53:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Vanderhaden](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@Peter\_Vanderhaden](https://rubytalk.org/u/Peter_Vanderhaden)\
**Post date:** [15 October 2007 23:53 UTC](https://rubytalk.org/t/newbie-question-how-to-get-current-directory-into-a-variab/41411/1 "2007-10-15T23:53:23Z")

</div>

I'm running Ruby on Ubuntu Linux, and I need to be able to get the  
current directory that the program is running in into a variable in my  
program. Is there a simple way to do this? Also, is there a way to  
change directories within the program?  
Thanks....

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [16 October 2007 00:24 UTC](https://rubytalk.org/t/newbie-question-how-to-get-current-directory-into-a-variab/41411/2 "2007-10-16T00:24:55Z")

</div>

Peter Vanderhaden wrote:

> I'm running Ruby on Ubuntu Linux, and I need to be able to get the  
> current directory that the program is running in into a variable in my  
> program. Is there a simple way to do this? Also, is there a way to  
> change directories within the program?  
> Thanks....

Check out the Dir class, particularly Dir.pwd and Dir.chdir,

> **···**
>
> --  
> RMagick OS X Installer [[http://rubyforge.org/projects/rmagick/\](http://rubyforge.org/projects/rmagick/%5C)]  
> RMagick Hints & Tips [[http://rubyforge.org/forum/forum.php?forum\_id=1618\](http://rubyforge.org/forum/forum.php?forum_id=1618%5C)]  
> RMagick Installation FAQ [[http://rmagick.rubyforge.org/install-faq.html\](http://rmagick.rubyforge.org/install-faq.html%5C)]

---

<div class="post-metadata">

**Author:** ![Peter\_Vanderhaden](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@Peter\_Vanderhaden](https://rubytalk.org/u/Peter_Vanderhaden)\
**Post date:** [16 October 2007 00:36 UTC](https://rubytalk.org/t/newbie-question-how-to-get-current-directory-into-a-variab/41411/3 "2007-10-16T00:36:44Z")

</div>

Thanks Tim. the Dir.pwd is exactly what I need. I haven't figured out  
how to use the Dir.chdir yet, but that looks promising too.  
PV  
Tim Hunter wrote:

> **···**
>
> > Peter Vanderhaden wrote:
> > 
> > > I'm running Ruby on Ubuntu Linux, and I need to be able to get the  
> > > current directory that the program is running in into a variable in my  
> > > program. Is there a simple way to do this? Also, is there a way to  
> > > change directories within the program?  
> > > Thanks....
> > 
> > Check out the Dir class, particularly Dir.pwd and Dir.chdir,
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [16 October 2007 03:43 UTC](https://rubytalk.org/t/newbie-question-how-to-get-current-directory-into-a-variab/41411/4 "2007-10-16T03:43:22Z")

</div>

Peter Vanderhaden wrote:

> Thanks Tim. the Dir.pwd is exactly what I need. I haven't figured out  
> how to use the Dir.chdir yet, but that looks promising too.

p Dir.entries(".") #array containing files in current directory  
puts

Dir.chdir("../") #relative path  
p Dir.entries(".")  
puts

Dir.chdir("/Users/me/Documents/ruby") #absolute path  
p Dir.entries(".")

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Janus\_Bor](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@Janus\_Bor](https://rubytalk.org/u/Janus_Bor)\
**Post date:** [14 July 2008 12:07 UTC](https://rubytalk.org/t/newbie-question-how-to-get-current-directory-into-a-variab/41411/5 "2008-07-14T12:07:42Z")

</div>

Sorry for raising this thread from the dead!

7stud -- wrote:

> p Dir.entries(".") #array containing files in current directory  
> puts

Will this work on Windows, too? Or is the "." a Linux specific thing? If  
it doesn't work on Windows, how can I get the absolute path of the  
scipt? Thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Dave\_Bass](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@Dave\_Bass](https://rubytalk.org/u/Dave_Bass)\
**Post date:** [14 July 2008 14:44 UTC](https://rubytalk.org/t/newbie-question-how-to-get-current-directory-into-a-variab/41411/6 "2008-07-14T14:44:00Z")

</div>

Janus Bor wrote:

> > p Dir.entries(".") #array containing files in current directory  
> > puts
> 
> Will this work on Windows, too? Or is the "." a Linux specific thing?

Works for me (WinXP, Ruby 1.8.6-111).  
Both "." and ".." work fine on Windows.

Dave

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sean\_Willis](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@Sean\_Willis](https://rubytalk.org/u/Sean_Willis)\
**Post date:** [28 August 2008 20:34 UTC](https://rubytalk.org/t/newbie-question-how-to-get-current-directory-into-a-variab/41411/7 "2008-08-28T20:34:08Z")

</div>

Thought I'd keep this one going!

Using Dir.chdir() I have no problem changing to directories created by  
my rails application, however, I can't get it to change to a directory  
that I made. I tried adding "config.load\_paths += %W(  
#{RAILS\_ROOT}/public/my\_new\_folder" to my environment.rb file, but it  
didn't work. When should I be using config.load\_paths?

Any ideas? Thanks!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
