could someone give me a hint, howto serve the current directory from command line with ruby? it would be great, if i can have some system wide configuration (e.g. mime-types) and simply launch it from every directory.
The CWD (Current Working Directory) is a path (of a directory) inside the file system, where the shell is currently working. The current working directory is essential for resolving relative paths. Cd is a generic command found in the Command Interpreter of most operating systems.
Print Current Working Directory ( pwd ) To print the name of the current working directory, use the command pwd . As this is the first command that you have executed in Bash in this session, the result of the pwd is the full path to your home directory.
To print the current working directory, we use the pwd command in the Linux system. pwd (print working directory) – The pwd command is used to display the name of the current working directory in the Linux system using the terminal.
In a Windows command prompt, chdir or cd will print the full path of the current working directory in the console.
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.
Simplest way possible (thanks Aaron Patterson/n0kada):
ruby -run -e httpd . -p 9090
Alternate, more complex way:
ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 9090, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"
Even the first command is hard to remember, so I just have this in my .bashrc
:
function serve { port="${1:-3000}" ruby -run -e httpd . -p $port }
It serves the current directory on port 3000 by default, but you can also specify the port:
~ $ cd tmp ~/tmp $ serve # ~/tmp served on port 3000 ~/tmp $ cd ../www ~/www $ serve 5000 # ~/www served on port 5000
As Aaron Patterson tweeted it out today you can do:
ruby -run -e httpd . -p 5000
And you can set the bind address as well by adding -b 127.0.0.1
Works with Ruby 1.9.2 and greater.
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