This tutorial provides an introduction to the Unix command line.

What is the OSX terminal?

The OSX terminal provides an command line interface to Mac operating system (OS). This allows you to

  • customize your workspace
  • work faster and more efficiently: no mouse or touchpad!
  • better debug when you break stuff

This tutorial will teach you how to get started with the Unix command line, which encompasses Linux systems Android, iOS, and macOS.

I called this tutorial “Using the OSX terminal” but what we learn here will allow you to interact with any OS within the family Unix operating systems.

Getting started

There are two ways to open the OSX terminal.

  1. Open terminal with Spotlight Search using Cmd-space
  2. Open Applications > Utilities > Terminal

Your open terminal will look something like this:

You’ll see a prompt on the screen, which is an alphanumeric string that (usually) ends in $. Commands are typed after the $. In the screenshot provided I typed the command cd Desktop, which we’ll get to shortly.

First we’ll use the echo command, which prints a string to the console. Type ‘echo hello’ at the prompt and press Return.

$ echo hello
hello
$ echo 'hello'
hello
$ echo "hello"
hello

All of the calls above produce the same result. You can use quotations to define a string, but often you don’t have to! Be careful, though! If you forget to close the string like in the example below,

$ echo "hello
>

you won’t get the prompt line back! To get back to the prompt line, $, type Ctrl-C. This will get you out of trouble in many other cases as well.

Try it!

  • type echo some cool message after the $ and press Return

Asking for help

Typing ? function_name() into your R Studio console brings up the help file, or documentation, for that particular function. Knowing how to access and read the documentation for a programming language is a crucial set of skills. How do we pull up documentation for the Unix and Unix-like operating systems?

Let me man-page-splain this to you.

The documentation for Unix is pulled up using the man command, which stands for manual, as in user manual.

$ man echo

The above command will pull up the documentation for the echo command, which looks like this:

man pages can take getting used to! Luckily all follow the same format:

  • commands, like echo, have options, or flags, that are specific to that command and help you customize what you are trying to do
    • similiar to arguments of a function in R
    • access using a - after the name of the command: command_name -flag_name
    • you can use multiple flags with one command like this: command_name -flag1 -flag2
  • The SYNOPSIS part of a man page shows what flags are available for that particular command
    • for the echo command there is only one flag option: -n
    • options are explained in the DESCRIPTION section
  • exit a man page by pressing Q on your keyboard

For example, try

$ echo -n hello world

What do we expect will happen here based on the description in the man page for echo?

Creating and manipulating files

Okay, so now we can navigate around our computer’s file system, but what about editing files and directories? The following commands will get you far.

  • mkdir: make directory
  • rm: remove a file (or directory, with special options). Irreversible, so be careful!
  • cp: Copy a file
  • mv: Move and rename files. Can be used just to rename a file if you do not want to retain a copy of the old file
  • open: Open a file or directory
  • touch: technically this command is for changing time stamps of files, but if the file name you give it does not exist it creates that file for you! This secondary purpose of the touch command can be very useful.

Let’s try some of these commands now.

$ pwd
/Users/juliawrobel
$ cd Desktop
$ mkdir favorite_animals
$ ls
$ cd favorite_animals
$ ls
$ echo "snakes are my favorite animal. I used to have a pet ball python"
snakes are my favorite animal. I used to have a pet ball python
$ echo "snakes are my favorite animal. I used to have a pet ball python" > snake.txt
snake.txt
$ ls
snake.txt
$ cat snake.txt
snakes are my favorite animal. I used to have a pet ball python
$ mv snake.txt reptile.txt
$ ls
reptile.txt
$ touch reptile2.txt
$ ls 
reptile.txt reptile2.txt
$ touch gerbil.R
$ ls *.txt
animal.txt  reptile2.txt
$ ls -l *.txt
$ cp reptile2.txt animal2_copy.txt
$ open animal2.txt
$ rm animal2_copy.txt

Try it!

  1. create a new directory within your favorite animals directory called junk_folder
  2. rename the directory
  3. navigate into that directory and put a new text file in it
  4. navigate up a directory
  5. delete the directory previously named junk_folder and all of it’s contents

The last step may be a little more challenging. Hint: use an option from the man page for the rm command.

Shortcuts

This is an interlude on some tricks and shortcuts to maximize your efficiency when using the terminal.

  • up arrow: access previous commands
  • tab: autocomplete! Super useful
  • !command_name: Repeats the line from the last time you used that command. Useful for long commands you don’t want to re-type
  • Ctrl-A : Moves to the beginning of the line
  • Ctrl-E: Moves to the end of the line
  • Ctrl-U: Clears everthing to the left of the cursor
  • Option-click: Moves cursor to location clicked
  • clear: Typed as a command at the prompt, this clears the window

In our next live coding chunk I’ll incorporate some of these shortcuts.

Text file tricks

I could have probably come up with a better header here- but the point is you can create, look at, and manipulate text files right from the terminal, and this can streamline your workflow.

As an example we’re going to work with a text snippet from the Wikipedia page on Giant Pandas.

