In the Beginning was the Command Line

Author: Matt Burton

Getting to the Command line

On OSX

OSX Terminal Location

On Windows (using Anaconda)

Anaconda Terminal Location

Moving around

The directory tree

The Directory Tree

Except in your head

terminal

Four commands to move around a (unix) command line

pwd - To print working directory (OSX/Linux only)

pilemian:2014-summer-camp mcburton$ pwd
/Users/mcburton/Dropbox/si/bigdatabootcamp/2014-summer-camp

ls (OSX) to dir (Windows) - To list directory contents

pilemian:2014-summer-camp mcburton$ ls
LICENSE.md  _config.yml _layouts    css     index.html  setup
README.md   _includes   _site       img     js      slides
pilemian:2014-summer-camp mcburton$

Use the -l parameter to do a long listing

pilemian:2014-summer-camp mcburton$ ls -l
total 40
-rw-r--r--   1 mcburton  staff  2838 Apr 24 15:14 LICENSE.md
-rw-r--r--   1 mcburton  staff   248 Apr 24 22:20 README.md
-rw-r--r--   1 mcburton  staff   321 Apr 24 16:06 _config.yml
drwxr-xr-x   7 mcburton  staff   238 Apr 24 15:14 _includes
drwxr-xr-x   4 mcburton  staff   136 Apr 24 16:57 _layouts
drwxr-xr-x  11 mcburton  staff   374 May 19 09:22 _site
drwxr-xr-x   6 mcburton  staff   204 Apr 24 17:20 css
drwxr-xr-x   9 mcburton  staff   306 May  9 12:10 img
-rw-r--r--   1 mcburton  staff  6717 May 18 22:00 index.html
drwxr-xr-x   4 mcburton  staff   136 Apr 24 15:14 js
drwxr-xr-x  16 mcburton  staff   544 May 18 22:17 setup
drwxr-xr-x   5 mcburton  staff   170 May 18 21:48 slides
pilemian:2014-summer-camp mcburton$

The dir command on Windows does a long listing automatically

C:\Users\mcburton>dir
 Volume in drive C has no label.
 Volume Serial Number is 2E3D-5F00

 Directory of C:\Users\mcburton

05/15/2014  01:24 AM    <DIR>          .
05/15/2014  01:24 AM    <DIR>          ..
05/14/2014  02:17 PM    <DIR>          .continuum
05/14/2014  06:59 PM    <DIR>          .ipython
05/14/2014  06:56 PM    <DIR>          .matplotlib
05/14/2014  07:42 PM    <DIR>          Anaconda
05/14/2014  01:56 PM    <DIR>          Contacts
05/15/2014  12:31 AM    <DIR>          Desktop
05/15/2014  01:24 AM                 0 dir
05/14/2014  02:17 PM    <DIR>          Documents
05/15/2014  12:31 AM    <DIR>          Downloads
05/14/2014  01:56 PM    <DIR>          Favorites
05/14/2014  01:56 PM    <DIR>          Links
05/14/2014  01:56 PM    <DIR>          Music
05/14/2014  01:56 PM    <DIR>          Pictures
05/15/2014  01:24 AM                 0 Saved
05/14/2014  01:56 PM    <DIR>          Saved Games
05/14/2014  01:56 PM    <DIR>          Searches
05/14/2014  01:56 PM    <DIR>          Videos
               2 File(s)              0 bytes
              17 Dir(s)  12,957,863,936 bytes free

cd <filepath> - To change directory

pilemian:2014-summer-camp mcburton$ pwd
/Users/mcburton/Dropbox/si/bigdatabootcamp/2014-summer-camp
...
pilemian:2014-summer-camp mcburton$ cd setup
pilemian:setup mcburton$ 
...
pilemian:setup mcburton$ pwd
/Users/mcburton/Dropbox/si/bigdatabootcamp/2014-summer-camp/setup

mv <existing filepath> <desired filepath> - To move files (OSX)

move <existing filepath> <desired filepath> - To move files (Windows)

File Paths

/foo/bar - the bar is a subdirectory of foo

/Users/mcburton/Dropbox/si/bigdatabootcamp/2014-summer-camp/setup

".." - means up/back one directory

pilemian:setup mcburton$ pwd
/Users/mcburton/Dropbox/si/bigdatabootcamp/2014-summer-camp/setup

pilemian:setup mcburton$ ls -l
total 3280
[email protected] 1 mcburton  staff  218212 May 16 12:31 OSX-anaconda-launcher.png
[email protected] 1 mcburton  staff  175418 May 18 22:17 anaconda-command-line.png
[email protected] 1 mcburton  staff  113444 May 16 12:37 anaconda-launcher-launch.png
[email protected] 1 mcburton  staff  114809 May 16 12:38 anaconda-launcher.png
[email protected] 1 mcburton  staff   77454 May 18 22:07 dir-tree.png
[email protected] 1 mcburton  staff  244584 May 16 14:17 sqlite-manager-install.png
[email protected] 1 mcburton  staff   79532 May 16 14:25 sqlite-manager.png
[email protected] 1 mcburton  staff  253725 May 18 22:15 terminal-location.png
[email protected] 1 mcburton  staff   44467 May 18 22:09 terminal.png
-rw-r--r--  1 mcburton  staff    8531 May 19 09:40 the-command-line.ipynb
-rw-r--r--  1 mcburton  staff    6787 May 18 13:13 the-setup.ipynb
[email protected] 1 mcburton  staff  277489 May 16 12:33 windows-8-anaconda-launcher.png
[email protected] 1 mcburton  staff   41325 May 19 09:33 windows-commands.png

pilemian:setup mcburton$ cd ..

pilemian:2014-summer-camp mcburton$ pwd
/Users/mcburton/Dropbox/si/bigdatabootcamp/2014-summer-camp

pilemian:2014-summer-camp mcburton$ ls -l
total 40
-rw-r--r--   1 mcburton  staff  2838 Apr 24 15:14 LICENSE.md
-rw-r--r--   1 mcburton  staff   248 Apr 24 22:20 README.md
-rw-r--r--   1 mcburton  staff   321 Apr 24 16:06 _config.yml
drwxr-xr-x   7 mcburton  staff   238 Apr 24 15:14 _includes
drwxr-xr-x   4 mcburton  staff   136 Apr 24 16:57 _layouts
drwxr-xr-x  11 mcburton  staff   374 May 19 09:40 _site
drwxr-xr-x   6 mcburton  staff   204 Apr 24 17:20 css
drwxr-xr-x   9 mcburton  staff   306 May  9 12:10 img
-rw-r--r--   1 mcburton  staff  6717 May 18 22:00 index.html
drwxr-xr-x   4 mcburton  staff   136 Apr 24 15:14 js
drwxr-xr-x  17 mcburton  staff   578 May 19 09:33 setup
drwxr-xr-x   5 mcburton  staff   170 May 18 21:48 slides

Running the Python Interactive Interpreter

mcburton$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello world')
hello world
>>>
In [ ]:
 

Downloading and Running a Python script from the web

Download this really simple python script

python <filename> - run the python script called filename in the current directory.

Move, using the commands introduced above, to the directory where you downloaded hello-world.py then execute it with the python command.

mcburton$ python hello-world.py 
Hello World
1
2

Running the IPython Notebook

mcburton$ ipython notebook

This spits out a bunch of gobbly gook into the terminal, but also launches a web browser.

Ipython Notebook Tree

Ipython Notebooks are web-based Python interpreters that let you mix prose and code.

Lab Notebooks for computational social science.

Downloading and Running a IPython Notebook from the web

Click the download notebook button

NBviewer example