Basic Unix - Part I
Jean-Yves Sgro
Updated: December 7, 2016
(modification and clean-up in progress - November 17, 2024 - fos1)

1 Introduction

This workshop is meant to learn and understand basic line commands as they are typed on a text terminal for a Unix-style operating system (Linux or Macintosh, or Windows with added software.)
This workshop will be loosely inspired by the Software Carpentry class The Unix Shell.1
We will learn "daily commands" i.e. commands that are useful for "every day life" on the computer such as creating files and directories, editing simple text files etc.

2 Set-up: Login to server

Using your NetID credentials login the FPU.
If this is the first time you use this computer some quick set-up will occur.
When the FPU ID screen appears answer the security questions.
Shortly after you will be logged in as <username> on the computer.

2.1 Terminal

The workshop will be conducted on FPU server a flavor of Unix we will first start with a terminal.

2.0 Hard drive organization.

It should be noted that while bin is a standard name for most if not all systems, Users is a newer label and older system might have a different name or a different position within the tree. The common name of directories is available online2.
On most modern systems the Users directory will contain the directory with "your name on it" containing your data and your files. We will explore this further below with the "home directory" . We will learn that we also use the "forward slash" or / to separate the name of successive directories within the hard drive "tree" of directories.
For user nelle that would be /Users/nelle where the leading slash represents root.
Note: Therefore there are two meanings for the / character. When it appears at the front of a file or directory name, it refers to the root directory. When it appears between names, it?s just a separator.

4 Command-line operation: The Shell

Before the development of graphical user interfaces (GUI) the command line interface (CLI) was the only way a user could interface with the computer.
A program called the shell takes commands typed on the keyboard and transfers them to the operating system (OS) for execution:

The shell transfers commands.

The Bourne Again SHell or bash shell is currently the default and most widely used shell, but other shell programs do exist and are available on a typical Unix-based system. These include: ksh, tcsh and zsh.
In this workshop we will only use bash.

5 The prompt: $

The $ is called "the prompt" a word with many definitions but one of the definitions in the Merriam-Webster dictionnary summarizes it well:
"verb: to move to action : incite"
In other words it is an "invitation" from the computer for you to give it a command.
Since the Terminal invites us for a command, let's see if we can check the name of the shell we are running.
At the prompt type the following command with the word shell in uppercase preceded by an attached $ sign which has a different function than the static $ prompt. This will be reviwed in a later section.
echo $SHELL
/bin/bash
You can see that the shell is actually a software that resides within the bin directory.

6 Username: whoami

In the Software Carpentry class The Unix Shell we follow Nelle Nemo -- (Nemo is Latin for "nobody" so she is probably not related to the famous Captain Nemo...).
Her username on the system is nelle and some commands refer to that.
Since you logged-in as you earlier on the iMac you probably know or easily guess your user name on this system. However, we'll learn our first command to verify that you are indeed logged-in as you!
The command is whoami and will echo on the screen your user name. Type the following command, press return and see who the bash shell thinks you are:
whoami
jsgro
The result is my username...
Of course for you the result will be different!
Commands:
More specifically, when we type whoami the shell:
1. finds a program called whoami,
2. runs that program,
3. displays that program?s output, then
4. displays a new prompt to tell us that it?s ready for more commands.
7 Files and Directories
7.1 Home and working directories
7.1.1 Home directory
We already looked at the hard drive organization above and we could see that nelle (or you) will have your files stored in a directory named after your user name and contained within the Users directory. For Nelle Nemo that would be /Users/nelle which would represent her "home directory".
When you first login or open a new terminal you "land" inside the "home directory" wherever it may be located within the hard drive.
Note: There is a very convenient short-cut to signify the home directory which can be symbolically represented by the tilde symbol ~ usually the key below the esc ecape key on most English-based keyboards.
In the next paragraph we'll learn how we can know in which directory we have "landed" or, if we have changed directory, know in which directory we are currently.

7.1.2 Working directory:

For now we only know that we should be in the "home directory" but later we'll navigate in and out of directories that we'll create or copy. It's easy to get lost! Therefore knowing which directory you are currenly "looking into" is very useful.
The command for that is pwd or print working directory:
pwd
/Users/jsgro
This is my "home directory" and you will see your "home directory" when you run this command.

7.1.3 Changing directory

We don't have any other directory that we know of yet but we can learn this command already since "it will always bring you home" if you are lost.
To change directory we use the command cd which is made from the first two letters of the English phrase "change directory" as is the case for many other shell commands.
cd
We will use this command later to move in and out of directories.

7.2 Listing content directories: ls

Specific, empty standard directories are created wihtin the "home directory" when a new user is added on a Macintosh computer. These directories are empty. Since we landed in the "home directory" we can ask to see them with the command ls short for the word list:
ls
You should see something like this on your terminal:
Last login: Tue Mar 22 08:50:34 on console
BIOCWK-00875M:~ jsgro$ ls
Desktop     Downloads   Movies    Pictures
Documents   Library     Music     Public
BIOCWK-00875M:~ jsgro$ 
ls prints the names of the files and directories contained within the current directory in alphabetical order, arranged neatly into columns.
If you have used this computer before there might be other files or directories.
We can update the file structure tree figure to reflect the content of your home directory, the top levels are simplified for clarity:

Hard drive organization for YOU.

Of course, on the Macintosh itself you can use the graphial user interface (GUI) to look at the content of your directories. However, the purpose of this tutorial is to learn how to use the line command interface (CLI) which is useful when connecting to a remote computer that cannot be controlled with a GUI but only with the CLI.
Since these directories are new, for now they don't contain anything.
Exercise: verify that the directories are empty.
(Hint: ls.)

7.2.1 Command flags

We can modify the behavior of most shell commands by adding flags, for example the flag -F tells ls to add a trailing / to the name of directories (but not files.) Since we only have directories within the current folder all output will be flagged.
Note that there is a white space (of any length) between ls and -F to separate the two words. Without the space, the shell would think that we?re trying to run a command called ls-F, which doesn?t exist.
ls -F
Desktop/  Downloads/  Movies/ Pictures/
Documents/  Library/  Music/  Public/

7.2.2 All (hidden) files

You saw above that the directories are "empty" if you were a new user to this computer because they were freshly made. However, all directories contain within themselves at least two hidden files.
The flag to see all files is -a
ls -a Documents
.       ..      .localized
We can also use it together with the flag we already know.
Note that there is no space between them. (An alternate notation would be ls -a -F with a space between each of the - dashes.)
ls -aF Documents
./      ../     .localized
Therefore we discovered that there are three hidden files in the directory. If you want you can check that all other directories harbor the same files even though they are "empty."
The .localized files are empty files specific to the Macintosh. They are used when a user has changed the default langage within the "International" preferences to reflect their local langage. For example a computer set to French would change on the fly the names Users and Library to their French equivallent of Utilisateurs andBiblioth?que.3
However, the two most important items are . (dot) and .. (dot dot) which are directories themselves as shown by the trailing /.
They are the symbolic representation of the "current directory" (dot) and the "parent directory" (dot dot).
Notation
Spoken Name
Definition