Pincus Lab

Washington University School of Medicine

Department of Developmental Biology
and Department of Genetics

A Brief Tutorial on the Command Line

You are likely familiar with the graphical interface for browsing through the files and folders on your hard drive and disks, and opening applications. On the Mac, this interface is called the "Finder", and on Windows, it's "Explorer". However, both systems also provide a text-driven "command-line interface" for the same tasks, which is better suited to certain kinds of uses.

Now, the graphical interface provides windows that show folders and files. You could say that you're "in" a particular folder that's shown in the front-most window, and that you move from folder to folder as you navigate around, double-clicking folders, etc. The command-line interface uses this exact same metaphor; however it doesn't show any pictures. Nevertheless, there is still the notion of being "in" a particular folder, and tools to navigate from folder to folder, to manipulate files, and to run applications.

Files and Folders and Paths

Before getting started with these, though, a brief aside about files and folders is warranted. First, note that the terms "folders" and "directories" are interchangeable. Now, as you know, files and folders are structured hierarchically on every disk: every file and directory is contained within another directory, and so forth, up to the "root" of the system. Any individual file or directory can thus be uniquely identified by two things: (1) the file/directory's name, and (2) where to find that file/directory -- that is, the list of directories one needs to traverse to get to it. The latter is known as the "path" to a file or directory.

On a Mac (or any other UNIX-like operating system), a "path" might look something like this: "/Users/zpincus/Documents/manuscript.doc", which is to say that, on the main hard drive there is a folder called "Users", in which there is another folder called "zpincus" containing yet another folder called "Documents", containing a file called "manuscript.doc". (The slashes are the way that we separate out different components of the path on UNIX systems.) On Macs, files on other drives all live in the same basic hierarchy: "/Volumes/MyUSBDrive/revised.txt" refers to a file called "revised.txt" which is on a disk called "MyUSBDrive". (Other UNIX systems handle disks slightly differently.) The situation on Windows is similar, but slightly different. On Windows, a path to a particular document might look like: "C:\Documents and Settings\zpincus\My Documents\manuscript.doc", which is to say that on the main hard drive ("C:/", for historical reasons), there is a folder called "Documents and Settings", etc. etc. as above. A file on the removable drive "MyUSBDrive" might be at "H:\revised.txt". The main differences between Windows and the rest of the world are that different drives are identified with letters (which have nothing in particular to do with the "name" of the drive that you can see), and that the separator between path components is a back slash instead of a forward slash.

