The pwd command displays the full, absolute path of the current, or working, directory. It's not something you'll use all the time, but it can be incredibly handy when you get a bit discombobulated.
The pwd command can be used to determine the present working directory. and the cd command can be used to change the current working directory.
The current working directory is the directory in which the user is currently working in. Each time you interact with your command prompt, you are working within a directory. By default, when you log into your Linux system, your current working directory is set to your home directory.
Pass a string to File. expand_path to generate the path to that file or directory. Relative paths will reference your current working directory, and paths prepended with ~ will use the owner's home directory.
Dir.pwd
seems to do the trick.
http://ruby-doc.org/core/Dir.html#method-c-pwd
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)
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__)
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"
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"
If you want to get the full path of the directory of the current rb file:
File.expand_path('../', __FILE__)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With