Home CPSC 225

Introduction to Unix


 

What is Unix

The term "Unix" refers to a large family of operating systems. The first version of Unix was created by AT&T Bell Laboratories in the 1970s (the same group which created the C and C++ programming languages).

Today, there are several operating systems in the Unix family which share the same philosophy and design. These include:

Altogether, Unix runs on most web servers, all super computers, most phones and tablets and some laptop and desktop computers. The only widely used operating system not in the Unix family is Microsoft Windows. Though you can still apply everything you learn in this class on Windows with the help of tools such as WSL.

In this course, we will focus on Linux, though nearly everything we do will apply equally to the other systems.


 

The Command Line

In this course, we will learn to work with programs over the command line. When using the command line, you issue commands to the computer by typing them in, as opposed to using a graphical user interface.


A typical command line terminal.

While it may seem old-fashioned, there are many reasons to learn to use the command line effectively:


 

SSH

As mentioned, a benefit of command-line systems is that they allow remote access. For this class, we will be connecting to the computer science department server. This will be done using a protocol called Secure Shell (SSH).

SSH allows you to connect to, and interact with, a remote machine over the command line. SSH consists of a server which runs on the remote machine, and a client which runs on your local machine:

The SSH client sends your commands to the server where they are carried out. The commands are encrypted so that they cannot be read as they are sent over the network.

When you are connected to a machine over SSH, the commands you enter are not run on your own local machine. They are run on the machine you are connected to. Likewise, when you edit files over SSH, those files are not stored on your computer, they are on the server. Using SSH allows you to use a computer any where in the world as if you were sitting right next to it.

SSH is widely used in industry. For instance web developers use it to log in to web servers hosting their sites, data scientists use it to log into super computers to run analyses, and it can also be used to access embedded systems without displays (such as a Raspberry Pi).


 

Connecting to the CPSC Server

There are different SSH clients available depending on the operating system of your local machine. You can see instructions for setting up SSH clients either Windows or OSX/Linux below:

No matter which SSH client you use, note that it's possible to open up multiple SSH windows at one time. Rather than do everything in one window, it often is easier to write a program in one window, and run it in another.

The linked instructions above also contain optional instructions on connecting over SSH using keys instead of a password. The advantages of this are that you do not need to type your password to login, but also that keys are generally more secure. Using a key is like using a super-long extra secure password which is entered automatically for you.

SSH keys come in pairs: there is a public key and a private key. The public key can be read by anybody and is placed on the server you want to log into.

The private key must be kept secret. If anybody gets a hold of it, they will be able to login as you and access your files. You should generally only set up SSH keys to login from systems which are secure. For example, setting up SSH keys on your personal laptop makes sense, but doing so on a shared lab computer might not be.


 

Entering Commands

Once you have logged in, you should see something like this:


Logged into cpsc.umw.edu.

You are now running a shell on cpsc.umw.edu which is ready to take your commands. The text that you see at the bottom ("ifinlay@cpsc:~$" in my example) is called a command prompt or just a prompt. The shell will take commands that you type at the prompt and execute them.

You can now enter commands which will be carried out on the cpsc.umw.edu server. For example, you can type the 'date' command, and then hit the Enter key to see the current time and date:


Result of the 'date' command.
 

Anatomy of a Command

The date and passwd commands as we entered them are very simple, "one word" commands. Many commands take arguments that they operate on (like parameters to a function), and options (also sometimes called flags which control how the command works.

For example, the cal command can be used to display a calendar on the command line. It can be used without arguments or options in which it will simply display the current month, with the date highlighted:

ifinlay@cpsc:~$ cal
     June 2018        
Su Mo Tu We Th Fr Sa  
                1  2  
 3  4  5  6  7  8  9  
10 11 12 13 14 15 16  
17 18 19 20 21 22 23  
24 25 26 27 28 29 30  

However, we can also give cal a month and year as an argument. So if I was curious what day of the week I was born on, I could enter the following:

ifinlay@cpsc:~$ cal 10 1984
    October 1984      
Su Mo Tu We Th Fr Sa  
    1  2  3  4  5  6  
 7  8  9 10 11 12 13  
14 15 16 17 18 19 20  
21 22 23 24 25 26 27  
28 29 30 31

Here, "10" and "1984" are arguments to the cal command. For a look at a historical anomaly, try passing the arguments "9" and "1752" to cal!

cal also accepts several options. Options in Unix commands begin with the hyphen character. For example, the -y option tells cal display the whole year instead of just the current month:

ifinlay@cpsc:~$ cal -y
                            2018
      January               February               March          
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
    1  2  3  4  5  6               1  2  3               1  2  3  
 7  8  9 10 11 12 13   4  5  6  7  8  9 10   4  5  6  7  8  9 10  
14 15 16 17 18 19 20  11 12 13 14 15 16 17  11 12 13 14 15 16 17  
21 22 23 24 25 26 27  18 19 20 21 22 23 24  18 19 20 21 22 23 24  
28 29 30 31           25 26 27 28           25 26 27 28 29 30 31  
                                                                  

       April                  May                   June          
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7         1  2  3  4  5                  1  2  
 8  9 10 11 12 13 14   6  7  8  9 10 11 12   3  4  5  6  7  8  9  
15 16 17 18 19 20 21  13 14 15 16 17 18 19  10 11 12 13 14 15 16  
22 23 24 25 26 27 28  20 21 22 23 24 25 26  17 18 19 20 21 22 23  
29 30                 27 28 29 30 31        24 25 26 27 28 29 30  
                                                                  

        July                 August              September        
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7            1  2  3  4                     1  
 8  9 10 11 12 13 14   5  6  7  8  9 10 11   2  3  4  5  6  7  8  
15 16 17 18 19 20 21  12 13 14 15 16 17 18   9 10 11 12 13 14 15  
22 23 24 25 26 27 28  19 20 21 22 23 24 25  16 17 18 19 20 21 22  
29 30 31              26 27 28 29 30 31     23 24 25 26 27 28 29  
                                            30                    

      October               November              December        
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
    1  2  3  4  5  6               1  2  3                     1  
 7  8  9 10 11 12 13   4  5  6  7  8  9 10   2  3  4  5  6  7  8  
14 15 16 17 18 19 20  11 12 13 14 15 16 17   9 10 11 12 13 14 15  
21 22 23 24 25 26 27  18 19 20 21 22 23 24  16 17 18 19 20 21 22  
28 29 30 31           25 26 27 28 29 30     23 24 25 26 27 28 29  
                                            30 31

Some options require arguments themselves. For example the "-m" option to cal requires the month as an argument. So to check the date of Christmas, we could use the following command:

ifinlay@cpsc:~$ cal -m 12
   December 2018      
Su Mo Tu We Th Fr Sa  
                   1  
 2  3  4  5  6  7  8  
 9 10 11 12 13 14 15  
16 17 18 19 20 21 22  
23 24 25 26 27 28 29  
30 31                 

Unix commands let you combine arguments and options which gives the command line so much flexibility.


 

Using the Shell

This section contains some helpful tips for working at the shell.


 

Exiting

Finally, to exit the shell, you can simply enter "Control-D". That is, hold the control key and tap the "D" key on your keyboard.

You can also enter the exit command to exit the shell.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.