Assignment 1: The Unix Command Line

In order to use RTcmix and some other computer music programs, you should be familiar with the Unix command line.

Unix is a family of general-purpose computer operating systems derived from work at AT&T’s Bell Labs in 1969. (Bell Labs was also a hotbed of early computer music research, by the way.) By “Unix command line,” we really mean a program called “the shell” that lets you type in commands that control programs, edit text, manipulate files, etc., and see the results in textual form. There are many examples of shell programs: the Bourne shell (sh), the C shell (csh), the Korn shell (ksh), the Bourne-again shell (bash). Bash is the one used most frequently now and is the default in OS X (which is partly derived from BSD Unix).

Once you get the hang of working in the shell, it can be a very efficient way to work. The disadvantage is that it will strike most people as unintuitive. But it builds character!

Follow the steps given below for an introduction to the command line. You can do this on the studio Mac or any of the iMacs in the library. Although the commands below would work on any Unix system (a Linux box, the IU mercury webserver, etc.), those won’t have the TextWrangler program installed. If you want to do this on your own Mac, install the free TextWrangler program, which you can get here.

Using the Shell

  1. Launch the Terminal program from your Dock. (If this isn’t in your Dock, you’ll find it in /Applications/Utilities.)

    If everything is set up correctly, you’ll see a mostly-blank window with a line that begins with “Last login:” and a brief line of text ending in ‘$’. This line is the command prompt, and is where you type commands to be interpreted by the shell. The commands let you manage files (move, copy, delete, rename), run RTcmix scripts, and a million other things we won’t bother with.

  2. Before you can do anything, you have to know how to get around the file system — see files and folders, move from one place on the disk to another, etc. What follows is a mini-tutorial.
  3. To give a command, you type the command name, followed perhaps by space-separated arguments (such as file names), and then press the return key. Some commands will print results to the screen, then the prompt will return.
  4. Give the command ls, short for “list.” (Don’t forget the return key.) You’ll see a list of files and folders in your home directory, or the top level of your personal file space. (A directory is the same thing as a folder.)
  5. Give the command pwd, short for “print working directory.” The working directory is the directory that you’re currently in. You should see something like this, but with your user name instead of mine:
          /Users/johgibso
    This is a full path name. It lists, from left to right, the hierarchy of folders leading to the last element, which can be either a folder or a file. Each element of the path is separated by a slash, and the root, or highest level, of the hierarchy is represented by a single slash on the far left side.
  6. Now we’ll use the command cd, short for “change directory.” Type this at the command prompt:
          cd Documents
    followed by the ls command again. You’ll see a list of files in your Documents folder. If you get an error message like “command not found” after giving the cd command, make sure you type a space between “cd” and “Documents.”
  7. Give the pwd command again. You should see something like:
          /Users/johgibso/Documents
  8. Type this at the prompt:
          cd ..
    (That’s two periods with no space between them.)

    “..” is another name for the parent directory, one level up in the hierarchy: in this case, you change back up to your home directory.

  9. Now we’ll make a directory to hold your RTcmix scores.
          mkdir sco
  10. Give these other commands:
          cd sco
          ls
          touch dummy        (create an empty file called "dummy")
          ls
          mv dummy bogus     (rename the file to "bogus")
          rm bogus           (remove -- i.e., delete -- the file)
    If that last one asks whether you really want to delete the file, type a lower-case ‘y’ and press return. If it doesn’t ask, say goodbye to your file. It’s not in the Trash, it’s gone forever.
  11. Once we start using RTcmix, we’ll have to view and edit text files, because that’s what RTcmix scores are.

    More commands...

          cd
          edit blah
    cd without a directory name means to change to your home directory. The edit command opens a new file, named “blah,” in the TextWrangler text-editing program.

    Type some random stuff into the TextWrangler window, and save the file. Then return to your shell window in the Terminal program. (Type command-tab to get there without using the mouse.) Give these commands:

          ls           (you'll see the "blah" file among those listed)
          cat blah     (dump your text file to the screen)
          clear        (clear the screen)
  12. Sometimes you may need to move and copy files...
          mv blah Documents
    This moves the “blah” file into the Documents folder. (Note how similar this is to using mv to rename a file, as we did earlier. The difference in that case was that the second name was a file name, rather than a directory name.)
          cd Documents
          cp blah blather        (copy the "blah" file into a new file, "blather")
          mv blather ..          (move "blather" to parent directory)
          cd ..                  (change working directory to parent)
          rm blather Documents/blah
    You can remove multiple files with one rm command. “Documents/blah” is an example of a relative path name: you construct a path that is relative to your current location in the file system. You could’ve typed this (substituting your user name for mine) with the same result:
          rm /Users/johgibso/blather /Users/johgibso/Documents/blah
    You can see why having really long file names, or file names with embedded spaces or weird characters, would make life difficult for the shell user. You have to put quotation marks around filenames that the shell has trouble understanding.
  13. Here’s one tip that helps when you have long file names. Type a few letters of the file name, and press the tab key: the shell will fill in (autocomplete) the remaining letters. Try it...
          cd Do<tab>
    You’ll see the name expand to “Documents,” as long as you don’t have any other file names in the working directory that start with “Do.”



If you’re feeling your Unix oats, look through Prof. Hass’ Basic Unix Commands for Musicians.

Two of the commands I use most often are grep, which lets you search through many text files for a word or phrase, and diff, which lets you compare two text files. See if you can figure out how to use those.

If you want even more info about this command-line business, see:

©2009, John Gibson