The paths described above are known as "absolute" paths: how to get to a file or directory from the top of the hierarchy. One can also use "relative" paths, which are how to get to a file or directory from some particular directory. In the above example, the relative path (on a Mac) from the "/Users/zpincus" directory to the document is just "Documents/manuscript.doc". Note that the relative path has no leading "/" (or "C:/, on Windows): this indicates that the path doesn't start at the top of the drive (which is called "/" or "C:/"), but relative to some particular folder. While absolute paths can only go "down" from the top of the drive, through the directory hierarchy, to a particular file, relative paths can also go "up". Suppose that there is a directory called "C:\Documents and Settings\zpincus\Desktop" -- what is the relative path from that directory to the aforementioned "manuscript.doc" file? It's "..\Documents\manuscript.doc" -- which is to say, "starting out in C:\Documents and Settings\zpincus\Desktop, go up one level to C:\Documents and Settings\zpincus, and then go to Documents and therein find manuscript.doc." On both Windows and Mac/UNIX systems, ".." means "one level up the hierarchy". (Note: "." means the current level of the hierarchy, so both "manuscript.doc" and "./manuscript.doc" are valid relative paths from "/Users/zpincus/Documents/" to the file of interest.)

Command Line Basics

Now we're ready to start! Where do we find this command-line interface? On Macs, open the program named "Terminal" in the "Utilities" folder, which is in the "Applications" folder at the top of the hard drive. (Or we could say, more compactly, open /Applications/Utilities/Termial). On Windows, run the program "Command Prompt", which is found in the Start menu, via Programs -> Accessories. (Or choose the "Run..." option and type in 'cmd'.)

In either case, you will be greeted with a prompt containing some information, but an otherwise blank window! Now what? First, let's ask where we are: on a Mac, type pwd (short for "print working directory"), and on the PC, type cd (short for "current directory"). In either case, the full path to the "current directory" -- the directory that you are currently "in" -- is printed.

To see a list of files in the current directory, type ls (Mac; short for "list") or dir (PC). To see a list of files of any directory, type ls or dir, followed by the path (relative or absolute) to the directory. for example,

ls /Users


dir C:\Documents and Settings

will print out the folders and files in those directories. Likewise, ls .. or dir .. will print out the folders and files in the directory "above" the current one in the hierarchy.

How about moving around the hierarchy? We can use the cd command: typing cd followed by the path (relative or absolute) of a directory moves us to that directory. (On Windows things work the same, with the odd exception that to move from one disk to another, you need to use cd /D instead of cd.

Making your Life Simpler

Of course, this is all something of a pain when we have the directory that we want to be in open in the graphical interface! Slowest would be to manually type cd followed by the path to that directory. Fortunately, there are a few shortcuts: type cd, and then either drag the folder onto the terminal window (Mac), or copy the path to the folder from the explorer window's address bar and paste it into the command window (with a right-click since control-V doesn't work) on Windows, and hit enter. Even easier, you can get system additions to open a command prompt with the current directory already set to the directory selected in the graphic interface. For Windows, download the "Open Command Window Here" Powertoy from Microsoft. After installation, you can right-click on a folder and select "Open Command Window Here". On Macs, try Open Terminal Here, which is a application that you drag to the toolbar of the Finder windows, and when you click on it, it opens up a Terminal in the same directory.

The command-line also has some tools to make things easier. Pressing up-arrow takes you to the previous command you typed, and down-arrow from there takes you to the next one. Far easier than re-typing a command that contained a tiny typo! Also, if you type the first few letters of a file name or path, and press the TAB key, the rest of the path will be automatically completed (if the prefix is unique), or the options will be cycled through with each tab-press (Windows and some other systems), or the matching file names will be displayed (this may take two TAB presses). Together, these tricks will save a ton of time -- make sure to use them!

Manipulating Files

Most commands take as input the name of a file or directory to do something with: for example, the ls command allows you to specify the directory to list. Other commands like cp (Mac) / copy (PC) take two paths as input: the source file and the destination file for the copy operation. (mv/move are similar.) In general, the input files to be operated on by various commands are simply listed on the command line after the command's name.

Now, there are certain shortcuts: suppose we are in a directory that contains some TIFF images and some JPEG images, and we want to delete all of the JPEG images. We could type rm (Mac) or del (PC), followed by the names of all of the JPEG images, but that would be laborious! Instead, we can simply type rm *.jpg / remove *.jpeg, and everything that ends with '.jpg' will be deleted. That is, the * is a "wildcard" which can match zero or more letters in any file name. It doesn't have to be at the beginning of the file name, either: supposing that we had manually prepended "bad" to the names of every picture we didn't want to keep, rm bad*.jpg would remove all files with names starting in "bad" and ending in ".jpg". Multiple levels work too, on Macs (and with celltool on PCs) rm Old*/backup* will remove any file with a name starting with "backup" in any directory located in the current directory with a name starting with "Old".

As we have seen, commands typically take file names as input. Sometimes we need to tell the command to do something different with the files, though: how do we provide additional input to a command in a way that is separate from the file names? This is done by specifying options, prefaced by a special character. For example, ls -l doesn't try to print the contents of the directory named "-l"; instead it interprets that as a directive to print extra information about the files. (On a PC, dir does this by default, dir /B turns it off.) The format that the options take differs: on UNIX-like systems, options are typically single letters after a hyphen (as above), or words after two hyphens (see below); on PCs options are typically specified as single letters after a forward slash. (Celltool uses the UNIX convention.)

Dealing with Spaces in Paths

Many command-line programs take as input a list of file names, separated by spaces. What, then, if a file has a space in it's name? How is the program to tell whether that space indicates the gap between two different file names, or one single name? In general, the command cannot tell (the exception is that on Windows, some commands which only take a single input path, like cd, know that if there's a space the path should still be treated as one). Thus, when spaces are part of the path, we need to put the entire path name inside quotation marks to "group" it together.

Things to know for Celltool

To use the celltool command from the command line, type celltool followed by the name of the celltool command to use. To see what celltool commands are available, type celltool --help. Most options have long and short forms: celltool -h and celltool --help do the same thing. To get help about a particular command, type celltool command --help (where "command" is replaced with the name of the command you want help for).

Further, some options require you to specify some additional information. For example, celltool plot_contours Data/*.contour --output=bacteria.svg asks celltool to execute the plot_contours command, taking as input all of the contour files in the Data directory, and to name the output plot "bacteria.svg". By itself, the "--output" option is useless, and so it requires the output file name to be specified after the equals sign. (Short form options don't use the equal sign: celltool plot_contours Data/*.contour -o bacteria.svg is equivalent to the above.)

Scripting Commands

It is possible to write multiple commands in a text file and execute those commands all together. On Windows, this is called a "batch file", and on Macs and other UNIX-like systems, it's called a "shell script". Briefly, on Windows, make a new text file and rename it to end in '.bat' instead of '.txt'. Then right-click on it and select "edit", and add the lines you want to execute. Double-clicking on this file from the GUI executes the contents of that file with the current directory set to that in which the batch file is stored. If you type the path to that file into the command line, the contents are executed in the current directory of the command prompt.

On a Mac (etc.), make a text file and put the commands you want to execute into them. To run these commands from the terminal, just type sh followed by a space and then the path to the file; then these commands are executed with a current directory the same as the one in the terminal. To execute commands by double-clicking them from the Finder, there are several additional incantations. First, rename the file so that it ends in '.command'. Then, from the terminal, type chmod a+x followed by a space and then the path to the .command file -- this makes the file "executable". Now, we also need to make the file be run by the "sh" shell-command interpreter as above, so the first line in the file should be #!/bin/sh. Finally, if we want the commands to execute in the same directory that the file is in (so that we can use relative paths in the commands), we need to add the following as the second line of the file:

cd "`dirname \"$0\"`"

(you'll have to trust me on this one...).