Using Unix

CMSC 141: Introduction to Python • a hands-on walkthrough

These pages are for students new to working at the terminal. Work through the sections in order and, by the end, you should feel comfortable with the handful of commands you need to get your coursework done.

You do not have to memorize everything in one sitting. Come back anytime to refresh. The more comfortable you are at the terminal, the faster the rest of the course goes.

Unix is basically a simple operating system, but you have to be a genius to understand the simplicity. Dennis Ritchie

Opening a terminal

All of the commands below are typed in a terminal window. On macOS, open the Terminal app. On Windows, open PowerShell or the WSL terminal. Keep the terminal and your browser side by side so you can try commands as you read.

If this course uses a shared server, your instructor will give you the one-line ssh command to log in. Otherwise, the terminal on your own laptop works fine for everything here.

The dollar sign $ below is just the prompt. You don't type it; you type everything after it. Your prompt may look a little different, and that's fine.

$ pwd
/home/student

The pwd command ("print working directory") shows where you are. When you first open a terminal you usually start in your home directory, /home/student in these examples (yours will use your own username).

Directories (aka folders)

Your files live in a tree-like structure. Here is a small slice of one:

/ ├── bin/ ├── etc/ └── home/ └── student/ ├── cs141/ │ └── labs/ │ ├── 00/ │ └── 01/ └── Documents/

The full path to student's home directory is /home/student, and the full path to the 01 lab directory is /home/student/cs141/labs/01. The pwd command always tells you where in this tree you currently are.

Try this example! Let's make a directory called testdir to experiment in:

$ mkdir testdir

The mkdir command makes a new directory. If it worked, there's no output at all, which is normal for Unix.

Changing directories

Use cd ("change directory") to move around, and pwd to check where you landed.

Try this!

$ pwd
/home/student
$ cd testdir
$ pwd
/home/student/testdir

The cd testdir moved you one level down into the directory you just made. To go back up one level, use cd .. Do it enough times and you reach the root of the tree, /:

$ cd ..
$ pwd
/home
$ cd ..
$ pwd
/
$ cd
$ pwd
/home/student
cd by itself always takes you back to your home directory (see the last cd above).

Listing your files

The ls command ("list") shows the files and directories in the current directory. You can also list another directory by giving its path.

Try these!

$ ls
cs141  Documents  testdir
$ ls testdir

The first ls shows what's in your home directory. The second shows what's inside testdir, which is empty for now since you just made it. In the next section we'll copy a file into it.

Adding -l (a lower-case L) gives the long format, with the owner, size, and permissions for each file:

$ ls -l

Copying files

The cp command copies a file. The form is cp [file] [newfile], which makes a copy of [file] named [newfile]. For example, to back up a program before you change it:

$ cp cubs.py backup-cubs.py

Try this! Move into testdir and copy in a starter file. Your instructor will tell you the exact shared path; here we'll call it /shared/cs141:

$ cd testdir
$ cp /shared/cs141/cubs.py cubs.py
$ pwd
/home/student/testdir
$ ls
cubs.py

The ls now shows a copy of cubs.py in your testdir.

If you ever feel lost, a plain cd puts you right back in your home directory.

One more to try! Copy a second file, and use a dot . for the destination. The dot is shorthand for "name the copy the same as the original, here in the current directory":

$ cp /shared/cs141/white_sox.py .
$ ls
cubs.py  white_sox.py

Viewing files

Sometimes you just want to see what's in a file without editing it. Two commands do this: cat and less.

The cat command ("concatenate") dumps the whole file to the screen at once. Fine for short files:

$ cat cubs.py
$ cat white_sox.py

For a long file, use less to view it one page at a time. Press the spacebar to page down and q to quit:

$ less white_sox.py

Editors

There are several text editors available. In this course we use VS Code. Some people prefer vim or emacs. Your instructor will say which editor to start with; any of them will open and edit your .py files.

Renaming or removing a file

The mv command ("move") renames a file. The form matches cp: mv [file] [newfile]. For example, to rename badname.py to cubs.py:

$ mv badname.py cubs.py

You can also move a file into another directory with mv [file] [directory].

The rm command ("remove") deletes a file, for example rm white_sox.py. There is no undo.

Examples to try!

$ cp /shared/cs141/white_sox.py sox.py
$ ls
$ mv sox.py white_sox.py
$ ls
$ cat white_sox.py
$ rm white_sox.py

These commands copy a file, rename it, show its contents, and finally delete it. When you remove a file it may ask remove white_sox.py?, to which you answer y or n.

Logging out

When you're done, close the terminal with Ctrl-d (hold Ctrl and press d), or type exit. If you're working on a shared lab machine, log out of the session when you finish so your files stay safe and the machine is free for the next person.

Summary

The commands from this walkthrough, in one place:

pwd print your current directory ls list your files cd change directory mkdir make a new directory cp copy a file mv rename or move a file rm remove a file cat print a file to the screen less view a file one page at a time

For a fuller list, see the Useful Unix Commands reference.