Command line



The "lingo"

  • "Terminal"
  • "Command-line"
  • "Command prompt"
  • "Shell"
  • "Console"

These are all pretty much the same thing.

What is a Terminal?

A text-based command interpreter

The most common shell is "bash"

For OS X, use the "Terminal" application

Fire it up!



Usually shows your username and computer name

Indicates that the terminal is ready for a command



Indicates your current spot in the terminal

Shows you where the stuff you type will go

Your First Command

  1. Type pwd into the terminal
  2. Press the enter key

Your First Command


The clear command clears the contents
of the terminal and issues a prompt.

This is good for removing previous
output that is unnecessary to the task at hand.

Feel free to use this whenever things get too cluttered.


Also referred to as "folders"

A container for files or other directories

Nested files and directories
can be referenced using paths

Directory Trees

The set of all folders, taken together,
makes up your entire file system.

This system is organized into a kind of
upside down tree.

At the very top of the tree is the root folder. 


Each directory or file is separated by a forward slash "/"

There are two kinds:

Relative: Desktop/the_project/overview.txt

Absolute: /Users/jcash/Desktop/walk-the-line.mp3


  • Current Directory -  .
  • Parent Directory -  ..
  • Home Directory -  ~


The cd command changes
the current working directory.

It expects a file path as an "argument".

If no file path is given, it assumes
your home directory by default.



The ls command lists the
contents of a directory.

It expects a file path as an "argument".

If no file path is given, it assumes
the current directory by default.



The ls command accepts several options flags.

A flag is an special argument that
is used to set an option for the command.

These are commonly a hyphen followed by a single character (e.g. "-g")

Setting the -l flag on the ls command causes it to provide more verbose output.

ls -l

cd & ls

Play with the cd and ls commands.

Be sure to incorporate:
  1. relative file path
  2. absolute file path
  3. the . shortcut
  4. the .. shortcut
  5. the ~ shortcut
  6. cd without an argument

use pwd to check your location periodically

Making a Directory

Use the mkdir command to create a new empty directory

Pass the path of the directory as the first argument

If the base of the path doesn't already exist,
the command will fail

Use the -p flag to create the full path is non-existent

Removing Directories

Use the rmdir command to remove an empty directory

Use the rm -r to remove a non-empty directory


  1. cd to your home directory
  2. create the girl/develop directory path
  3. navigate into the girl/develop directory
  4. create the it directory
  5. view the contents of the it directory
  6. navigate up two directories
  7. use the pwd command to verify you are home
  8. remove the girl/develop/it path


Use cat to output the contents
of a file to the console

Use more to step through the
contents of a file a screen at a time

Use less to step backwards or forwards 


Explore the /usr/share/misc files
using cat, more, and less

Hidden Files

Filename that begin with a period
are hidden from normal output.

e.g. ".bashrc"

Use the ls command with the -a flag
to see hidden files in addition to the usual output.

Type ls -la into your terminal

Use the -h flag to get human readable file sizes

ls -la

Creating and Removing Files

Use touch to create a blank file or update the timestamp on an existing file
Use rm to remove files


The unix security model breaks up file
permissions into three groups:

  1. Owner
  2. Group
  3. Others

rwx r-x r-x 


Changing Permissions

Use chmod to change
permissions on a file/directory

The argument has three sections:

  1. u, g, o to signify which level to target
  2. + or - to indicate whether adding or removing
  3. r, w, x to indicate what type of permission to change

chmod u+w to add write permissions for the file's owner
chmod go-rwx to remove all permissions for
the file's group and everyone else.


  1. Create a new file named testfile in your home directory
  2. Grant yourself execute permission on the file
  3. Remove read permission on testfile from
    your group and everyone else
  4. Delete testfile

Standard Output

Most commands display their results to
a mechanism called the standard output.

By default, this directs its content to the display.

The standard output can be redirected to
a file using the > operator.

e.g. ls > file_list.txt

