| Home | Classes | Papers | Essays | Short Fiction | About |

Table of Contents

UNIX

What is UNIX?

UNIX is an operating system (OS), or really a collection of OSs, which was originally developed at Bell Labs by in the Late 1960s by Ken Thompson and Dennis Ritchie1. UNIX was developed to be a successor to MULTICS which was the first time-sharing OS, also created by Thompson and Ritchie. UNIX caught fire because it was the first2 portable OS written—this was because it was written entirely in C.

Dennis Ritchie's name might sound familiar. This is because he is the Ritche from Kernighan and Ritchie's C Programming Language book!

What is an Operating System?

In order to understand what UNIX is, we first have to understand what an Operating System is in the first place. Simply put, an operating system is a special program that operates as a sort of middle man between the user and the hardware. Prior to the development of operating systems, users would interact with computers by submitting programs for execution one-at-a-time. If Alice needed neutron diffusion calculations and Bob needed to simulate blast wave propagation, each would have to write a separate program and wait in line to submit it. Once Alice's program finished, Bob's program could run. Since computers prior to the explosion of integrated circuits were expensive3 users would have to contend with one another for access time. This creates an obvious problem: the owner of a computer needs to have multiple technicians on staff dedicated to handling program submission requests.

The first "operating systems" were little more than hoppers. A technician would take a stack of punchcards representing a program from a user and place it on the queue. Once a previous program finished calculating, the next would be taken off the queue. This too presents problems like: what happens when the program crashes? Or, what happens if the program stalls? Eventually, programmers decided to come up with programs that could solve these logistical issues. And thus the Operating System was born.

Modern OSs provide more than just the ability to queue up a few programs. They supply powerful abstractions that developers are constantly making use of, whether they recognize it or not. When we start programming in C we'll find we have to do very little besides write in C to get something runnable. This is unfathomable without the intervention of an OS.

What is the UNIX Philosophy?

The UNIX philosophy was outlined by Brian Kernighan in 1984's The UNIX Programming Environment, "The power of a system comes more from the relationships among programs than from the programs themselves". What he meant was that UNIX provides many useful abstractions to allow for communication between multiple processes. Instead of one super program which is in charge of many different aspects of the system, UNIX is structured in such a way to allow for many specialized programs to do simple jobs well. Later in this lecture we'll see some practical examples showcasing this behaviour.

Is Linux UNIX?

Most people no longer directly use UNIX like they did fifty years ago. Instead, we use OSs heavily inspired by UNIX. In this course we'll be using Linux. Linux itself isn't a singular OS, it instead refers to a family of OSs which more-or-less share a kernel (key subroutines in an OS). There are many different distros (or distrobutions) of Linux: Ubuntu, Debian, Red Hat, Arch, etc. Each distribution has its own flavour and goals, but all are fundamentally Linux.

The name Linux is a portmanteau of the first name of its creator, Linus Torvalds, and UNIX. Linux + UNIX = Linux.

At Binghamton, we use Linux for a variety of reasons, but the most important is that it is open-source software. This means that anyone can take a look at the Linux source code. This makes it perfect for educational purposes! There's no guesswork when it comes to understanding how Linux works, we can simply take a tour through the actual code itself.

Using a UNIX-like Machine

Most students who take this course have never used Linux before and have also never used the command line. The purpose of this portion of the lecture, and the first lab, is to familiarize you with some terminology and get you a bit more used to interacting with a UNIX-like system.

Keep in mind, getting used to using the command line is not easy. You will have to work at it the whole semester. Take my word for it: it's worth the effort!

The Filesystem

Before we start looking at how to interact with data on Linux, we need to understand a little bit about the filesystem. A filesystem is an abstraction provided by the OS which organizes data stored in memory (often in secondary storage). This is not an OS course, so there's no need to get into how exactly that works here—we're just going to take for granted that it does.

                       /

   ___________________/\___________________
  /          /       / \       \     \     \
bin         dev    etc  usr    tmp  unix   boot
                       /|\
             _________/ | \_________
            /           |           \
          joe          mike   paul  mary
           /                  /\      /\
 hello_world.c            junk temp junk data

In Linux, all data is stored as a file. A file is a sequence of bytes. Files will contain meta-data, or information about the file itself (size, location, permissions, etc.). Files are arranged in a hierarchy as shown by Listing 1. There are special files in the filesystem called directories which are files that contain other files. In Listing 1, /, usr, and joe are all directories and hello_world.c is a regular file.

The Shell

The shell is a program whose key purpose is to provide the user with direct access to the underlying system. There are many different possible shell programs that can be loaded onto a Linux/UNIX-like machine. You may or may not be familiar with sh, bash, zsh, fish, pwsh, etc. All of which have their own pros and cons associated with them. For this class we will be focused on using bash terminals4.

A shell is a command interpreter. This means that when you're interacting with a shell program you are actually programming. Shell programs have their own languages that they use to understand the commands given to them by their users.

Opening up a terminal will be different for different OSs. If you're on a Mac device, you can follow along by searching for the "Terminal" application. Most Linux distros

When you open up a shell it will look something like the following:

josephraskind@josephraskind-ThinkPad-T470:~$ 

