Programming Digital Media

Basic Unix shell commands

When you start the Terminal application, you are looking at a program called a shell. The shell is a program that looks like the traditional model of a window into which you type commands and see their output. The major transition from Mac OS 9 to OS X was the use of a version of the Unix operating system instead of the traditional Mac OS. A shell program has been the traditional text-based mode of interaction with Unix from its beginning, and the Terminal is running a shell.

Different versions of shell programs have been created since programmers started running Unix in the 1970s. The shell that runs by default in Mac OS X is called the Bash shell. All shell versions have a core set of commands. A few commands will get you started, but we'll also need to talk about the ideas behind some of them.

Unix filenames

Files in Unix are organized into "directories". This is the same idea as "folders" on the Macintosh (or in the Windows copy of that idea). Since a directory can also contain another directory, a hierarchical structure can be created in the way that files are organized.

The file system has a base directory, called the "root", and is represented with a single forward slash character (/). Any file in that directory can be specified by preceding its name with the slash, for example, /usr. A directory in another directory uses the slash character to separate the names. For example, /usr/local/src means the file named "src" in the directory called "local" which is itself in the directory called "usr". Just from this filename, we can't tell if "src" is a directory or not, though /usr/local/src/ (with the trailing slash) would make it clear. A directory is just a special kind of file, so the way you construct filenames is the same both for regular files and directories.

Shell commands

There are several commands that are "built-in" to the shell, and will be available no matter which type of shell you are using. A shell command is executed by entering its name, followed by some number of arguments. The documentation for a command can displayed by using the man (for "manual") command. For example, to see the documentation for the man command itself, enter:
        man man 
Since Unix comes from the ancient times of slow teletypes, shell commands are typically very short, but are usually mnemonic for the activity that they perform.

Listing files in a directory

The ls command "lists" the files in a directory directory. For example, to see the files in the root directory, enter:
        ls / 
The ls command has a number of options that modified what it lists. For example, to see a "long listing" (including dates and the size of the files), use the -l option:
        ls -l / 
To see the documentation of ls, you can use the man command:
        man ls 
By default, ls will list the contents of the current directory if you don't specify the name of a file to list.

One very handy set of options lists the files in reverse chronological order, so the last file listed is the most recent that was created or modified:

        ls -ltr 
This is very useful when you are trying to figure out which file you modified last.

Your home directory

When you are assigned a Unix account, you are also assigned a primary directory for your use. This is called your "home" directory. We will put the software we write in this class in our home directory. Later, as we make images for the class animation project, we'll copy software and imagery from our home directory to a common directory we'll use for the project.

Your home directory has a special name: the tilde character (~). (Remember the value of brevity for anyone using a teletype.) You can use the tilde as a directory name. To see the size of a file called "notes" in your home directory, for example, you would enter:

        ls -l ~/notes 
If you want to refer to someone else's home directory, you follow the tilde character with their user name. Since my user name is "ack" (my initials), then you could use ls with my notes file as an argument like this:
        ls -l ~ack/notes 
If you try it, you'll get an error (since there isn't a file named "notes"), but you'll also see how the shell has turned "~" into a complete pathname for my home directory. The tilde character is also used in web addresses. For example, my webpage at CalArts was http://im.calarts.edu/~ack — my username was "ack", so "~ack" meant my home directory.

The current directory

At any point, the shell is said to have a "current directory". This is the directory that will be listed if you enter "ls" without specifying a directory. You can see what your current directory is with the command pwd ("print working directory", another name for the current directory):
        pwd 
You can refer to files in the current directory without including the directory name; its assumed that the file is in the current directory. This means that when you are editing files in a directory (one of your programs, for example), it's handy to make that directory your current directory so you can easily refer to files in that directory.

For example, if your home directory is the current directory, then you could see a long listing of the file "notes" like this:

        ls -l notes 

You refer to the current directory and the directory that contains it so frequently they have shortcut names. The current directory can be referred to with the period character (.). Because ls without arguments refers the current directory, the following commands are eqiuvalent:

        ls 
        ls .        
The directory "above" the current directory — its parent — has the shortcut name of two period characters (..). If you want to see the list of directories at the same level in the hierarchy as your current directory, then can list its contents like this:
        ls .. 
(In the Unix shell at least, brevity is still the soul of wit.)

Changing the current directory

The command cd changes the current directory to whatever you specify as an argument. For example, if you have a directory called "project1" in your home directory, you could make that your current directory like this:
        cd ~/project1 