Standard Output

In order to append to the file instead of overwriting it, use the >> operator instead.

e.g. ls >> file_list.txt

Both the > and >> operator will create the file if it doesn't exist.

Standard Input

Whenever commands accept keyboard input, it's likely they are really just drawing input from a mechanism called standard input.

By default, this is set to keyboard input.

The input to a command can be redirected to
a file by using the < operator.

e.g. sort < file_list.txt

Both input and output can be redirected at the same time.

e.g. sort < file_list.txt > sorted_file_list.txt


The "|" character can be used to allow
commands to communicate during execution

Pipes are placed between commands.

A pipe will cause the output of the
left command to be used as the
input of the right command

ls -l | grep "myfile.txt"
du | sort -nr


The grep command outputs only the
lines from a file that match a given pattern.

This is useful for doing text searches within a file.

The first argument is the pattern to match
The the second, third, and so on are files to search within

e.g. grep "ls" .bash_history

If no file argument is given, it
will expect input from the keyboard


Commands whose behavior follows the pattern:

  1. Accept input from standard input
  2. Perform some operation on it
  3. Send the results to standard output


Check out the man pages for the following



ls -a | grep bash
Use the "ls" and "grep" commands to print
out only the files in your home directory that contain the word "bash"


Whenever a command is executed, a new process is created


Provides an up-to-date feed of information
on the most active processes across the machine.
Useful for finding processes that are hogging the CPU


The ps utility displays all of your
processes that have controlling terminals.


Key Sequences

Ctrl+Z  Suspend the current process

Ctrl+C Terminate the current process

Suspending a Process

bg & fg

The bg and fg commands bring a suspended process back into a running state.

bg runs it in a background process

fg runs it in the foreground of the current terminal

By default they target the most recently suspended process.

The job number can be provided as an argument.
(The number we saw when we suspended the process)


  1. Run the top command
  2. Suspend the process
  3. Bring the process back into the foreground


The kill command terminates
or sends a signal to a process

It takes the process id (PID) as an argument

You can find the PID using top or ps


  • Clear your terminal and run the "yes" command
  • Use the Ctrl+Z key sequence to suspend the process
  • Use the "bg" command to send the process to the background
  • Notice the output is still happening even though the process is running in the background
  • Open a new terminal window, locate the PID for the "yes" process and use it to kill the process.


Typing ! followed by the name of command
will repeat the last line that began with that command

  1. ls -la ~ | grep "Desktop"
  2. !ls

In both instances, you should get the same
output because the second line is simply repeating the first

Type !! by itself will re-run the last line entered


The terminal keeps an ongoing log
of all the commands you've run.

The history command prints the
contents of the history file



You can run a previous command
using the number in the history.

e.g. !12

Almost no one does this


Ctrl+a moves the cursor to the beginning of the command
Use the tab key to auto complete file paths
Ctrl+e moves the cursor to the end of the command


  1. Use your up and down arrows to locate a past
    command with one or more arguments
  2. Jump to beginning of the line
  3. Jump to the end of the line
  4. Change one of the arguments and run it
  5. Run the date command
  6. Re-run the command from step 4 using !
  7. Time the execution of your original
    command by running time !!

Real World Examples

Here are some examples of using
the command line to accomplish
tasks relating to the development process

Serve up a Directory

python -m SimpleHTTPServer

don't forget: you can Ctrl+C to exit

Version Control

Check out Pro Git for a crash course on command-line git
Initialize the current directory as a Git repository
git init .
Include all changes next time we commit
git add --all
Commit the changes
git commit -m "Hello Git!"
View the commit history
git log

Watching a Log File

tail -f my_awesome.log

The Red Pill

For those wanting to go deeper down the rabbit hole,
check out these resources.

Hit me up with questions.
@meany_face (twitter)

Intro to Command line

By Jared Stilwell

Intro to Command line

  • 704
Loading comments...

More from Jared Stilwell