2020 December 28,

CS 311: Unix Tutorial

Learning Unix is not a goal of this course, but you have to know a tiny amount, just to get your graphics work done. This tutorial covers just what you need, and a little more, that you might want.

Getting a Shell

In macOS, go to your Applications folder, then the Utilities subfolder. Launch the Terminal application. I recommend that you keep this application in your dock.

In Windows running the Ubuntu Linux application, a shell is what pops up when you start the application. You're ready to go.

In Ubuntu Linux, you can obtain a shell by pressing Control-Alt-T. Or you can click the grid of nine dots in the lower left corner of the screen, search for the string "terminal", and click on the icon that pops up.

Moving Around: pwd, ls, and cd

Your computer's file system is a huge tree of directories (also called folders) and files. The root node is the directory /. Within it there might be several other directories, one with a name such as Users (in macOS) or usr (in Linux). Within that directory might live your home directory, whose name is your username; for example, mine is jdavis. So the path from the root to my home directory is /Users/jdavis.

As you navigate around your file system tree, your working directory is the directory that you are currently inspecting. The command pwd, which stands for "path to working directory", tells you your working directory.

The command ls, which stands for "list", lists the contents of the working directory, including subdirectories and files. Every directory contains two special, hidden items. The first item is called ., and it represents the directory itself. The second item is called .., and it represents the parent directory.

The cd command, which stands for "change directory", lets you change your working directory. You can change to a subdirectory, or to the parent directory .., or to any other directory (for which you have permission).

Because your home directory is so important, it has a special symbol: ~. So, for example, cd ~ immediately takes you home, wherever you are in the tree.

The following transcript illustrates these concepts and commands. Click to enlarge.

pwdlscd screenshot

The tab completion feature can greatly accelerate your work in the shell. For example, if you want to do cd Documents, then type something shorter — maybe just cd Do — and press the tab key. If there is only one file or directory that you could mean, then it is immediately typed for you.

By the way, if a directory name or file name contains a space, then you need to escape the space with backslash. For example, to change to a subdirectory named "My Documents", do cd My\ Documents.

Executing Programs

pwd, ls, and cd are examples of programs that you can execute. In general, whenever you type a command line into the Unix shell, the shell assumes that the first item in the line is a program. Then it must find the program and execute it. To help it find programs, the shell maintains a list of paths, that specify directories where programs tend to be kept. This list of paths is collectively called your search path or (confusingly) just your path.

Usually, the working directory is not in your search path. (This is a security measure. It is intended to keep you from accidentally running code that you just downloaded.) To run a program in your working directory, you must explicitly signal that intent using the . symbol. See the following example.

aout screenshot

Managing Files: mv, cp, and rm

The following commands are not essential, because you probably have a graphical file browser, in which you can do them more comfortably. Do whatever works for you.

The mv command lets you move a file from one directory to another directory, possibly with a different name. You can also use it to rename a file, because that's just a move within a single directory. The cp command lets you copy a file from one place to another. The rm command lets you remove (delete) a file.

mvcprm screenshot

Miscellany

Sometimes you want to view or edit files, for which you don't have permission. For example, this course's setup instructions ask you to install files far outside your home directory, inside the directories that the operating system uses for its own needs. If you try to mv myText.txt /usr/local/bin, then the shell refuses. But if you have administrator privileges on your computer, then you can force the command by entering sudo mv myText.txt /usr/local/bin. After you enter your password, the command is executed. Use this power sparingly, because you can damage files if you don't know what you're doing.

To make a directory called "myStuff" in the working directory, enter mkdir myStuff.

Programmers use a lot of text files, and sometimes they want to see what's in a text file without leaving the Unix shell. The cat command, as in cat myFile.txt, prints the file. For long files, it might be more convenient to use the more command, as in more myFile.txt. This command lets you browse the file using the arrow keys. Press Q to quit.

The man command, which is short for "manual", shows you a help page for any of the standard programs. For example, enter man ls to learn more about the ls command.