This tutorial provides an introduction to the Unix command line.
The OSX terminal provides an command line interface to Mac operating system (OS). This allows you to
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.
There are two ways to open the OSX terminal.
terminal
with Spotlight Search using
Cmd-space
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!
echo some cool message
after the
$ and press ReturnTyping ? 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:
echo
, have options, or
flags, that are specific to that command and help you
customize what you are trying to do
R
-
after the name of the command:
command_name -flag_name
command_name -flag1 -flag2
man
page shows
what flags are available for that particular command
echo
command there is only one flag option:
-n
man
page by pressing Q
on your
keyboardFor example, try
$ echo -n hello world
What do we expect will happen here based on the description
in the man
page for echo
?
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 directoryrm
: remove a file (or directory, with special options).
Irreversible, so be careful!cp
: Copy a filemv
: Move and rename files. Can be used just to rename a
file if you do not want to retain a copy of the old fileopen
: Open a file or directorytouch
: 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!
The last step may be a little more challenging.
Hint: use an option from the man
page for
the rm
command.
This is an interlude on some tricks and shortcuts to maximize your efficiency when using the terminal.
!command_name
: Repeats the line from the last time you
used that command. Useful for long commands you don’t want to
re-typeCtrl-A
: Moves to the beginning of the lineCtrl-E
: Moves to the end of the lineCtrl-U
: Clears everthing to the left of the cursorOption-click
: Moves cursor to location clickedclear
: Typed as a command at the prompt, this clears
the windowIn our next live coding chunk I’ll incorporate some of these shortcuts.
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.
The following commands allow you to view, quantify, compare, and search text files.
head
: shows the first 10 lines of a filetail
: shows the last 10 lines of a filewc
: word count! Counts the number of lines, words, and
bytes in a filediff
: Compare two files line by line>
: Redirect operator (we touched on this earlier)
head panda.txt > panda_head.txt
|
: Pipe operator
%>%
function in R
head panda.txt | wc
less
: allows you to interactively look through and
search a text file in the terminal
less file_name.txt
cat file_name.txt
^F
: Move forward a page^B
: Move backwards a page/<string>
: Backslash allows you to search through
the filen
: Move to next search resultN
: Move to previous search resultG
: Move to the end of the fileless
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
Cmd-shift-4
and dragging the cursor over
the area on the screen you want to selectfavorite_animals
folderfavorite_animals
folder and check
that your image is there. What commands did you use to do this? What
other commands could you also use?R
within the terminalYou 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.
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
$ echo "dlroW ,olleH" | rev
Hello, World
$ 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
Ctrl-C
)$ yes "this tutorial is awesome!"
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
git
through the command line