34 Command Line

So far, we’ve been using R as our main computational tool, interacting with it via RStudio as our main working environment. These programs, and other similar data analysis environments are very convenient, allowing us to do all sorts of things, from data manipulation, to exploration, to visualization, modeling, simulations, creation of reports, etc. However, sooner or later, you will find yourself using a set of more “low-level” tools, interacting with them in a more “primitive” or “old-fashion” way with what is called a command line interface (CLI) or terminal emulator, instead of using a graphical user interface (GUI) or an integrated development environment (IDE).

34.1 GUIs -vs- CLIs

Most of us who have a laptop or a desktop computer, we interact with them using a Graphical User Interface, commonly referred to as GUI. These interfaces have made our interaction with a computer fairly easy.

Graphical User Interface Finder in Mac

Figure 34.1: Graphical User Interface Finder in Mac

  • Easy to learn
  • Rely on visual displays
  • Can be extremely useful
  • Have improved the friendliness and usability of computers

But this hasn’t always been like that the time. Before the invetion of GUIs, the way in which people interacted with computers and programs was mainly through a command line interface or CLI for short, basically using a keyboard to type in commands, that when executed, we run by a special program called the shell, who talks directly to the operating system of the computer.

In other words, there were no windows, and icons, and interactive devices like a trackpad, a mouse, a touchscreen, or even voice-commands. Instead, if you wanted the computer to do something, you had to tell it with a command, thus using text, typing things on the keyboard, and often without a screen to see what was going on. You only had a command-line interface.

Don't touch that mouse (or trackpad)!

Figure 34.2: Don’t touch that mouse (or trackpad)!

This form of interaction and communication with computers and their programs is still in use. While it is true that most computer users employ a GUI, and will never had the need to learn about CLIs, certain tasks, certain applications, certain jobs, can only be done with a CLI.

  • The command-line is a text-based interface
  • Interacting with the computer via commands
  • The user issues commands in the form of text
  • One of the earliest ways of interacting with a computer

We like to use the metaphor of automatic cars versus stick-shift cars to explain the difference between graphical user interfaces and command line interfaces. Think of GUI as driving an automatic car; this can very quite convenient, letting you do certain tasks in an easier way without having to worry about constatntly changing gears, controlling three pedals. In contrast, CLIs are like driving a stick-shift car which requires using the clutch, shifting gears, listening to the engine, etc.

So why would you ever want to use a CLI, while a GUI seem to be much better?

Well, with stick shift, certain maneuvers are easier to accomplish; it also gives you more control: going up or down a very steep hill, driving on muddy terrain, pushing the car if it runs out of gasoline, battery, or if something else breaks.

The same thing can be said about a GUIs and CLIs.

GUIs are great for:

  • watching videos
  • Playing video games
  • Listening to music
  • Editing videos
  • Photo editing
  • Document Layout
  • Browsing the web
  • Graphic design
  • and having a highly visual display of things like files, directories, programs, tables, etc

But they have their limitations. They don’t allow you to have more control over what your computer can do. Some operations are labor intensive and repetitive. You use clicking & dragging with the cursor, which reduces reproducibility

  • moving files
  • copying a large number of files
  • renaming things
  • finding things
  • Lack of repeatability
  • Lack of reproducibility
  • Some tasks may be labor intensive
  • Limit analyses on a cluster of computers

34.2 Command Line

The type of command line and shell that we are going to cover is a unix-like style. Which means that, if you are Windows user, you don’t have a unix-based operating system. If this is your case, we recommend that you install Git, which comes with a bash emulator called git-bash. Git for Windows is free, and can be easily downloaded and installed by following the instructions in the link below:


If you are using linux as your operating system, then you have the terminal. If you are using a Mac computer, you also have a terminal.

34.2.1 Some Vocabulary and Technicalities

Some terms:

  • Kernel
  • Shell
  • Bash shell
  • Command Line Interface
  • Terminal Emulator
Terminal, Shell, OS

Figure 34.3: Terminal, Shell, OS

The Kernel is the core of the operating system in Unix. It takes care of allocating time and memory to program. It does very fundamental root level management.

The Shell is the outer layer of the operating system. That’s what we see when we open up a terminal window: we are working in the Shell. Simply put, it is a program that takes commands from the keyboard and pass them to the Operating System, via the Kernel, to be executed. The Shell interacts with the user and sends requests to the kernel. The Kernel, in turn, sends results back to the Shell.

The command line or terminal is a text-based interface where you type commands and direct text-based input and output to the screen, to files, or to other programs. The environment you use is called a shell or a command-line interpreter.

In this book, the main programs we’re going to be concern are the Terminal and the Shell.

The way to interact with UNIX is from the command line. If you use Mac, access to the command line is with the Terminal Application. That’s why many people use the terms “command line” and “terminal” as synonyms (although technically they are not the same thing).

The shell does 4 simple things:

  • displays a prompt in the terminal window (waiting for commands)
  • reads your command
  • runs the command
  • prints the output, if any, to the Terminal window

The most common type of Shell is Bash which an acronym for Bourne Again Shell. The bash sheel is the default shell for Linux and Mac computers. It is also the shell flavor used in Git-bash, which is what we recommend to use if your computer runs Windows.

The prompt, and its meaning

Figure 34.4: The prompt, and its meaning

Interacting with the shell requires using commands.

34.2.2 Anatomy of shell commands

All shell commands have the same structure, illustrated in the following figure

Anatomy of a shell command

Figure 34.5: Anatomy of a shell command


  • Name of the command.
  • Always a single word.
  • The thing you want to do.


  • Options are optional.
  • Not all commands require options.
  • Controls the behavior of the command.
  • Specified with single or double dashes.


  • Arguments may be optional.
  • Arguments tell the command what to operate on.
  • Typically will be the name of a file or directory.

Let’s see an example with the ls command. This one shell commands that you will use constantly when working with the terminal. This command lists all the contents (in terms of files) in a given directory. The default usage is like so:


With the default usage, you just call the name of the command, without specifying explicit options and arguments.

Let’s use ls and one of its options, the -l option to lists the contents in long format:

ls -l

In addition to the -l option, we can also add an argument, for example, a directory /usr/bin

ls -l /usr/bin

Some of the basic commands are:

  • exit: closes the terminal
  • clear: clears the console
  • cal: shows calendar of current month
  • date: displays today’s date and time
  • who: displays username that is logged in

Your Turn

Run the following calls of command cal and see what happens:

  • cal
  • cal 2020
  • cal jan 2020
  • cal -m jan
  • cal -m 1
  • cal -y 2000
  • cal -j 2010

34.2.3 Manual Documentation

The man command is the command that shows you the technical (manual) documentation of a given command:

Manual docummentation of command 'cal'

Figure 34.6: Manual docummentation of command ‘cal’