Skip to content

Command line

Leon du Toit edited this page Oct 14, 2013 · 4 revisions

This section assumes you are running a Terminal session running on a Unix-like OS (i.e. Mac or Linux). If you're on Windows you can skip ahead to the Vagrant VM section, boot it up and log in, and return here. I also assume that you are running [bash](http://en.wikipedia.org/wiki/Bash_(Unix_shell). You should see something like this:

Last login: Mon Oct 14 14:22:56 on ttys007
something_here:~ leondutoit$

I'll simply use the $ to indicate the current cursor position. Everything after a # is a comment. To get a hang of the essentials you can do the following and see how it happens.

Very basic

$ echo "Hello there person"

# present working directory
$ pwd

# manual entry about pwd - use man to read about things
$ man pwd
$ man man
$ ls

# list files and display human readable sizes in long format
$ ls -alh

Basic

Let's make a new directory, create some files, put stuff in them, see what is inside and delete them all.

$ mkdir new_dir
$ cd new_dir
$ touch file1
$ echo "this is going to be line 1 in the new file" >> file1
$ echo "this will be line 2" >> file1

# common ways to look at files
$ man cat
$ man less
$ man tail
$ man more

# look at it (to escape less press 'q')
$ cat file1
$ less file1
$ tail file1
$ more file1

# go to the directory 'above' and search for the file
$ cd ..
$ find . -iname file1

# remove the folder and file
$ rm -rf new_dir

Little less basic

Let's make a bash script that runs a small python program - absurd, but sensible enough to show some things.

$ mkdir a_thing
$ cd a_thing
$ touch stupid.py
$ echo "print 4 + 4" >> stupid.py
$ touch run_it.sh 
$ echo "echo 'Going to print the answer to 4 + 4 using python'" >> run_it.sh
$ echo "python stupid.py"

# make the shell script 'executable'
$ chmod 777 run_it.sh
$ ./run_it.py

Make some useful aliases

An alias is a keyboard shortcut for the command line. You can define your shortcuts in the .bash_profile, if you also make sure to call your .bashrc each time (You will see this in the example).

# Source bashrc each time a new window is opened
[[ -r ~/.bashrc ]] && . ~/.bashrc

# shortcut to find files
alias f='find . -iname'

# avoid irritating typo
alias cd..='cd ..'

# listing files
alias ll='ls -al'
alias lh='ls -alh'

We will see a few more useful ones shortly when discussing version control.

Clone this wiki locally