UNIX (unix) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, developed starting in the late 60s and early 1970s at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and others.
Many Unix-like operating systems have arisen over the years, of which GNU/Linux is the most popular. Below is a chart that maps the many flavors of UNIX operating systems:
There are many versions (often called “flavors”) of Unix (and Unix-like) operating systems, such as Linux, BSD, Solaris, and others. Even Mac OS is technically a UNIX system, because it was originally based on UNIX. As you will notice by looking at the graph, Windows is not a UNIX system, since it is based on the DOS kernel.
The computer science department at the University of Arizona primarily uses two UNIX operating systems: MacOS and Ubuntu (which is linux based). Most of the computer labs (GS 228, 930) have primarily mac machines. The department also has a number of Ubuntu linux machines, All of the programming assignments in this course must be written to work on a UNIX system. If you do not already own one, you can use the Mac and Ubuntu computers in the labs. Altenrately, you can try installing Ubuntu on your laptop or desktop and using that (Ubuntu is free!). Though, if you’ve never tried installing a linux operating system before, be warned - it can take a lot of time and messing with to get it working.
In this course, you are going to learn how to use a unix system via the “shell”. You’ve probably seen it before in the movies. A computer Nerd frantically typing away at a computer with a black screen with green text frantically flying down the monitor. In this class, you will become that person (OK, not quite, but you will learn how to use this interface). Using the shell might seem like a daunting idea. Isn’t that only for computer super geeks? Well no, actually.
Most people are used to using computers with the “point and click” interface. To browse files on the computer, we drag the mouse over the Finder icon, click, and are presented with a graphical view of our files and folders. To browse through them, we click, scroll, and look at icons with fines and their names. To use the internet, we drag our mouse over the Chrome icon, click, and are presented with a graphical internet browser. To navigate to a site, we click on the url text box, type in an address, and hit Enter
. This takes us to a graphical website, with buttons to click, videos to watch, etc. The “point and click” method of interfacing with our computers is the most common way of doing so. However, this is not the only way to interface with the operating system.
As you probably guessed, another way to do so is using the shell.
Most of us are probably familiar with the file system on our computers. Every windows, mac, and linux computer has some type of file system on it. A file system is a piece of software on your computer used to manage and organize the files and folders (directories) on your computer. Generally, file systems are hierarchical, meaning that their organization structure is a tree.
A single hard drive can have one or more partitions, and each partition can have a different file system on it. On a UNIX machine “everything is a file”. Programs that you run are files. Documents are files. Even directories are just a special type of file.
The below diagram given an example of an example UNIX file system.
Each file system has a “root” (/
) directory, which represents the top of the file system tree. All files on the computer are organized as directories and sub-directories in this root directory.
Files and directories can be specified using paths.
The absolute path of the root directory is /
.
The absolute path of index.html
is /home/jelkner/public_html/index.html
.
The absolute path of the network
directory is /etc/network
.
Try writing down some other absolute paths based on this diagram as a separate exercise.
Let’s walk through a comparison of browsing files in Finder vs browsing through files in the shell (using the Terminal app). On a mac, when you click on Finder to browse files, you will see a window that looks like this:
This is your “home” directory, which contains folders for your Documents, Photos, Videos, and more. If we double-click on the Desktop
folder, we see something like this:
We can take a similar set of actions using the shell. To open a shell on a mac, search for the application named Terminal
and open it. This will open up a new window with a blank shell, ready for use. When you do so, you will see a window looking something like this:
This is a new shell session. Each shell session has a “current working directory” which is the directory that the shell currently “resides” at. To figure out what the current working directory is, type pwd
and then hit enter:
a new session typically defaults to the current user’s home directory, which is why pwd
shows /Users/bddicken
(my home directory on my Mac). We can also take a look as the files and folders that currently exist in this directory by typing ls
and then hitting enter:
Look! All of the same files/directories that we saw when we first opened Finder are show here. Thus, we are currently in the same directory that the first finder window was displaying. In the finder example, we double-clicked on the Desktop
icon to view the files in that directory. How do we do that in the shell? Type cd
(stands for “change directory”) and hit Enter:
(BREAK: Taking a bazillion screenshots of the terminal window is hard. From here on out, most of my shell and code examples will just show the text in a code box)
Now, run the pwd
command again to make sure we changed directories:
Last login: Fri Dec 30 12:44:31 on ttys002
$ pwd
/Users/bddicken
$ ls
Applications Downloads Music dev
Desktop Library Pictures rc
Documents Movies Public teaching
$ cd Desktop/
$ pwd
/Users/bddicken/Desktop
$
Looks like we are now on the desktop!
Now, re-run ls
to see what files are in the desktop directory:
$ ls
ben-small.jpg ubuntu-14.04.5-desktop-amd64.iso
hagura.zip unetbootin-mac-625.dmg
Looks like it matches with the finder window!
What if we want to create a new directory?
Well, there is also a bash command for that, and it is mkdir
(short for “make directory”).
To make a new directory called test
we can run:
$ mkdir test
And now, when we run ls
, we will see:
$ ls
ben-small.jpg test unetbootin-mac-625.dmg
hagura.zip ubuntu-14.04.5-desktop-amd64.iso
We can cd
into this directory, and we will see nothing, because it is empty:
$ cd test/
$ ls
$
To create a new file (not a directory), the command touch
is used.
Running touch todo.txt
will create a new file named todo.txt
with no contents.
$ touch todo.txt
$ ls
todo.txt
We can create a few more:
$ touch a.txt b.txt c.txt
$ ls
a.txt b.txt c.txt todo.txt
We can also move files with the mv
command.
mv
can be used to move files from one directory to another.
A typical mv
command takes two arguments.
The first is the name of the file to move.
The second is the location/name of the file to move the file to.
It can also be used to change the name of a file.
For example:
$ ls
a.txt b.txt c.txt todo.txt
$ mv a.txt AAA.txt
$ ls
AAA.txt b.txt c.txt todo.txt
$ mv b.txt another-file.csv
$ ls
AAA.txt another-file.csv c.txt todo.txt
$
Great, files!
For now, we are not going to really do anything with these files.
We will learn more about how to view and manipulate file contents within bash in a later lecture.
However, we do want to clean up this little mess we have made.
After all, we don’t need a bunch of empty files and useless directories lying around.
The command rm
can be used to delete
files.
To delete all of these files, run:
$ rm AAA.txt another-file.csv c.txt todo.txt
$ ls
$
All gone!
We should also delete the test directory that we created.
We are currently inside of it though, at the absolute path /Users/bddicken/Desktop/test
.
To go back up one directory level (to /Users/bddicken/Desktop
), we can do cd ..
.
$ cd ..
$ pwd
/Users/bddicken/Desktop
We can delete the directory with the rmdir
command (stands for “remove directory”).
$ ls
ben-small.jpg test unetbootin-mac-625.dmg
hagura.zip ubuntu-14.04.5-desktop-amd64.iso
$ rmdir test
$ ls
ben-small.jpg ubuntu-14.04.5-desktop-amd64.iso
hagura.zip unetbootin-mac-625.dmg
Aaaaaand, it’s gone.
We just learned about several shell commands: pwd
, ls
, cd
, mkdir
, touch
, rm
, and rmdir
.
Combining these few commands can be used to browse through and manipulate the files on your operating system, just like clicking with your mouse in Finder.
Same task, different interface.
We only scratched the surface of some of these commands, but we will use them more as the semester goes on.
When we open terminal and get a new shell, what exactly is going on in the background? Terminal is just an application on our computer that opens up a window with text. Terminal starts up a new shell by running one of the shell programs on the operating system. Conceptually, a shell program is pretty simple. It takes user-typed text as input, processes that input, and executes the commands that the text represents.
So, what shell program does terminal use?
On most systems, including Macs, the default and built-in shell program is called bash
. This stands for “Bourne-Again SHell”. There’s a long history as to why it is named this way, but we won’t go into it. Feel free to Google if interested. There are other shell programs available that do the same basic task (IE, take user-types text as input, interprets the text, and executes tasks accordingly), but have features that differ from each-other. Another popular one, which is my personal favorite, is called zsh
. It works very similarly to bash
, but has a few extra handy features. In this class, we will stick with bash
.
In the shell, we can check with shell program we are using by executing echo $SHELL
:
$ echo $SHELL
/bin/bash
echo
is a new shell command! echo
simply prints out whatever text comes after the command. $SHELL
is a variable that contains the path to the current shell program executable (don’t worry, we will learn more about bash variables later on). Thus, when echo $SHELL
is run, it prints out the value of that variable, and /bin/bash
is printed. Thus, we know that we are using the bash
shell.
From this point on, instead of talking about “the shell”, I will refer to it as bash
, since that is what we will use for the remainder of this class.