# Which Directory was my ruby script run from?

**URL:** <https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929>\
**Category:** Ruby\
**Created:** [January 20, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929 "2006-01-20T15:29:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [January 20, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929/1 "2006-01-20T15:29:21Z")

</div>

Hi All,

How do I find out which directory my ruby script was run from?

Thanks Matthew J.  
p.s Today I’m using Windows

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [January 20, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929/2 "2006-01-20T15:37:12Z")

</div>

On Jan 20, 2006, at 7:52 AM, Matthew J. wrote:

> Hi All,
> 
> How do I find out which directory my ruby script was run from?
> 
> Thanks Matthew J.  
> p.s Today I’m using Windows

Try:

File.dirname($PROGRAM\_NAME)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [January 20, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929/3 "2006-01-20T15:46:07Z")

</div>

Matthew J. wrote:

> Hi All,
> 
> How do I find out which directory my ruby script was run from?

Maybe Dir.getwd?

## ------------------------------------------------------------- Dir::getwd Dir.getwd =\> string Dir.pwd =\> string

```
 Returns the path to the current working directory of this process
 as a string.

    Dir.chdir("/tmp") #=> 0
    Dir.getwd #=> "/tmp"
```

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [January 20, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929/4 "2006-01-20T17:07:52Z")

</div>

Mike F. wrote:

> 

I use File.dirname($0).

Jamey C.

Confidentiality Notice: This email message, including any attachments,  
is for the sole use of the intended recipient(s) and may contain  
confidential and/or privileged information. If you are not the intended  
recipient(s), you are hereby notified that any dissemination,  
unauthorized review, use, disclosure or distribution of this email and  
any materials contained in any attachments is prohibited. If you receive  
this message in error, or are not the intended recipient(s), please  
immediately notify the sender by email and destroy all copies of the  
original message, including attachments.

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [January 24, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929/5 "2006-01-24T16:57:47Z")

</div>

Matthew J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve written a little script to test, and it looks like the  
> second reply by Mike is the only one that works. The other  
> two provide me with the location of the file, not the  
> directory where the file was run from.

Excellent! I love seeing posters get resourceful with the  
answers they receive! This makes me happy. 🙂

Yes, File.dirname and Dir.getwd do two very different things,  
which you discovered. There seems to have been a small  
misunderstanding.

File.dirname simply does string manipulation on its argument,  
whereas Dir.getwd actually gets the directory from which the  
current script ($0) was invoked… which is what you were asking  
for.

BTW, $0 and $PROGRAM\_NAME are aliases for the same variable.

> Thanks for your help.

Thank you for making the most of the help offered!

Cheers!  
Tim H.

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [January 20, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929/6 "2006-01-20T17:34:18Z")

</div>

I’ve written a little script to test, and it looks like the second reply  
by Mike is the only one that works. The other two provide me with the  
location of the file, not the directory where the file was run from.

My script is in c:\sparkdriver\bin and this is set in my path.

When I run the the testfile.rb script from the c:\Documents and Settings  
folder I get the following results:-

C:\Documents and Settings\>testfile.rb  
Physical Location of File c:\sparkdriver\bin

c:/sparkdriver/bin - File.dirname($PROGRAM\_NAME)  
C:/Documents and Settings - Dir.getwd  
c:/sparkdriver/bin - File.dirname($0)

---- testfile.rb

puts “Physical Location of File c:\sparkdriver\bin\n\n”

dir = File.dirname($PROGRAM\_NAME)  
puts dir + ’ - File.dirname($PROGRAM\_NAME)’

dir = Dir.getwd  
puts dir + ’ - Dir.getwd’

dir =File.dirname($0)  
puts dir + ’ - File.dirname($0)’

Thanks for your help.

Matthew J.

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [January 24, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929/7 "2006-01-24T17:00:51Z")

</div>

Tim H. wrote:

> BTW, $0 and $PROGRAM\_NAME are aliases for the same variable.

“ **FILE** ”, too, I guess

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [January 24, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929/8 "2006-01-24T19:24:30Z")

</div>

“Gene T.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Tim H. wrote:
> 
> > BTW, $0 and $PROGRAM\_NAME are aliases for the same variable.
> 
> “ **FILE** ”, too, I guess

No, **FILE** is the current file, while $0 is the script ruby was  
started with.

---

<div class="post-metadata">

**Author:** ![mjacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mjacobs](https://www.ruby-forum.com/u/mjacobs)\
**Post date:** [January 24, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/which-directory-was-my-ruby-script-run-from/52929/9 "2006-01-24T17:36:00Z")

</div>

Gene T. wrote:

> Tim H. wrote:
> 
> > BTW, $0 and $PROGRAM\_NAME are aliases for the same variable.
> 
> “ **FILE** ”, too, I guess

Nope. **FILE** is the name current source file, which can be different  
than $0 in different modules.

freebie:~ 839\> ruby aprog.rb  
$0: aprog.rb  
**FILE** : ./amodule.rb  
aprog $0: aprog.rb  
aprog **FILE** : aprog.rb  
freebie:~ 840\> cat aprog.rb  
#!/usr/local/bin/ruby  
require ‘amodule’

f = Foo.new

f.pr\_dollar0  
f.pr\_file

puts “aprog $0: #{$0}”  
puts “aprog **FILE** : #{ **FILE** }”

exit 0

**END**  
freebie:~ 841\> cat amodule.rb  
class Foo  
def pr\_file  
puts “ **FILE** : #{ **FILE** }”  
end  
def pr\_dollar0  
puts “$0: #{$0}”  
end  
end
