What You Need

Shell, Bash, and Terminal - An Overview

In the lesson on Open Reproducible Science, you learned that Shell is the primary program that computers use to receive code (i.e. commands) and return information produced by executing these commands (i.e. output).

These commands can be entered via a Terminal (i.e. Command Line Interface - CLI) or can be executed via software, which provide graphical user interfaces (GUIs) that send commands to the Shell and receive output back from the Shell when the command has been executed.

Bash (also known as the “Bourne-Again Shell”) is an implementation of Shell and provides many commands that can be executed via a Terminal. You will only use a handful of commands in this course, but more information on the available commands is provided on the bottom of this page under Additional Resources.

There are also many options for terminals, including customized ones like Git Bash, which Windows users will use in this course.

use repeatable commands for these tasks across many different operating systems (Windows, Mac, Linux)

What Is a Directory?

A directory refers to a folder on a computer that relates to other folders. The term directory considers the relationship between that folder and the folders within it and around it. Directories are hierarchical.

What Is a Parent Directory?

The term “parent” directory is used to describe the preceding directory in which a sub-directory is created.

Open a Terminal to Run Bash Commands

The tool that you use to run bash commands will vary depending upon your computer’s operating system.

Mac (OS X)

You can use the program called Terminal, which can be searched for in Spotlight (or found in /Applications/Utilities) and uses the Bash implementation of Shell. Terminal is installed natively on the MAC OS.

This is what the Terminal on Mac looks like. Source: Apple.com.

Linux

You can use the program called Terminal (or Terminal Emulator), which typically uses the Bash implementation of Shell.

This is what the Terminal on Linux looks like.

Windows

There are many options for running bash on windows. For this course, you will use Git Bash which comes with your installation of Git. Instructions for setting this up are here:

Once you have installed Git on a Windows machine, you can open this Terminal by searching for Git Bash in the start menu. Use Git Bash for all hands-on activities and assignments in this course that ask you to use the Terminal.

This is what the Git Bash Terminal on Windows looks like.

Run Bash Commands in the Terminal

The dollar sign ($) is a prompt that shows you that Bash is waiting for input.

$ bash
$

Note: depending on your computer’s set-up, you may see a different character as a prompt and/or may add information before the prompt, such as the directory that you are currently in.

When typing commands (either from this course or from other sources), do not type the dollar sign (or other character prompt). Only type the commands that follow it.

Test That Bash Is Available on Your Computer

Type “bash” into the Terminal. You will only get a message back if the command is not successful. If the command is successful, you will simply see a new line prompt waiting for more input.

$ bash
$

Note: In the examples below, the indented lines that follow a prompt and do not start with a dollar sign ($) are the output of the command. The results of the commands below on your computer will be slightly different, depending on your operating system and how you have customized your file system.

Print Your Current Working Directory (pwd)

Your current working directory is the directory where your commands are being executed. It is typically printed as the full path to the directory (meaning that you can see the parent directory). To print the name of the current working directory, use the command pwd.

$ pwd
/users/jpalomino

As this is the first command that you have executed in Bash today, the result of the pwd is the full path to your home directory. The home directory is the first directory that you will be in each time you start a new Bash session.

Windows users: note that the Terminal uses forward slashes (/) to indicate directories within a path. This differs from the Windows File Explorer which uses backslashes (\) to indicate directories within a path.

Change Working Directory (cd)

To change directories, use the command cd followed by the name of_directory (e.g. cd downloads). You can print your current working directory again to check the new path.

For example, you can change the working directory to documents under your home directory, and then check that the current working directory has been updated.

$ cd documents
$ pwd
/users/jpalomino/documents

You can go back to the parent directory by using the command cd ... The full path to the current working directory is understood by Bash.

$ cd ..
$ pwd
/users/jpalomino

You can also go back to your home directory (e.g. /users/jpalomino) at any time using the command cd ~ (the character known as the tilde).

$ cd ~
$ pwd
/users/jpalomino

Create a New Directory (mkdir)

The first step in creating a new directory is to navigate to the directory that you would like to be the parent directory to this new directory. Then, use the command mkdir, followed by the name you would like to give the new directory (e.g. mkdir directoryname).

For example, you can create new directory under documents called assignments. Then, you can navigate into the new directory called assignments, and print the current working directory to check the new path.

Machine Readable Directory Names Are Important

Don’t use spaces in your names. Instead of spaces, you can use - or _ to separate words within the name.

In the image below, compare the list of file names on the LEFT to those on the right. Which ones are easier to quickly understand?

This image displays examples of good and not-so-good names for directories and files. Source: Jenny Bryan, Reproducible Science Curriculum.

Windows Users: note that the names of your existing directories often begin with upper case letters. When creating new directories, use lower case to follow the lessons more easily and for best results from future programming tasks.

Print a List of All Files and Sub-directories (ls)

To see a list of all files and sub-directories within your current working directory, use the command ls.

$ pwd
/users/jpalomino
$ ls
documents downloads

For example, you can change your current working directory to documents and print a new list of all files and sub-directories to see our newly created assignments directory by using the cd and ls functions.

$ cd documents
$ ls
assignments

You can also create a new directory under assignments called homeworks, and then list the contents of the assignments directory to see the newly created homeworks.

$ cd assignments
$ mkdir homeworks
$ ls
homeworks

Delete a Directory (rm)

To delete (i.e. remove) a directory and all the sub-directories and files that it contains, navigate to its parent directory and use the command rm -r, followed by the name of the directory you want to delete (e.g. rm -r directoryname).

For example, you can delete the assignments directory under the documents directory because it does not meet the requirement of a good name for a directory.

The rm stands for remove, while the -r is necessary to tell Bash that it needs to recurse or repeat the command) through a list of all files and sub-directory within the parent directory. This means that the newly created homeworks directory under assignments will also be removed.

Close the Terminal

You can close the terminal at any time by typing the command exit and hitting Enter.

For now, leave the Terminal open, so you can complete the rest of this lesson.

Optional Challenge

Test your Bash skills to:

Create a new directory called earth-analytics-bootcamp in your home directory (hint: cd, mkdir).

Check that earth-analytics-bootcamp has been successfully created in your home directory (hint: ls).

Change the directory to earth-analytics-bootcamp and create a new directory called data (hint: cd, mkdir).

Check that data has been successfully created in earth-analytics-bootcamp (hint: ls).