Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serve current directory from command line

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.

like image 421
Sebastian Avatar asked Jun 24 '10 08:06

Sebastian


People also ask

What is current directory in CMD?

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.

How do I get the current directory in terminal?

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.

How do I get the current directory in Linux?

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.

How do I print a directory in command prompt?

In a Windows command prompt, chdir or cd will print the full path of the current working directory in the console.

How do I find the current working directory in Windows?

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.


2 Answers

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 
like image 141
Daniel Perez Alvarez Avatar answered Nov 15 '22 23:11

Daniel Perez Alvarez


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.

like image 23
KARASZI István Avatar answered Nov 15 '22 22:11

KARASZI István