workshops

Introduction to the Unix Shell Part 1: Navigating Files and Directories

Setup

Before beginning this lesson, you’ll need to do a few things:

Overview

What is the Unix Shell?

Before introducing the Unix Shell, it may be valuable to start by introducing the graphical user interface (GUI). This is the mide widely used way to interact with a personal computer, and the one that you are most likely familiar with. The GUI allows you to see various icons and applications, and to use a mouse to navigate your way through the computational landscape.

source source

As opposed to this graphical interface that can be navigated with a mouse, the Unix Shell is a way of interacting with your computer by providing it with textual instructions. In this vein, you can think of the shell as a box in which you type instructions for your computer to execute.

It’s worth noting that there are different “brands” of shell that look different, but that work the same (more or less. The truth is actually more nuanced, but for the purposes of this session, you can think of the different types of shells working almost the same).

Regardless of the brand/look of shell, each shell begins with some information about where you are in the computer. As an example, these are 3 different shells, all in my personal documents folder:

Each shell also has a prompt, which indicates that the shell is waiting for input. The prompts differ across each type of shell, but can include symbols like ‘>’, ‘%’, ‘$’:

Going back to the concept of the shell as a box in which you type instructions to your computer to execute, it needs to be mentioned that it does not accept human language commands, and that it has its own vocabulary and grammar.

Why use the shell?

With this in mind, you might be thinking to yourself, “why would I ever want to use this?? I know how my computer works, and don’t need to learn a new language!” While you would be completely reasonable in thinking that, the shell does provide a much more streamlined approach to automating data-oriented tasks and sequences, and is also the easiest way to work with remote machines and high-performance computing clusters.

Consider the following scenarios:

You could do this manually, but it’s going to be a very long process, and the chance of making mistakes is quite high (and being able to tell if you’ve made a mistake is an issue in itself!).

source

Exploring Files and Directories

Before we jump into some commands, it’s worth defining two key terms:

Files: Objects on a computer that store data, information, settings, or commands used with a computer program.

Directories: Also called “folders”, are the units that hold files or other directories.

Paths

A path is a string of characters that specifies a unique location in a directory hierarchy. A file path specifies the location of a file in a computer’s file system structure.

The below picture illustrates the directory structure of a typical computer. Within this structure, the path to my Desktop would be:

/Users/Rochlinn/Desktop

When dealing with paths in Unix, directories are denoted by a slash “/”, and each “layer” of the directory heirarchy is separated with a slash.

Exercise

Take a look at the following directory structure.

Answer questions in Padlet

Unix Commands

To start off, here are a few useful commands to get started with navigating your files and directories. A valuable tip for Unix commands (and most coding languages), is to know that the internet has a lot of documentation of how to solve issues. When playing around with these commands, it’s highly recommended to use a search engine to help you!

pwd

ls

cd

cd..

cat

Absolute vs. Relative Paths

When navigating paths through your directories, there are two types of paths that can be used:

Absolute path: Includes the entire path from the root directory.


Relative path: Includes the path relative to where you are.

It should be noted that relative paths only work going down the hierarchy, and not up. In this example, the path already assumes /Users/Rochlinn/Music

Exercise

Using the data files that you downloaded at the beginning of the session, explore the directories and files and answer the following questions:

Answer questions in Padlet

References & Resources