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:
Linux
A free operating system which runs on every type of computer. It is popular for web servers, super computers and forms the basis for Android phones and tablets.
OSX and iOS
Apple's operating systems for laptops, desktops, phones and tablets are all in the Unix family.
BSD
BSD is another free operating system, which is not quite so widely used.
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.
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:
Ubiquity
Different computer systems have very different graphical user interfaces, but command line interfaces have much less variation. For example, the GUI used by OSX, Android, and different versions of Linux are all completely different, but the command line interfaces are largely identical. If you learn to use the command line effectively, you can use that knowledge on a huge range of systems.
Stability
The command line interface in Unix systems does not change very frequently. Commands that were developed in the 1980s still work the same today. While new versions of GUI systems typically work quite differently, new versions of command line programs normally only add options, keeping existing usage intact.
Remote Access
Accessing remote machines is easier and more efficient when done via command line. While there are systems that allow for running GUI programs remotely, they can be difficult to setup and slower to use. Moreover, most server machines and super computers do not have GUI programs installed and can only be accessed through the command line.
Efficiency
Most people can type much faster than they can use a mouse. Many, but not all, tasks can be more efficiently completed by entering commands than by using a graphical interface.
Scriptability
Perhaps the biggest advantage of the command line is that complex or repetitive tasks can be automated with scripts. Command line programs on Unix all work with plain text, so different commands can be easily "chained up" together to build more complex actions. Generally, graphical programs won't work together unless they were designed to. Also, as programmers we can create new commands that work with the others.
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).
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.
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:
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.
This section contains some helpful tips for working at the shell.
Command History
The shell saves your previous commands, and you can scroll through that history using the "up" and "down" keys on your keyboard. This allows you to repeat commands that you have used in the past. You can also edit the commands before executing them. This is really handy for when you reat a command (like running a program) over and over. Also, if you mistype a command, you can go back and edit it rather than type it all from scratch.
Tab Completion
You can have the shell complete
your commands by hitting the "tab" key. For example, rather than type the
entire passwd
command, we could instead have typed pass
and
then hit the tab key to complete the command to passwd
. While that
does not make much difference in this case, it make longer commands faster to
type.
Tab completion also lets you find commands more easily. If you remember
that the passwd
command begins with "pas", but cannot remember the
exact spelling, you can type pas
and hit tab. The system will then
either complete the command for you, or tell you all of the commands that start
with "pas", if there are multiple.
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 Creative Commons BY-NC-SA 4.0 License.