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

**URL:** <https://www.ruby-forum.com/t/newbie-question-how-to-get-current-directory-into-a-variab/117337>\
**Category:** Ruby\
**Created:** [October 16, 2007, 1:53am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-get-current-directory-into-a-variab/117337 "2007-10-16T01:53:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [October 16, 2007, 1:53am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-get-current-directory-into-a-variab/117337/1 "2007-10-16T01:53:21Z")

</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…

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [October 16, 2007, 2:25am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-get-current-directory-into-a-variab/117337/2 "2007-10-16T02:25:38Z")

</div>

Peter V. 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,

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [October 16, 2007, 5:43am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-get-current-directory-into-a-variab/117337/3 "2007-10-16T05:43:18Z")

</div>

Peter V. 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(".")

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [July 14, 2008, 2:12pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-get-current-directory-into-a-variab/117337/4 "2008-07-14T14:12:14Z")

</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!

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [July 14, 2008, 4:48pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-get-current-directory-into-a-variab/117337/5 "2008-07-14T16:48:32Z")

</div>

Janus B. 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

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [August 28, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-get-current-directory-into-a-variab/117337/6 "2008-08-28T22:38:53Z")

</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!

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [October 16, 2007, 2:36am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-get-current-directory-into-a-variab/117337/7 "2007-10-16T02:36:42Z")

</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 H. wrote:

> Peter V. 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,