josephraskind is the user, josephraskind-ThinkPad-T470 is the name of the machine, and ~ is the shell's current working directory. The $ indicates the beginning of the prompt, or the command that you'll be sending in. Try typing ls into the shell and hitting ENTER. Here's what it looks like for me:

josephraskind@josephraskind-ThinkPad-T470:/tmp/example$ ls
homer

In Listing 6, I am in the subdirectory example/ of the tmp/ directory, which itself is a subdirectory of the root directory, /. What I see are the files contained within /tmp/example/ which happens to be a directory called homer. You can probably guess what ls is supposed to do, but you can doublecheck your guess by using the command man ls. When I do so my terminal changes to show the following output:

LS(1)                                           User Commands                                          LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information about the FILEs (the current directory by default).  Sort entries alphabetically if
       none of -cftuvSUX nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..
...

You can exit man with the "q" key.

man shows the man page, or manual entry, for commands/programs installed on the machine. We can see here that ls is a program that lists information about files! You'll also notice that the man page lists a whole bunch of "options" that can be used to modify the behaviour of ls. Try out ls -a and see what it prints out. Here's mine:

josephraskind@josephraskind-ThinkPad-T470:/tmp/example$ ls -a
.  ..  homer

You'll notice that ls now prints out two additional files: . and ... Believe it or not, those are also directories. Every single directory in Linux has those two directories contained within it. They represent connections to the current directory, ., and the parent directory, ... Watch what happens when I use ls ../:

josephraskind@josephraskind-ThinkPad-T470:/tmp/example$ ls ../
example

We can change directories using the cd command:

josephraskind@josephraskind-ThinkPad-T470:/tmp/example$ cd homer
josephraskind@josephraskind-ThinkPad-T470:/tmp/example/homer$ 

My working directory has now changed from /tmp/example/ to /tmp/example/homer. If I ls now I should see different files:

josephraskind@josephraskind-ThinkPad-T470:/tmp/example/homer$ ls
iliad.txt  odyssey.txt

cd is a built-in command whereas ls is a program. When ls is invoked the program must be turned into a process by the OS and loaded onto the CPU.

If I want to peak inside of the iliad.txt file I can use the head program:

josephraskind@josephraskind-ThinkPad-T470:/tmp/example/homer$ head iliad.txt 
The Project Gutenberg eBook of The Iliad

This eBook is for the use of anyone anywhere in the United States and
most other parts of the world at no cost and with almost no restrictions
whatsoever. You may copy it, give it away or re-use it under the terms
of the Project Gutenberg License included with this eBook or online
at www.gutenberg.org. If you are not located in the United States,
you will have to check the laws of the country where you are located
before using this eBook.
  • Additional Functionality

    Linux adheres to the UNIX philosophy by providing communication protocols that can be used to send information from one program to another. These come in two forms on the command line: pipes and redirects. Pipes pipe information from one program to another. Syntatically, we must place a | between the first in second program to indicate the direction of the dataflow. Here's an example where I count all of the lines in the Iliad where the word "water" is used:

    josephraskind@josephraskind-ThinkPad-T470:/tmp/example/homer$ grep -ie "water[ .,-;]" ./iliad.txt | wc -l
    12
    

    First I use the grep program to look for instances of "water" then I take the output of that program and send it to wc which counts the number of lines in the text it takes as input!

    Redirects can be used to send the output of a program that would normally be printed to the screen to go to a file instead. Here's an example where I use echo to print out a phrase to a new file:

    josephraskind@josephraskind-ThinkPad-T470:/tmp/example/homer$ ls
    iliad.txt  odyssey.txt
    josephraskind@josephraskind-ThinkPad-T470:/tmp/example/homer$ echo "The Simpsons" > simpson.txt
    josephraskind@josephraskind-ThinkPad-T470:/tmp/example/homer$ ls
    iliad.txt  odyssey.txt  simpson.txt
    

    echo normally prints out to the screen, but the redirect tells the bash shell to make sure it goes to the new file simpson.txt instead.

Exercises

  1. Using the man command, look up the pwd command. What does it do? Run it and describe the output.
  2. Navigate to your home directory using cd and use ls -a to list all files including hidden ones. How many hidden files (files starting with .) do you see? What do you think they might be used for?
  3. Create a directory structure that mirrors the filesystem diagram from Listing 1 — specifically the usr subtree with at least two user directories beneath it. Use cd and ls to verify your work.
  4. Use head and wc -l together in a pipe to count the number of lines that head prints by default. What is that number?
  5. The cat command prints the entire contents of a file to the screen. Use echo and a redirect (>) to create a file called hello.txt containing the text "Hello, UNIX!". Then use cat to verify its contents.
  6. What is the difference between /usr and ./usr? Think about what / and . represent before answering.

Footnotes:

1

There were more, but Thompson and Ritchie often get the lion's share of the credit.

2

This is not strictly true, but it was the first portable OS to be shared around more than a local lab group.

3

For example, the ENIAC cost about $8.9 million in today's money (2026).

4

There are two reasons for this. (1) bash is what is installed on the remote servers. (2) bash is the terminal scripting language I am most familiar with. It also happens to be incredibly popular, so you don't lose anything learning a little bit about it.

Contact: [email protected] | rss feed | Compiled with org-mode