Tutorial: terminal basics

Workshop 1

The terminal

This brief command line tutorial follows up on the information provided in the lecture notes on “The terminal, conda, & python”.

The following cheatsheet shows the most important commands in both operating systems, Windows and Mac OS/Linux:

In addition to these commands, it is important to know that the command prompt represents the current directory with a dot ., and the parent directory with two dots .. . It is also useful to remember that you don’t have to manually type long directory names yourself, but that you can start typing the first letters of the name and then hit the TAB key for auto-completion.

The following two sections demonstrate some important tasks on the command line using the exercise from the lecture notes:

Exercise

Goal: Create a directory for our programming course at a location in your file system that suits you, for example:

/home/flo/documents/fhv/LV_scipro/
C:\Users\flo\Documents\fhv\LV_scipro


  1. Open a terminal and perform the following tasks from the terminal
  2. Change directory to the location in your file system where you want to create the new folder
  3. Create the new directory (pick a name that you like! I call it scipro for now)
  4. Create another directory called testdir
  5. Move testdir into scipro
  6. Verify that testdir is listed within scipro
  7. Move into scipro, then testdir, and then back out into scipro
  8. Delete testdir

On Windows

When we open a command prompt, the prompt is situated in the Home directory. On Windows, the Home directory most often looks similar to C:\Users\YourName. Let’s open a command prompt, and verify in which directory the prompt is:

In the screenshot, we see the start-up message of the prompt (this might look different in your case!), and the first command line in the second to last row. It reads C:\Users\knan>. The > sign tells us that the prompt is ready for us to type in a command. In the screenshot, the first command I typed is chdir, which asks the prompt to display the path of the current directory. The output that resulted from the command is printed in the last row, C:\Users\knan. The path displayed in front of the prompt sign > is identical to the output of the command. From that we learn, that the prompt always tells us in which directory it is currently located.

Let’s do tasks 2–4 from the exercise. That is, we first move to a location in the file system where we want to create a directory for our course. In this example, I will create the directory on the Desktop.

The first of the three commands changed the directory to the Desktop.

How do we know that the prompt actually changed to the Desktop?

We know, because the path in front of the prompt sign > of the second line changed to C:\Users\knan\Desktop.

The other two commands created two folders on the Desktop, one called scipro one called testdir.

The very first command I used earlier (chdir) was used stand-alone by itself. This time, the command to change directory to the Desktop involved two words. The first word is the command cd, followed by a space and then the directory where we want to go. Similar with creating a new directory. We tell the prompt that we want to mkdir (“make a directory”) with the name scipro.

Tasks 5 and 6 ask us to move the directory testdir into the directory scipro and verify that the task was successful.

The command to move testdir into scipro consists of three words. We can read it out loud like “move testdir into scipro”. The prompt confirms by tellung us that it moved one directory, and when we ask it to show us the contents of scipro, it displays some information about the storage disk, the file path of the directory, and then it lists three sub-directories. Hold on, why three? It shows the current directory (.), the parent directory (..), and then testdir. So, first of all, moving testdir into scipro did work! The two directories . and .. are always contained in every directory. They allow for easy and consistent navigation through the file system of your computer. To demonstrate this, let’s move into testdir and then back out of it into scipro. Remember that we are currently still on the Desktop. So,

The first command moves into scipro and continues straight into testdir. To achieve this, we separate the two directory names with a \. To move back out of testdir we cd into .., which represents the parent directory. The paths displayed in front of the prompt tell us that the navigation worked as expected and we ended up in scipro.

The last task asks us to delete testdir. The cheatsheet tells us that the correct command is rmdir. To make this exercise a bit more realistic and insightful, I added another file into testdir in the meantime. Let’s see whether rmdir does the job:

Upon rmdir testdir, the prompt tells us that the directory (testdir) is not empty. This is a safety mechanism that prevents you from accidentally deleting a folder that contains files or other folders. To delete a non-empty directory, you have to use del as you see in the screenshot.

Important

Files and directories deleted with del are gone forever. They don’t move to the “Trash” but are immediately deleted!

Programmers usually don’t use names for files or directories that contain spaces. However, since most of you likely have some directories that contain spaces, I am showing you how to deal with them:

If you read carefully up until here, I don’t need to explain anymore what the first two commands and their output mean. At this point I want to highlight the useful feature of command auto-completion. At the last prompt, I am typing cd Nam (i.e., the first three letters of the directory "Name with spaces"). Note that I even leave out the tick marks ". Now, when I hit the TAB key, this is what happens automatically:

You can cylce through your previous commands with the arrow buttons. Arrow-up will display the most recent command, and Arrow-down will bring you back to the empty prompt.

OK, so let’s move into this directory and then go back to our Home directory. To do this, let’s count how many directories we have to step out of. There is "Name with spaces" in scipro, which is on the Desktop. So, our Home directory is three levels up. That makes three .. and we can switch to Home like that:

I show you another trick, if you don’t want to count. But first, let’s got back into "Name with spaces" in one go:

You will reach your home directory from anywhere in your file system if you type

That concludes our basic command prompt tutorial. Make sure you understand how to navigate through your file system using the command line!

On Mac OS and Linux

When we open a terminal, the prompt is situated in the Home directory. On Unix-like operating systems, the Home directory most often looks similar to /home/YourName. Let’s open a terminal, and verify in which directory the prompt is:

