Guide to the Unix command line

Prepared by Alexandre Becker, cs107 TA, Modified for cs107E by Pat Hanrahan

This course will use Unix for all the assignments. An important goal of the course is to familiarize you with the Unix programming environment. This includes the command line, text editors, and program development tools.

To begin, you should be familiar with the command line.

Are you familiar with the following commands?

  1. Do you know how to open a terminal window that is running a shell?
  2. What are the commands for changing directories and listing files in a directory? Do you know how the filesystem is organized in Unix?
  3. Do you know how to create, move, rename, and delete files and directories from the command line?
  4. Do you know how to create a text file using an editor such as vi or emacs? Do you know how to create a text file on your terminal?
  5. Do you know how to run a command with different options? Do you know how to find documentation about the options to a Unix command?
  6. Are you proficient with Unix text editors such as Emacs or Vi? If not, you should learn one of these editors. Stanford has good tutorials on both emacs and vi.
  7. Do you know about environment variables such as PATH? Do you know the purpose of PATH? Do you know how to change PATH?

If you don’t know how to do all these things, you will need to learn these skills.

Philip Guo, a former PhD student at Stanford. has produced some nice videos that introduce basic unix commands.

Unix commands

This tutorial walks you through the use of most basic Unix commands for navigating the file system, viewing files, and reading the manual pages.

Browsing the file system

Open a terminal. The terminal window will show:


What does this mean? The $ is called the “command prompt”. (Depending on what flavor of unix and your login profile, you may see a different prompt.)

Let’s try running our first command. Enter the ls command and then press the return key (or “enter” key):

$ ls
Documents  Downloads   Mail

What does this command do? It lists all the files and folders contained in the current directory. To include more details about each file, such as the type or the size, you can add the -l flag to the command:

$ ls -l

Then, you may need to change the current directory. Use the cd command to set a different directory as current:

$ cd Documents

Typing cd by itself, returns you to your home directory (the root of your personal file system). Wherever you are in the filesystem, you can print the full path of the current directory with the pwd command.

$ cd
$ pwd

Congratulations, you can now browse and display the files wherever you have access on your machine!

Creating folders and files

You may want to create a folder dedicated to cs107. To do that, go in the folder where you want to create a directory, and use the mkdir command:

$ mkdir Class
$ cd Class
$ ls
$ mkdir cs107
$ ls

You can create a new file ‘foo.c’ using your favorite editor (Emacs or Vim).

You can move the file to another directory with the mv source destdir command.

$ mv foo.c cs107

If destination is a file, the mv source destination command will rename the file:

$ cd cs107
$ ls
$ mv foo.c bar.c
$ ls

You can also use the cp source destination command to make a copy of the file with a new name.

$ cp bar.c foo.c
$ ls
bar.c foo.c

To delete folders and fies, use these commands:

Viewing a file

Besides opening a file in your favorite text editor (e.g. Emacs or Vim), you can view its contents with cat file or more file. Whereas cat will print the entire file in one go, more allows you to scroll up and down. You can use these keystrokes to scroll:

Using the man pages

Each command has a manual page that shows you how to use it and what its options are. You can view a man page using man command. This is one of the most useful commands to know!

$ man ls

Tip: you can use the same keyboard shortcuts to scroll the man page as in the more program!

Most commands also accept options to print out a short usage reminder. For example,

$ more --help

list all the options and keyboard shortcuts. If --help doesn’t work, try -h.