Introduction to terminals
General considerations
Let’s start with a couple of definitions.
- Terminal A terminal is a program that provides a text-based interface to your computer. Trivially, it is the window where you type commands and see the output, acting as the communication interface between you and the system. The standard in macOS is Terminal.app, in Linux Ubuntu it is the GNOME Terminal, while in Windows it is generally the PowerShell.
- Shell A shell is the command interpreter that runs inside the terminal. It reads the commands you type, executes them, and displays the results. Common examples include bash and zsh.
You can see the relationship between a human user, a terminal, a shell, and a computer as two people talking in a room. One person is you, the other is the computer, and the room where you are talking is the terminal—a common space where you can directly interact. The shell is the common set of rules you both understand: your shared language. Depending on the operating system and your preferences, you can “speak” with a computer in different rooms and in different languages. Generally, terminal and shell are used interchangeably, although they are not the same.
Before the advent of the most common graphical user interfaces, most of the interactions with computers happened thorough a terminal with a keyboard. Nowadays, instead, most people interact with computers using a combination of keyboard and mouse/touch-pad by clicking on windows, files, etc. This is equivalent to using a terminal, simply the commands are interpreted as clicks or specific keystrokes, e.g., double-clicking on a directory to open it or pressing Del to delete a file.
In scientific sectors, the usage of the terminal is still very common. There are several reasons for this. One is that many scientific tools have been developed for being used within terminals. Another is that basically all super computers (like Baobab from the University of Geneva) are usually accessed online and have no graphical interface, only a terminal one. Most importantly, using a terminal (especially in a Unix/Unix-like environment like macOS or Linux Ubuntu) provides a universal common ground that works the same on laptops, servers, and high‑performance computing systems.
This tutorial is a brief introduction to common terminal vocabulary, that is, we will learn commands for a given shell. Here, we focus on commands available in both bash and zsh, two extremely widespread and popular shells.
Getting Comfortable
Start by opening your terminal apps.
Since this guide focuses on bash and zsh:
- macOS users → open Terminal.app
- Linux users → open the terminal distributed with your OS
- Windows users → do not use PowerShell (different language). Instead open the Windows Subsystem for Linux (WSL).
If we set-up together the WSL app, you should see that you are using some version of Linux Ubuntu as the first message when you launch it. Formally, for you this is equivalent as being on another operative systems (Linux Ubuntu instead of Windows) and you are already inside the terminal, as no graphical interface is shipped with WSL.
As few general notes before starting with the tutorial.
- When you read that you have to
runa command, it means that you have to type it within the terminal and to pressEnter. - Remember that you can always check online how to use a command and, by running
command --helpyou can access a brief explanation of the tool (e.g.cp --help). - You can use the
Tabkey on your keyboard for fast autocompletion of paths and commands.
Exercise 1: Where Am I?
Goal: Learn to check the current directory and its content.
Commands: pwd, ls
Without a graphical interface, you cannot see where you are located (Desktop? Downloads? Home? etc.) and what is inside the directory. You can check these by using the pwd (print working directory) and ls (list) commands, respectively.
Run
pwdThe command will answer with something of the form like
/directory1/directory2/final_directory. This means that, in this moment, you are insidefinal_directory(the last entry of the line), which itself is insidedirectory2that is insidedirectory1. Notice how the names of the different directories are separated by the symbol/. The core idea is that there is a starting directory, called root (indicated by the first symbol/on the left) that contains all directories. Then, like a tree from its root, all the following content of the computer is inside directories of directories. This is the standard organization of all computers (although in Windows Terminals like Powershell you might see\rather than/to separate directories).Very importantly, the output of
pwdis the path to your directory, that is, a unique combination of the position and name that gives the location of the directory in the computer.Run
lsThis command will show the content of the directory where you are (that is, the one given by
pwd) as a list of files. If it shows nothing, then the directory is empty.
Exercise 2: Going around
Goal: Learn how to change directories, and absolute & relative paths.
Commands: cd
To change a directory, you run cd (change directory) and insert the path of the directory you want to move to. Generally, when you open a terminal, you are in your home directory. From wherever you are, you can always run cd ~ or cd.
Enter your home
List what is in your home and access one of the directories you find with
cd name_directory.Move one directory up with
cd ..The
..is a general way to say one up. You can concatenate the points, i.e.,cd ../..means go two directories up,cd ../../..means three and so on. The single dot., instead, indicates this directory where you are now. For example, runningcd .moves you… nowhere, since you are telling the computer to move to where you are to where you are. While the single dot might seem (and it is) useless in the context of this command, it will be useful for others that you will encounter down the road.
This is a good moment to grasp the concept of paths. As said before, a path is a text string that tells the computer where a file or directory is located in the filesystem. Example of paths are
/home/user/Documents
Documents
./notes/todo.txt
../data/results.csvPath can be absolute or relative. An absolute path always starts from the root of the filesystem, represented by /. It is the full address of a file or directory and is valid no matter where you currently are. A relative path, instead, is defined in relation to your current directory, and it never starts with /. In the four examples above, the first is an absolute path to the Documents directory, while all the others are relative paths. This means that, wherever you are, if you type cd /home/user/Documents you will always be taken to that specific Documents directory. Typing cd Documents instead will take you inside the directory Documents contained in your current working directory, or will fail if the directory is not present. Similarly, ./notes/todo.txt and ../data/results.csv are relative paths to files, that is, the first means I am referring to the todo.txt file inside the directory notes inside . (this) directory and the second I am referring to the results.csv file inside the data directory contained in .. (one directory above me).
To make the difference more clear and state better the importance of this difference, let’s suppose you have two different Documents directories with absolute paths /home/bob/Documents and /home/alice/Documents. The command cd Documents will work both in bob and alice directories, but will take you to two directories that have the same name but different absolute paths, that is, they are different directories that will have different files, sub-directories etc. You can easily imagine that absolute paths are unique, as you can’t have files or directories with same name within the same folders (as in all computers).
You can play a bit with the widget below to familiarize with paths. In the widget you select a directory by clicking on it. The current directory is in bold blue, while in bold black are those accessible directly below from the current one. Absolute (abs) and relative (rel) paths are reported for all directories. You can see how absolute paths never change, while relative ones depend on the directory you are in.
Try navigating using both absolute and relative path in your terminal. Remember that you can always go back to your home with cd or cd ~.
Creating Things
Exercise 3: Create directories
Goal: Learn how to create a directory
Commands: mkdir
To create a new directory, you can run the command mkdir (make directory). Specifically, running mkdir name_directory will generate a directory with name name_directory in the directory where you are, equivalently to mkdir ./name_directory. You can use absolute and relative paths as well, e.g., mkdir ../name_directory to create it inside the directory containing the current one or mkdir /home/bob/Documents/name_directory to create it specifically at that path.
Create a directory called
playgroundEnter this directory
Inside it, create two directories:
animalsandplants. You can create multiple directories at once withmkdir name_dir1 name_dir2 name_dir3 etc...
Exercise 4: Make Some Files
Goal: Learn how to generate files Commands: touch
You can create files with the touch command.
Create three files:
dog.txt,cat.txt,elephant.txtin theanimalsdirectoryCheck that they exist using
lsWhat is their relative path? And their absolute one?
Go back two directories to the one containing
playgroundwithcd ../..and try to createbamboo.txtandbaobab.txtinsideplantswithout entering inplantsbut by using relative or absolute paths
Viewing & Editing
Exercise 5: Read Your Files
Goal: Learn how to visualize a file
Commands: cat, echo
You can access the content of a file by using the command cat. The simple version cat file will print the content of file on the screen. echo, instead, is used for printing something specific. echo "Hello World!" will print Hello World! on the screen.
Navigate to the directory containing the file
dog.txtDisplay the contents of
dog.txtusingcat. It should be emptyTry the command
echo "Hello World!"Add a line to the file using
echo "Dogs are friendly animals." > dog.txt
You will notice that the echo command is unchanged, but we added a > dog.txt at the end of it. This means *whatever is the output of the command before >, put it in the file dog.txt.
Exercise 6: Append Content
Goal: Learn output redirection (>>)
Append another sentence to
dog.txtusing>>, e.g.echo "your cool sentence" >> dog.txtDisplay the updated file with cat
Notice that > rewrites the content of the file, that is, whatever is contained in the files gets overwritten with the new input, while >> appends to the content of the file, i.e., it adds the input at the end of it.
Moving and Renaming Things
Exercise 7: Rename a File
Goal: Learn how to rename and move files and directories
Commands: mv
You can rename or move a file with the command mv (move). Example usages are mv a.txt b.txt, which renames file a.txt in b.txt, and mv a.txt ./Documents/, which moves file a.txt from the current directory inside the Documents directory. You can also combine them like mv a.txt ./Documents/b.txt which both moves and renames the file. The same can be applied to directories.
Rename the
elephant.txtintotoad.txtin the animal folderCreate
otter.datin the directoryplantsand move it in the directoryanimalsRename the
plantsdirectory inanimal_gardenand move it inside theanimalsdirectory. Try to do it in one command
Copying and Removing Things
Exercise 8: Make a Copy
Goal: Learn how to make a copy of a file or a directory
Commands: cp
You can copy files with the command cp (copy), e.g. cp a b will copy fle a in b. Remember that absolute and relative paths can be used everywhere you specify a file or a directory, for example cp ../a.txt /home/Documents/a_backup.txt will copy the a.txt file from the directory one above us to a file named a_backup.txt inside the directory /home/Documents/
Copy the
dog.txtfile both intodog.txtanddog_backup.txt. Which one works? Why?Copy the full directory
animalswithcp animals animals_2. Does it work?
Directories can’t be simply copied as they might contain other files, and you have to specify what to do with them. To copy a full directory you have to specify the flag-r(for recursive) so that the copy command knows that the order has to be repeated for everything contained inside the directory as well.Copy the full directory
animalswithcp -r animals animals_2. Does it work? What’s inside the directoryanimals_2?
Exercise 9: Delete Things
Goal: Learn how to delete a file or a directory
Commands: rm, rmdir
You can remove files and directories with rm (remove) and rmdir (remove directory).
There is no Recycling Bin. Everything that is deleted is lost, permanently. Double think before deleting stuff, and double check your commands. Removing files and directories from certain System directories can permanently damage the operative system of your computer, or you can easily delete one month worth of data analysis. As a safety rule, do not touch things that you are not sure what they are.
Create a directory called
poachinginsideplaygroundRemove it with
rmdir, e.g.rmdir poachingCreate it again, and inside it create two files,
rifle.txtandpoacher.txtRemove the directory with
rmdir. Does it work? Why?Now remove it with
rm, e.g.rm poaching. Does it work? Why?Remove the
rifle.txtfile with thermcommandAnd now remove the full
poachingdirectory with therm -rcommand. Here the-rflag option ofrmhas the same value as incp, i.e., recursive, so apply this command to this directory and recursively to everything that is inside it, that is, remove this directory and everything inside
Text editors
Goal: A first approach to text editors in a terminal
Commands: vim, nano
Until now, you saw how to add data to a file by using the echo command. This is a neat trick to store on file output from commands, but in general it is not very useful to open and modify files. There are several text editors available in terminals, and which one to use largely depends on the personal preferences. Two of the most common are vim and nano. These are complex software packages with a billion options, and sadly the learning curve is a bit steep, but the basics for opening, modifying and saving (a basic usage of a text editor) are very simple.
Open the
toad.txtfile in theanimalfolder withvimby runningvim toad.txtGo into insert mode by pressing
a(you will see the last line of the page change into –INSERT–)Type whatever you want. Remember, you have to move with arrows and keyboard as clicking on the terminal is not available
Press
Esc. The –INSERT– mode is not active anymore. You can now access the general options by typing:(you will see the symbol appear at the bottom of the page, outside the file text)To save the file, write
:w(write) and press enter. Now you can quit with:q(quit).Re-open the file with
vim. Go again inINSERTmode and type something. Now try to close the file without saving with:q. Does it work? Why?You can force the closure with the
!symbol, e.g.,:q!will close without saving any changes