If you have Python installed, it is possible to run a web server from a directory in your system with just one simple command in terminal.
You can check if python is installed by typing this in terminal:
python --version
If python is installed, use cd to navigate to the folder from which you want run the server.
For example for a folder named “project” on my desktop, I would use:
cd /Users/my_user_name/Desktop/project
Then type this to run the server:
python -m SimpleHTTPServer
By default an HTTP server will be started on port 8000.
Open your browser and navigate to one of these links
http://localhost:8000/
http://127.0.0.1:8000/
If the folder contains an index.html file, this will be open in the browser, otherwise you’ll see a list of files and folders contained in the folder from which you are running the server.
If you want to run a server on a different port, specify the port number after the command.
python -m SimpleHTTPServer 8080
To stop the server press CTRL + C in terminal.
We can create a Bash function to simplify the way we run this command.
Open .bash_profile either in a text editor or in Terminal. Usually this file is hidden inside Macintosh HD/Users/{name of user}. It can also be opened in the default text editor using Terminal and the following command.
open ~/.bash_profile
Add following function at the end of the .bash_profile, and save the file.
function server() { local port="${1:-8000}"; python -m SimpleHTTPServer "$port"; }
Now it is possible to start a server from a directory by typing server in terminal, or server 8080 to run on port 8080.
If a .bash_profile file does not exist, you can create one with the command:
touch .bash_profile
If you have no permission to edit the .bash_profile, you might need to change ownership of the file, and assign it to yourself.
chown your_user_name ~/.bash_profile
We can also add the functionality to start the browser and navigate to http://localhost by changing the function like this:
function server() { local port="${1:-8000}"; open "http://localhost:${port}/"; python -m SimpleHTTPServer "$port"; }
If you are still in the same terminal session, you’ll need to reload the .bash_profile file in order for the changes to take effect.
Type:
source ~/.bash_profile
Next time you type server in terminal, a web server will run from the directory, and the browser will open showing the address http://localhost:8000.
The $PATH, or Shell Path in OSX allows the user to specify locations in the system where it is possible to use commands directly, rather than specifying the full path to those commands.
For example in the case of node.js, where the environment is installed in /usr/local/bin, it would be possible to use
node
instead of
/usr/local/bin/node
The locations for a command or program are searched in the order they are specified.
Follow these steps to add one location to $PATH
1) Check which location is in the $PATH by opening Terminal and typing
echo $PATH
Terminal will show the locations that are already specified in the $PATH
2) Open .bash_profile either in a text editor or in Terminal. Usually this file is hidden inside Macintosh HD/Users/{name of user}. It can also be opened in the default text editor using Terminal and the following command
open ~/.bash_profile
3) Add a location to $PATH using following syntax: export PATH="locations separated by colon (:) followed by $PATH"
export PATH="/usr/local/bin:/usr/bin:$PATH"
The $PATH at the end of the string adds the original locations to those just specified.
More single word automation - PostgreSQL dump/export/restore
Why automate this? Because I work on several computers and because South can only update the database structure, whereas, when I move, I want the test data I was working on as well.
Again, a basic hack-ey script - but it feels so good to use:
pg_dump exports the database
dropdb drops it (yeah, I know...)
pg_restore recreates it.
So my automation works similarly to the previous 1 line deploy setup. I take 2 scripts (dump_kfe.sh and restore_kfe.sh) and kick them off from a 'dump' and 'restore' alias in my ~/.bash_profile (this project is called 'kfe')
these are my files inside vagrant:
-------------------------------- ~/.bash_profile
cd /vagrant/kfecom
alias kfe-v='source ~/kfe-venv/bin/activate'
alias dr='source deploy.sh'
alias dl='source local.sh'
alias dump='source /vagrant/kfecom/dump_kfe.sh'
alias restore='source /vagrant/kfecom/restore_kfe.sh'
--------------------------------
-- /vagrant/kfecom/dump_kfe.sh (project folder for my django project)
(DANGER! dropdb will not ask if you are sure!!! -C Creates in the -d database 'postgres')
--------------------------------
Now I can type
dump
to dump the database on my macbook after tinkering, go to the imac, 'vagrant up' the vBox and type
restore
and my data is duplicated on the postgreSQL development server and I can carry on. This works because the script saves the .dump file to the project folder and I use Sugar sync to keep the folders synced. Alternatively, Git would work, as would emailing/Dropboxing the .dump file.
Having used Linux for about 6 years I have found OSX pretty frustrating. Something that really helps productivity in the command line is alias's.
Why OSX is weird
Under Unix .bash_profile is executed when you login, while .bashrc is executed when you open a new terminal window. But in OSX when you open a terminal it runs a login shell by default.
다른 사용자 계정으로 명령을 실행할 수 있는 방법이 다양하게 있는데, 그 중 가장 많이 사용되는 방법을 나열하면 아래와 같다. $ su - 아이디 -c "명령어1; 명령어2; 명령어3" $ sudo -u 아이디 "명령어" $ ssh 아이디@호스트 "명령어" 위와 같은 방법으로 하면 간단하게 실행할 수 있는데, 환경변수가 제대로 먹지 않아서 실행이 안되는 경우가 있다. su, ssh 의 경우, 환경변수를 **.bashrc** 에 넣어두면 안되고, **.profile** 이나 **.bash_profile** 넣어야 된다. sudo 의 경우는 환경변수 보다는 보안적인 이유로 특정 디렉토리에 있는 파일만 실행되도록 설정되어 있다. **/etc/sudoers** 파일을 열어서 아래와 같은 부분을 찾아서, 자신이 실행하고자 하는 파일이 존재하는 디렉토리를 포함시켜주면 된다. Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Here's another favorite - change to a directory, and list all contents.
cd() { builtin cd "$@" && l; }
Better directory listings
You probably noticed that "l" seems a bit unfamiliar. That's because it's actually an alias I setup myself.
See it in action:
And here is "ll" in action. Now you can see the hidden files:
Together, my .bash_profile and .bashrc files are (purposely) extremely generic. Basically, functions and aliases I can use on just about any *nix system. For system-specific configuration data, I put that in .bash_local. It's the perfect spot for exports such as $MYSQL_USER or $MYSQL_PASSWORD.
In practice
For my own convenience, I store these configuration files in my Dropbox. I have symlinks pointing to my home directory.
Now, whenever I ssh into a machine for the first time I have easy access to my dotfiles.
I can't take full credit for every little bit of cleverness, many pieces were cobbled together over time.