how to get the current working directory's absolute path from irb
Solution 1
Dir.pwd
seems to do the trick.
http://ruby-doc.org/core/Dir.html#method-c-pwd
Solution 2
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)
Solution 3
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__)
Solution 4
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"
Solution 5
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"
Related videos on Youtube
Dexygen
"Knowledge is of two kinds. We know a subject ourselves, or we know where we can find information upon it." -- Samuel Johnson Full-stack/front-end web developer since 1999. Strong with the command line, design patterns, SQL and regular expressions. Debugged Javascript parseInt/radix issue in 2000. PHP experience on-and-off since 2001, Java since 2002. Created one-file, 60-line "bare bones mvc" PHP framework in 2007, downloaded several thousand times before being migrated off code.google.com to github, now as "JackRabbitMvc" Created "DeskML" platform around 2010/2011, a rough equivalent of Adobe AIR (and now Node-Webkit) using a TCL-based platform and just 100 lines of glue code. Speaker at technical user meetings Writer of pre-publication technical reviews Focused primarily on the front end since 2009 Participation in interview of other potential software developers Discovered way of emulating various frameworks' document/ready functionality by simply using setTimeout/0. Five years' experience with ExtJS, and more recently some Backbone, Angular 1, and Angular 2 (and, of course, jQuery) Working with Vue.js as of 2017
Updated on June 26, 2020Comments
-
Dexygen almost 4 years
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
-
amenthes about 9 yearsThe 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 isFile.dirname(__FILE__)
)? Imagine calling a script from anywhere else (likeruby testdirectory/testscript.rb
) here, the two will be different! -
Dexygen almost 9 years@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?
-
amenthes almost 9 yearsit'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 isDir.pwd
). -
Dexygen almost 9 years@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"
-
Ken Williams almost 3 yearsThe 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".
-
-
Barry Kelly about 10 years
Dir.pwd
is equivalent topwd -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). -
awenkhh almost 10 yearsplease take also a look to the often forgotten Pathname class: ruby-doc.org/stdlib-2.1.1/libdoc/pathname/rdoc/Pathname.html
-
Brandon over 9 yearsThere is a problem, Dir.pwd will print the working directory of where the script is ran - which may not be what you want.
-
Besi about 9 yearsPlease note that the working directory must not be the same as the actual file. So
Dir.pwd
and your suggestion might potentially differ. -
Phrogz over 8 years@Zequez Because
__FILE__
is a constant but__dir__
is a method. -
aldrien.h almost 8 yearsthis will print the working directory of where the script is ran as @Brandon said.
-
hiveer about 6 yearsYes, assuming that you run command
bundle exec rspec spec
in directory '/project', while in file 'spec/spec_helper.rb', the value ofDir.pwd
will still be '/project'. -
GDP2 about 4 yearsNote: 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. -
Dexygen over 3 years@GDP2 It does EXACTLY what I asked: "get the current working directory's absolute path".
-
GDP2 over 3 years@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.