In this class, you'll need some familiarity with the Unix shell. Those of you that took CS33 are probably somewhat familiar with it after spending several weeks writing one, but not everyone has done that, and the CS33 wasn't intended to teach how to use the shell efficiently as a user. Here are a few commands which you will probably find useful.

A first useful note is that each shell prompt will begin with a dollar sign, like so:


This is the shell's way of telling you it is waiting for more input.

Movement and file listing

Two of the most common commands are cd and ls. The former, which stands for change directory, is the command you use to enter other directories. Think of ls as short for LiSt. It will display the contents of the current directory when used without arguments, or the contents of any directories that you supply to it on the command line.

$ cd /course/cs0320
$ ls
admin/    asgn/  data/  handin/  ta/     webdev/
archive/  bin/   docs/  lib/     tabin/  www@
$ ls bin
ant@           old/          qtcreator@
cs032_handin*  priv_handin*
$ cd bin

The "punctuation" at the end of a file name tells you that a file is a directory (/), an executable program (*), or a symbolic link (@).

Commonly used flags with ls are -a, which lists all the files in the current directory including dot-files (which are usually "hidden" in the sense that ls doesn't show them), and -l, which lists things in long format, so you can see their permissions, owner, group, size, and date of last modification.

File operations

Sometimes, you want to move files around, copy them to different places, or get rid of them. You'll use mv (move), cp (copy), and rm (remove), respectively. mv will either rename a file, if it is given a file for the first argument and another filename which is not a directory for the second, or move files described by all arguments except the last into the directory given by the last argument. cp will copy the first argument to the second, or all arguments but the last into the directory specified by the last. rm will remove all files given as arguments.

$ ls
file1  foo/
$ mv file1 file2
$ ls
file2  foo/
$ cp file2 foo
$ ls
file2  foo/
$ ls foo
$ rm file2
$ ls
$ rm foo
rm: cannot remove `foo': Is a directory
$ mv foo bar
$ ls
$ rm -r bar
$ ls

As you can see, the rm command requires the -r (for recursive) flag in order to remove directories, but the mv command will simply move the entire directory somewhere else without need of a flag. Usually, when you delete files with rm they can't be recovered. However, on the department filesystem, you may be able to fetch it from the backups. If you mistakenly delete something, talk to a Sun Lab consultant to see if you can get it back.

Changing permissions

File permissions may seem complicated, but they are all in all not too difficult to understand. For checking permissions, you are probably going to make heavy use of ls -l, which can display permissions:

$ ls -l
total 16
-rwxrwxr-x 1 aisha aisha    46 Jan 18 16:48 file1
-rw-rw---- 1 aisha cs0320ta  63 Jan 18 16:48 file2

In long form, the first column is the bit string of permissions, which can be split into the owner's permissions, the group's permissions, and world permissions. In the first file, aisha is the owner, and aisha is also the group. The owner permissions are rwx, which means Read, Write, and eXecute, respectively. The same applies for anyone in the aisha group (which only contains the eponymous user). However, anyone else can only read or execute the file.

In the second file, the user and group only have read and write permissions, and everyone else has no permissions.

The command chmod (change mode) allows you to change a file's permissions if you are the owner of that file. For example:

$ chmod ug+x file2
$ ls -l
total 16
-rwxrwxr-x 1 aisha aisha    46 Jan 18 16:48 file1*
-rwxrwx--- 1 aisha cs0320ta  63 Jan 18 16:52 file2*

This chmod invocation specified ug+x, which added the eXecute permission for User and Group. You could remove it again for the group, with g-x, for example.

There are more complicated use cases, where you can give specific permissions to specific groups, or make one set of users' permissions the same as another.

Two other commands may be useful to you for managing permissions. chgrp changes the group of a file, and chown changes the owner.

Manual pages

If you are ever confused about a command's syntax or use cases, you can look it up in the manual pages (usually referred to as man pages). They can be accessed with the man command. Just type man and then the name of the command you would like to know more about. (For former CS33 students, many syscalls can be found in the man pages as well). If you want to do something but don't know the command to use to do it, then you can use apropos to search for a command based on some keywords. By default, it will look for commands whose short descriptions in the man pages contain any of the words you supply as an argument, but if you give it the -a flag, it will only return commands that have all of the words.

Further reading

More information about the shell can be found here