In the screenshot, we see a terminal with a command prompt. It reads flo@steed:~$. The $ sign tells us that the prompt is ready for us to type in a command. In the screenshot, the first command I typed is pwd (print working directory), which asks the prompt to display the path of the current directory. The output that resulted from the command is printed out, /home/flo.

Let’s also understand the term that is displayed before the prompt sign $. flo is the username, and steed is the name of the computer. In between the : and the $, the prompt displays the current path. In the above case ~. The ~ is the Unix symbol for Home directory.

Let’s do tasks 2–4 from the exercise. That is, we first move to a location in the file system where we want to create a directory for our course. In this example, I will create the directory on the Desktop.

The first of the three commands changed the directory to the Desktop.

How do we know that the prompt actually changed to the Desktop?

We know, because the path in front of the prompt sign $ of the second line changed to ~/Desktop.

The other two commands created two folders on the Desktop, one called scipro one called testdir.

The very first command I used earlier (pwd) was used stand-alone by itself. This time, the command to change directory to the Desktop involved two words. The first word is the command cd, followed by a space and then the directory where we want to go. Similar with creating a new directory. We tell the prompt that we want to mkdir (“make a directory”) with the name scipro.

Tasks 5 and 6 ask us to move the directory testdir into the directory scipro and verify that the task was successful.

The command to move testdir into scipro consists of three words. We can read it out loud like “move (mv) testdir into scipro”. When we ask the prompt to show us the contents of scipro with ls, it lists testdir. So, moving testdir into scipro did work! Let’s do something slightly different:

We just used an option to the command ls, -a. This option lists all files and directories within scipro, so also the hidden files and directories. It shows the current directory (.), the parent directory (..), and then testdir. The two directories . and .. are always contained in every directory. They allow for easy and consistent navigation through the file system of your computer. To demonstrate this, let’s move into testdir and then back out of it into scipro. Remember that we are currently still on the Desktop. So,

The first command moves into scipro and continues straight into testdir. To achieve this, we separate the two directory names with a /. To move back out of testdir we cd into .., which represents the parent directory. The paths displayed in front of the prompt tell us that the navigation worked as expected and we ended up in scipro.

The last task asks us to delete testdir. The cheatsheet tells us that the correct command is rmdir. To make this exercise a bit more realistic and insightful, I added another file into testdir in the meantime. Let’s see whether rmdir does the job:

Upon rmdir testdir, the prompt tells us that the directory (testdir) is not empty. This is a safety mechanism that prevents you from accidentally deleting a folder that contains files or other folders. To recursively delete a non-empty directory with all its contents, you have to use `rm -r`` as you see in the screenshot.

Important

Directories deleted with rm are gone forever. They don’t move to the “Trash” but are immediately deleted!

Programmers usually don’t use names for files or directories that contain spaces. However, since most of you likely have some directories that contain spaces, I am showing you how to deal with them:

If you read carefully up until here, I don’t need to explain anymore what the first two commands and their output mean. At this point I want to highlight the useful feature of command auto-completion. At the last prompt, I am typing cd "Nam (i.e., the first three letters of the directory "Name with spaces"). Now, when I hit the TAB key, this is what happens automatically:

Note the / at the end of the path you want to change directory to. If you carefully read the previous screenshots you saw that I didn’t include the trailing / in my manual commands. In our cases, both approaches are ok.

You can cylce through your previous commands with the arrow buttons. Arrow-up will display the most recent command, and Arrow-down will bring you back to the empty prompt.

OK, so let’s move into this directory and then go back to our Home directory. To do this, let’s count how many directories we have to step out of. There is "Name with spaces" in scipro, which is on the Desktop. So, our Home directory is three levels up. That makes three .. and we can switch to Home like that:

I show you another trick, if you don’t want to count. But first, let’s got back into "Name with spaces" in one go:

You will reach your home directory from anywhere in your file system if you type cd alone or cd ~:

That concludes our basic command prompt tutorial. Make sure you understand how to navigate through your file system using the command line!

File paths

The tutorial above taught you basic directory management and navigation from the terminal. As an aside you should remember that Mac and Linux operating systems are built on a powerful terminal, while it is rather rare to own a Windows for its command line.

The main differences between the Windows and Mac/Linux terminals

There are many differences, like different commands and capabilities, but for our course, you only need to know two visual differences:

  • The command line prompt in Windows starts with a >, in Linux it starts with a $.
  • The paths of the file system are assembled with backslashes \ in Windows, but with forward slashes / in Linux.

Please be aware of those two differences, when you see lecture material from my computer (which uses Linux).

Absolute and relative paths

You have already learned about the meaning of . (current directory) and .. (parent directory) in the context of directory navigation. Can you guess what the terms absolute path and relative path mean?

An absolute path starts all the way at the highest parent level of the file system. For example, this could be the C:\ drive on Windows and it is the ‘root’ / on Mac and Linux. Therefore an absolute path on Windows reads like C:\Users\knan\Desktop\scipro and on Mac/Linux like /home/flo/Desktop/scipro. On Mac and Linux, the user’s home directory (e.g., /home/flo) can be abbreviated with ~ and then reads like ~/Desktop/scipro (given that I am logged in as user flo).

A relative path always starts relative to the current directory. So, it has to start with a directory or file name that is present in the current directory. Note that this could also be . or ... For example, .\path\to\a\figure.png (Windows) or ./path/to/a/figure.png (Mac/Linux).

Learning checklist