# Determining the directory a script is running in

**URL:** <https://www.ruby-forum.com/t/determining-the-directory-a-script-is-running-in/48449>\
**Category:** Ruby\
**Created:** [November 17, 2005, 7:49am UTC](https://www.ruby-forum.com/t/determining-the-directory-a-script-is-running-in/48449 "2005-11-17T07:49:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin.jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85f322/32.png) [@kevin.jackson](https://www.ruby-forum.com/u/kevin.jackson)\
**Post date:** [November 17, 2005, 7:49am UTC](https://www.ruby-forum.com/t/determining-the-directory-a-script-is-running-in/48449/1 "2005-11-17T07:49:41Z")

</div>

How can I find the directory that the current script is running from?

I need to run a script and (without any arguments), assign a variable  
based on the directory that the script is being executed from, ie

c:\my\_dir\> ruby script.rb

-\> @dir = c:\my\_dir

or in unix

/home/kj/ruby-projects/ruby script.rb

-\> @dir = /home/kj/ruby-projects/

I can’t find what I’m looking for in the pickaxe, can anyone help out?

Kev

---

<div class="post-metadata">

**Author:** ![kevin.jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85f322/32.png) [@kevin.jackson](https://www.ruby-forum.com/u/kevin.jackson)\
**Post date:** [November 17, 2005, 7:58am UTC](https://www.ruby-forum.com/t/determining-the-directory-a-script-is-running-in/48449/2 "2005-11-17T07:58:43Z")

</div>

Sorry, I discovered that File.expand\_path(".") does work after all -  
strange I thought I’d tried it and got an error, perhaps it was  
something else

Kev

---

<div class="post-metadata">

**Author:** ![kevin.jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85f322/32.png) [@kevin.jackson](https://www.ruby-forum.com/u/kevin.jackson)\
**Post date:** [November 17, 2005, 9:23am UTC](https://www.ruby-forum.com/t/determining-the-directory-a-script-is-running-in/48449/3 "2005-11-17T09:23:05Z")

</div>

On 11/17/05, Kev J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sorry, I discovered that File.expand\_path(“.”) does work after all -  
> strange I thought I’d tried it and got an error, perhaps it was  
> something else

You may also find Dir.getwd/Dir.pwd useful.

Sam

---

<div class="post-metadata">

**Author:** ![kevin.jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/85f322/32.png) [@kevin.jackson](https://www.ruby-forum.com/u/kevin.jackson)\
**Post date:** [November 17, 2005, 9:32am UTC](https://www.ruby-forum.com/t/determining-the-directory-a-script-is-running-in/48449/4 "2005-11-17T09:32:07Z")

</div>

On Thursday 17 November 2005 07:48, Kev J. wrote:

> or in unix
> 
> /home/kj/ruby-projects/ruby script.rb
> 
> -\> @dir = /home/kj/ruby-projects/
> 
> I can’t find what I’m looking for in the pickaxe, can anyone help  
> out?

Dir.pwd is what you are looking for.

$ cd /home/stefan/tmp  
$ irb  
irb(main):001:0\> Dir.pwd  
=\> “/home/stefan/tmp”
