Which command will always change the working directory to the parent of the current location?

In this tutorial, you will access Bash/Shell through the Terminal, use basic commands in the terminal for file organization, and set up a working directory for the course.

At the end of this activity, you will be able to:

  • Open the Terminal.
  • Navigate and change directories in the Terminal.
  • Create an easy-to-use and well-structured project structure.

What You Need

You will need to have Git and Bash setup on your computer to complete this lesson. Instructions for setting up Git and Bash are here: Setup Git, Bash & Conda lesson.

How to Access Bash

Information below is adapted from materials developed by: U.C. Berkeley’s cs61a, Software Carpentry and The Hacker Within, Berkeley.

Get Started with the Terminal

In the early days of computing, the computer that processed data or performed operations was separate from the tool that gave it the instructions to do the processing. There was:

  • Terminal: which was used to send commands to the computer and
  • The computer: the hardware that processed the commands

Today, we have computers that can both provide commands AND perform the computation, and these computers have graphic interfaces (known as GUIs) that make it easy to perform tasks.

However, we still need to access the command line or terminal for certain tasks. In this lesson, you will learn how to use a command line interface (CLI) or terminal on your computer.

Which command will always change the working directory to the parent of the current location?
Image of an older computer and terminal.

About Bash

Bash is command line program that allows you to efficiently perform many tasks. The command line or terminal is where you provide Bash commands that the computer then executes. This allows you to control your computer by typing in commands entered with a keyboard, instead of using buttons or drop down menus in a graphical user interface (GUI) with a mouse/keyboard.

For example, you can use Bash to access and process files at the command line. Working with files at the command line is faster and more efficient than working with files in a graphic environment like Windows Explorer or Mac Finder.

In Bash, you can perform multiple operations on multiple files quickly. You can also write and execute scripts in Bash just like you can in R or Python.

Finally, you can use Bash to launch tools like Python, R and Jupyter Notebook. Throughout this class, you will use Python and Jupyter Notebook.

Data Tip:
Bash stands for “bourne again shell” and is an updated version of Shell. Sometimes you will hear Bash and Shell used interchangeably; in this course, we will use the term Bash.

You access Bash using a terminal program which we will discuss next.

What is a Terminal?

A terminal is the command line interface (CLI) that gives you access to Bash. There are many different terminal programs, and thus, the terminal program that you use on your computer will vary according to your operating system.

For instance, Mac computers have a terminal program already installed that provides access to Bash. However, on a Windows machine, you will have to install a new program to access Bash - like Git Bash, which we will use in this course.

For the rest of this course, we will refer to the command line environment that you use to access Bash as the terminal. For Windows users, this terminal can be accessed using a tool like Git Bash.

Why Use the Terminal?

Interact With Your Computer and Organize Files

Using Bash in the Terminal is a powerful way of interacting with your computer. GUIs and command line Bash are complementary - by knowing both, you will greatly expand the range of tasks you can accomplish with your computer.

You will also be able to perform many tasks more efficiently. Common tasks that you can run at the command line include checking your current working directory, changing directories, making a new directory, extracting files, and finding files on your computer.

Access Bash

How you access Bash will depend on your operating system.

  • OS X: The Bash program is called Terminal. You can search for it in Spotlight.

  • Windows: Git Bash came with your download of Git for Windows. Search Git Bash. For the rest of this course, even if you are on Windows, we will refer to the Terminal. You are using Git Bash as your terminal.

  • Linux: Default is usually Bash, if not, type Bash in the terminal.

Bash Commands

The dollar sign is a prompt, that shows you that Bash is waiting for input; your shell may use a different character as a prompt and may add information before the prompt.

When typing commands, either from these tutorials or from other sources, do not type the dollar sign only the commands that follow it. In these tutorials, subsequent lines that follow a prompt and do not start with $ are the output of the command.