Without arguments, cd will change the current directory to your home directory. Using this, you could change to the "project1" directory with these two commands:
        cd 
        cd project1 

Displaying the contents of a text file

The Python files that we will be creating are in plain text format — without special formatting codes — so we can print them out in the shell window. The Unix command for this is cat, short for "catenate". To print a file called blue.py to the screen, you would enter:
        cat blue.py 
This name doesn't seem to make much sense until you know that you can chain multiple files together in this way, and "catenated" means "connected in a chain."
        cat blue.py black.py circle.py 

Copying files

To copy a file, you use the "cp" command, supplying the current filename and the filename for the copy as arguments. For example, if I want to copy my file of notes to make a backup, I could enter something like this:
        cp notes notes.backup 
In Unix, there are few requirements on the structure of filenames, so I've added ".backup" to my filename as a personal convention I'll use when I make a backup copy of a file.

Creating a new directory

To create a new directory, you use the mkdir command. You specify one or more names for directories you'd like to create. For example, to create a directory called pdm in your home directory, you would enter:
        mkdir ~/pdm 
The rules for directory names is the same as for regular files, so if you don't specify the directory in which you want to create the new directory, the current directory will be used. For example, if you first make your home directory your current directory, you can specify the directory name alone to mkdir.
        cd 
        mkdir pdm 

Renaming files

To rename a file, you use the "mv" command (short for "move"). For example, if I wanted to rename my "notes" file to "notes1" (because I'm going to create a "notes2" file in a moment), I would enter:
        mv notes notes1 
There is now no longer a file called "notes", but a new file called "notes1" that is a perfect duplicate of file "notes". (Why "mv" instead of "rename" or "rn"? Probably because "rn" is too close to the way we delete files, "rm".)

Deleting a file

To delete a file, you use the "rm" command (short for "remove"). For example, to delete a file called "notes.old", you would enter this:
        rm notes.old 

Deleting a directory

To delete a directory, you use the "rmdir" command ("remove directory"). The directory must be empty. For example, to delete a directory called "current_notes", you would enter this:
        rmdir current_notes 

Running Python (a look ahead)

Once Python is installed, you can start it by entering the command "python" in the shell. It will print something like this:
        Python 2.3 (#1, Sep 13 2003, 00:49:11)  
        [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin 
        Type "help", "copyright", "credits" or "license" for more information. 
        >>>  
The ">>>" symbol is a prompt for you to type Python code, just like in a Unix shell. You can experiment with simple Python expressions this way. However, a Python program of any size at all will be saved in a file, usually with a filename that ends with ".py". You can run this file as if you typed it into Python by using the filename as an argument. For example, if I had a Python file called "project1.py", I could run it by entering this command to the Unix shell:
        python project1.py 
We'll talk about supplying arguments to our Python scripts in the Python script tutorial next week.

Command summary

man command Show the manual page for command.
ls List the files in the current directory.
ls directory List the files in directory.
ls -l directory List the files in directory with extra information.
ls -ltr directory List the files in directory with the newest files last.
cat filename(s) Output the file or files to the terminal window.
pwd Print the name of the current directory.
cd Change the current directory to your home directory.
cd directory Change the current directory to directory.
cp existing-file new-file Copy existing-file to new-file. The original file still exists.
mv existing-file new-file Rename existing-file to new-file. The original file no longer exists.
rm filename Delete file filename.
rmdir directory Delete directory directory. The directory must be empty.
rm -r directory Delete directory directory including everything it contains. Be careful!

An OS X addition to the shell: open

As in any operating system, different files are associated with different programs, usually the program that created it or is use to display it. For example, HTML files are associated in this way with web browsers. In OS X, the open command takes a file as an argument, and starts the application associated with that file. For example, if you open an HTML file, then (depending upon your browser configuration), a browser will be loaded with that HTML file and displayed.
        open tutorial.html  
You can open the Finder in this way. Since the current directory is called ".", you can create a Finder window for the current directory with:
        open . 
When we make image files, the open command will by default load that image into Preview if the type of the file is one that Preview can interpret.

Other Unix shell tutorials

This tutorial is another overview of the simpler Unix commands. (Windows users are told in this tutorial to use telnet, but Cygwin is a popular Unix-like shell for Windows that can also serve as a substitute.)

Another Unix and shell tutorial goes into more detail. It describes how to make terminal windows in Linux, but the principle is the same. At the bottom of the first page is a link to a description of a history of Unix.