318

I'm running Ruby on Windows though I don't know if that should make a difference. All I want to do is get the current working directory's absolute path. Is this possible from irb? Apparently from a script it's possible using File.expand_path(__FILE__)

But from irb I tried the following and got a "Permission denied" error:

File.new(Dir.new(".").path).expand
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Dexygen
  • 12,287
  • 13
  • 80
  • 147
  • 12
    The question is not actually clear: Do you want a) the current working directory (which is `Dir.pwd`) or do you want the directory where the currently running script is located (which is `File.dirname(__FILE__)`)? Imagine calling a script from anywhere else (like `ruby testdirectory/testscript.rb`) here, the two will be different! – amenthes Mar 22 '15 at 21:13
  • 1
    @amenthes You claim my question is unclear and then ask "Do you want a) the current working directory ...." and my question states "All I want to do is get the *current working directory's* absolute path...". What's unclear? – Dexygen Jul 18 '15 at 23:57
  • 9
    it's unclear because of the sentence " Apparently from a script it's possible using `File.expand_path(__FILE__)`" - because `__FILE__`'s location is a different animal than current working dir (which is `Dir.pwd`). – amenthes Jul 19 '15 at 09:06
  • 2
    @amenthes I thought I did a pretty good job differentiating "from irb" which is right there in the title of the question (and twice within the question itself), from "from a script" – Dexygen Jul 19 '15 at 14:49
  • The reason the question is very unclear is that *even in a script*, `File.expand_path(__FILE__)` does not "get the current working directory's absolute path". – Ken Williams Jul 02 '21 at 19:07

6 Answers6

575

Dir.pwd is the current working directory

http://ruby-doc.org/core/Dir.html#method-c-pwd

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
user85509
  • 36,612
  • 7
  • 33
  • 26
  • 3
    Note: this does **not** return the location of the current file. In order to do that, see the answers below. This only returns the current working directory of the shell which is calling the script (just like `pwd`), which might be somewhere completely different than where the script file is located. – GDP2 Feb 18 '20 at 19:29
  • 2
    @GDP2 It does EXACTLY what I asked: "get the current working directory's absolute path". – Dexygen Oct 11 '20 at 17:45
  • @Dexygen That's fine for you. I had a different use case scenario and so I made a note for myself and anyone else who might face the same issue. As you can see from the upvotes on the answer below, there are almost 200 others (at least) who also had the same issue. – GDP2 Oct 11 '20 at 20:06
198

File.expand_path File.dirname(__FILE__) will return the directory relative to the file this command is called from.

But Dir.pwd returns the working directory (results identical to executing pwd in your terminal)

BeMathis
  • 194
  • 2
  • 11
Psylone
  • 2,788
  • 1
  • 18
  • 15
  • 3
    `Dir.pwd` is equivalent to `pwd -P`. `exec('pwd -L')` will get the equivalent of pwd in the terminal (pwd is normally a bash builtin, and doesn't resolve symbolic links). – Barry Kelly Jan 26 '14 at 03:27
  • 1
    please take also a look to the often forgotten Pathname class: http://www.ruby-doc.org/stdlib-2.1.1/libdoc/pathname/rdoc/Pathname.html – awenkhh Jun 27 '14 at 19:26
  • 1
    There is a problem, Dir.pwd will print the working directory of where the script is ran - which may not be what you want. – Brandon Oct 13 '14 at 20:24
  • Yes, assuming that you run command `bundle exec rspec spec` in directory '/project', while in file 'spec/spec_helper.rb', the value of `Dir.pwd` will still be '/project'. – hiveer Mar 27 '18 at 03:01
70

As for the path relative to the current executing script, since Ruby 2.0 you can also use

__dir__

So this is basically the same as

File.dirname(__FILE__)
José Andias
  • 1,894
  • 2
  • 29
  • 31
udo
  • 1,486
  • 13
  • 18
  • 16
    @Zequez Because [`__FILE__` is a constant but `__dir__` is a method](http://stackoverflow.com/a/15190725/405017). – Phrogz Jan 03 '16 at 16:50
  • this will print the working directory of where the script is ran as @Brandon said. – aldrien.h Jun 03 '16 at 03:17
6

This will give you the working directory of the current file.

File.dirname(__FILE__)

Example:

current_file: "/Users/nemrow/SITM/folder1/folder2/amazon.rb"

result: "/Users/nemrow/SITM/folder1/folder2"

  • 1
    Please note that the working directory must not be the same as the actual file. So `Dir.pwd` and your suggestion might potentially differ. – Besi Feb 02 '15 at 13:41
6

Through this you can get absolute path of any file located in any directory.

File.join(Dir.pwd,'some-dir','some-file-name')

This will return

=> "/User/abc/xyz/some-dir/some-file-name"
yeshwant singh
  • 144
  • 1
  • 5
5

If you want to get the full path of the directory of the current rb file:

File.expand_path('../', __FILE__)
Châu Hồng Lĩnh
  • 1,986
  • 1
  • 20
  • 23