The giant panda (Ailuropoda melanoleuca, literally "black and white cat-foot"; Chinese: 大熊猫; pinyin: dà xióng māo, literally "big bear cat"), also known as panda bear or simply panda, is a bear native to south central China. 
It is easily recognized by the large, distinctive black patches around its eyes, over the ears, and across its round body. 
The name "giant panda" is sometimes used to distinguish it from the unrelated red panda. Though it belongs to the order Carnivora, the giant panda's diet is over 99% bamboo. 
Giant pandas in the wild will occasionally eat other grasses, wild tubers, or even meat in the form of birds, rodents, or carrion. 
In captivity, they may receive honey, eggs, fish, yams, shrub leaves, oranges, or bananas along with specially prepared food.

The giant panda lives in a few mountain ranges in central China, mainly in Sichuan, but also in neighbouring Shaanxi and Gansu. 
As a result of farming, deforestation, and other development, the giant panda has been driven out of the lowland areas where it once lived.

The giant panda is a conservation reliant vulnerable species. 
A 2007 report showed 239 pandas living in captivity inside China and another 27 outside the country. 
As of December 2014, 49 giant pandas lived in captivity outside China, living in 18 zoos in 13 different countries. 
Wild population estimates vary; one estimate shows that there are about 1,590 individuals living in the wild, while a 2006 study via DNA analysis estimated that this figure could be as high as 2,000 to 3,000. 
Some reports also show that the number of giant pandas in the wild is on the rise. 

In March 2015, Mongabay stated that the wild giant panda population had increased by 268, or 16.8%, to 1,864. In 2016, the IUCN reclassified the species from "endangered" to "vulnerable".

Using tools we just learned about, create panda.txt, a file containing the above text about giant pandas. Put it in your favorite_animals directory. There are two ways you can do this: (1) using the echo command and the > operator, and (2) using the touch command to create a new file then the open command to open and edit it.

Understanding your text files

The following commands allow you to view, quantify, compare, and search text files.

  • head: shows the first 10 lines of a file
  • tail: shows the last 10 lines of a file
  • wc: word count! Counts the number of lines, words, and bytes in a file
  • diff: Compare two files line by line
  • >: Redirect operator (we touched on this earlier)
    • Used to direct the results of a command to a new file
    • e.g. head panda.txt > panda_head.txt
  • |: Pipe operator
    • Used to string together commands
    • Analogous to %>% function in R
    • e.g. head panda.txt | wc
  • less: allows you to interactively look through and search a text file in the terminal
    • e.g. less file_name.txt
    • More flexible than cat file_name.txt
    • ^F: Move forward a page
    • ^B: Move backwards a page
    • /<string>: Backslash allows you to search through the file
    • n: Move to next search result
    • N: Move to previous search result
    • G: Move to the end of the file
    • Type Q to quit less

Now let’s get back to the terminal and use these commands.

$ pwd
/Users/juliawrobel/Desktop/favorite_animals
$ ls 
$ head panda.txt
$ tail panda.txt
$ head -2 panda.txt
$ wc panda.txt
$ head panda.txt | wc
$ less panda.txt
/panda
q
$ cp panda2.txt
$ diff panda.txt panda2.txt

Try it!

Let’s add a photo of one of our favorite animals to the favorite_animals folder

  1. Google image search an animal of your choice. I’m going to search kit foxes
  2. Take a screen shot of one of those images. On Macs you can take screen shots using Cmd-shift-4 and dragging the cursor over the area on the screen you want to select
  3. Screenshots automatically get saved to your Desktop with an uninformative name-
    1. Navigate to your Desktop
    2. Rename your screenshot and move it to the favorite_animals folder
  4. Navigate back to your favorite_animals folder and check that your image is there. What commands did you use to do this? What other commands could you also use?

Running R within the terminal

You can run R interactively right from the terminal! Just type R at the prompt then proceed as you would in an R console. Type q() to quit.

$ R
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> x = 1:100
> sum(x)
[1] 5050
> plot(x)
> q()
Save workspace image? [y/n/c]: n
$

You can also interactively run other programming languages right from the terminal, including Python, perl and others. Doing this can be useful for a quick calculation or if you’re working from within an HPC cluster or other environment that might not have R Studio available.

Running the terminal within R Studio

Right now this is only available in a special new Release of R Studio. * Use Unix-like commands from within R Studio * (Seems that it) Works for PC users as well as Mac users * Not yet available for the “regular” version of R Studio

Other fun tricks

  • Reverse a string
$ echo "dlroW ,olleH" | rev
Hello, World
  • Pull up a calendar of the current month
$ cal
   November 2017
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
  • Print a string a whole bunch of times (exit with Ctrl-C)
$ yes "this tutorial is awesome!"
  • Get the history of recently used commands.

Want to revisit the commands I used while live-coding today? I have stored all the commands I used in the live-coding session into a text file, and will post them shortly.

This is how I stored these commands. First I store the history in a text file then I clear it.

$ history > history.txt
$ history -c

Resources