Basic Bash Commands

  • ls: This command `lists all files in the current directory in alphabetical order, arranged neatly into columns.

$ ls Applications Documents Library Music Public Desktop Downloads Movies Picture

Your results may be slightly different depending on your operating system and how you have customized your filesystem.

  • pwd: This command prints the name of the directory in which you are currently working.
  • cd path-to-directory: The command followed by a path allows you to change into a specified directory (such as a directory named documents).

  • cd .. (two dots). The .. means “the parent directory” of your current directory, so you can use cd .. to go back (or up) one directory.

  • cd ~ (the tilde). The ~ means the home directory, so this command will always change back to your home directory (the default directory in which the Terminal opens).

  • mkdir directory-name: This command makes a new directory with the given name. The example below will make a new directory called notes inside of the documents directory.

$ cd documents $ ls documents data/ elements/ animals.txt planets.txt sunspot.txt $ mkdir notes $ ls documents data/ elements/ notes/ animals.txt planets.txt sunspot.txt

Notice that mkdir command has no ouput. Also, because notes is a relative path (i.e., doesn’t have a leading slash), the new directory is created in the current working directory (e.g. documents).

Data Tip: Directory vs Folder: You can think of a directory as a folder. However the term directory considers the relationship between that folder and the folders within it and around it.

Data Tip: Notice that you are creating an easy to read directory name. The name has no spaces and uses all lower case to support machine reading down the road. Sometimes this format of naming using dashes is referred to as a slug.

Project organization is integral to efficient research. In this challenge, you will use Bash to create an earth-analytics directory that you will use throughout this course.

You will then create a data directory within the earth-analytics directory to save all of the data that you will need to complete the homework assignments and follow along with the course.

Create a Directory for earth-analytics

Begin by creating an earth-analytics directory (or folder) in your home directory. This is the default directory in which the Terminal opens.

  • Create a new directory called earth-analytics.
  • Next, change your working directory to the earth-analytics directory, and create a new directory within it called data.

$ cd earth-analytics $ mkdir data

  • Last, go back to the home directory and confirm that you can then access the directories you just made.

$ cd ~ $ cd earth-analytics $ ls data

Set up Conda Environment Setup Git Bash & Conda

When you first login, your current working directory is your home directory. Your home directory has the same name as your user-name, for example, ee91ab, and it is where your personal files and subdirectories are saved.

To find out what is in your home directory, type

% ls

The ls command ( lowercase L and lowercase S ) lists the contents of your current working directory.

Which command will always change the working directory to the parent of the current location?

There may be no files visible in your home directory, in which case, the UNIX prompt will be returned. Alternatively, there may already be some files inserted by the System Administrator when your account was created.

ls does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information. They are hidden because you should not change them unless you are very familiar with UNIX!!!

To list all files in your home directory including those whose names begin with a dot, type

% ls -a

As you can see, ls -a lists files that are normally hidden.

Which command will always change the working directory to the parent of the current location?

ls is an example of a command which can take options: -a is an example of an option. The options change the behaviour of the command. There are online manual pages that tell you which options a particular command can take, and how each option modifies the behaviour of the command. (See later in this tutorial)

1.2 Making Directories

mkdir (make directory)

We will now make a subdirectory in your home directory to hold the files you will be creating and using in the course of this tutorial. To make a subdirectory called unixstuff in your current working directory type

% mkdir unixstuff

To see the directory you have just created, type

% ls

1.3 Changing to a different directory 

cd (change directory)

The command cd directory means change the current working directory to 'directory'. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.

To change to the directory you have just made, type

% cd unixstuff

Type ls to see the contents (which should be empty)

Exercise 1a

Make another directory inside the unixstuff directory called backups

1.4 The directories . and ..

Still in the unixstuff directory, type

% ls -a

As you can see, in the unixstuff directory (and in all other directories), there are two special directories called (.) and (..)

The current directory (.)

In UNIX, (.) means the current directory, so typing

% cd .

NOTE: there is a space between cd and the dot

means stay where you are (the unixstuff directory).

This may not seem very useful at first, but using (.) as the name of the current directory will save a lot of typing, as we shall see later in the tutorial.

The parent directory (..)

(..) means the parent of the current directory, so typing

% cd ..

will take you one directory up the hierarchy (back to your home directory). Try it now.

Note: typing cd with no argument always returns you to your home directory. This is very useful if you are lost in the file system.

1.5 Pathnames

pwd (print working directory)

Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your home-directory, type cd to get back to your home-directory and then type

% pwd

The full pathname will look something like this -

/home/its/ug1/ee51vn

which means that ee51vn (your home directory) is in the sub-directory ug1 (the group directory),which in turn is located in the its sub-directory, which is in the home sub-directory, which is in the top-level root directory called " / " .

Which command will always change the working directory to the parent of the current location?

Exercise 1b

Use the commands cd, ls and pwd to explore the file system.

(Remember, if you get lost, type cd by itself to return to your home-directory)

1.6 More about home directories and pathnames

Understanding pathnames

First type cd to get back to your home-directory, then type

% ls unixstuff

to list the conents of your unixstuff directory.

Now type

% ls backups

You will get a message like this -

backups: No such file or directory

The reason is, backups is not in your current working directory. To use a command on a file (or directory) not in the current working directory (the directory you are currently in), you must either cd to the correct directory, or specify its full pathname. To list the contents of your backups directory, you must type

% ls unixstuff/backups

~ (your home directory)

Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing

% ls ~/unixstuff

will list the contents of your unixstuff directory, no matter where you currently are in the file system.

What do you think

% ls ~

would list?

What do you think

% ls ~/..

would list?

Summary

Command Meaning
ls list files and directories
ls -a list all files and directories
mkdir make a directory
cd directory change to named directory
cd change to home-directory
cd ~ change to home-directory
cd .. change to parent directory
pwd display the path of the current directory

, © 9th October 2000