Wednesday, March 31, 2010

Efficient Bulk Processing: The Command Line

The most efficient way to do bulk processing (such as doing the same thing to lots of files) of the kind we need for simulations is at the command prompt. This post describes the basics of the command prompt.

What is a "Shell"?
A shell is a wrapper around the low level of the actual operating system. Linus/Unix users are generally familiar with shells. They even have a choice-the c-shell csh, the bourne shell (propriety), or it's more famous cousin, the "bourne again shell" bash (free), the tschell tsh, etc. In this post, I'll restrict myself to bash, as it is arguably the the most commonly used.

In the Windows world, it is not as well-known that there are also several shells available. The original, dinosaur is the MS DOS shell command.com. Windows XP introduced another shell, cmd.exe. Most recently, Microsoft has developed PowerShell, which is a substantially more powerful animal. I will discuss PowerShell in another post.

How Do I Get a Shell in Unix/Linux?
In the *nix world, this probably isn't an issue. Most users spend a good deal of time in the shell environment. If you are working in a desktop enviornment such as KDE or GNOME, you are looking for a program called called "terminal", "konsole", or "xterm".

How Do I Get a Shell in Windows?
In the ancient, pre-windows days, this simply wasn't a question. The computer booted and you were faces with the DOS command prompt.

In the Windows world, there are two solutions: one is to click "Start", then click "run", and type "command" (or "command.com" and hit return. A black box will open on the desktop.
The second way is for click "Start", then click "All Programs" "Accessories" and find the black-box icon called "Command Prompt". Clicking it will bring up the same black box.
Because We'll do a lot of work at the command prompt, it is worth having the command Prompt on your Windows desktop. Click on the icon and drag it onto the desktop. This will create a short-cut that you can double click.

Both bash and command are usually configured to display the current directory. You will see the current directory followed by the prompt symbol: "$" in bash, ">" in windows.

To move from place to place, we use the "cd" command for both operating systems. The only difference is that *nix uses a forward slash "/" to separate directories, while Windows uses a backslash "\". Sometimes Windows will accept a forward slash, but the rules for when it will and won't are more trouble than they are worth. When working at the Windows command line, just use the backslash and move on with your day.

To list the contents of the current directory, use "dir" on Windows and "ls" on *nix.
Navigate to the directory with your output, using the "cd" command. Say your output is a folder on your Windows desktop. The Windows command prompt opens in your home directory: usually
XP--typically c:\documents and settings\xxx
Vista and Windows 7--typically c:\users\xxx
where "xxx" is your user name (typically "owner" if you haven't changed it on a home machine, your login ID on a work machine.

To move around, use the "cd" command. this works on both *nix shells like bash and windows. To go down one directory, simply type "cd" , a space, and the name of the directory. Make sure to include the space, or you'll get an error.

NOTE: the windows command shell IS NOT case sensitive. The Unix/Linux shell IS case sensitive. Memorize that. Get used to it. And move on with your life.

Under XP you may see some rubbishy stuff before the ">" such as "C:\DOCUME~1\JDONOG~1>" Due to an old limitation of 8 characters for a name in the windows command prompt, the command prompt abbreviates long directory names with the first 6 characters, a tilde ("~") and a number. The number is generally 1, unless you have two or more folders that begin with the same name. In that case, you'll see "~2", "~3", etc., where the number indicates the order in which the folders were created. If you simply want to move to a directory with spaces in the name, like "Documents and Settings", "cd Documents and settings" will work. However, if you want to move down 2 directories, to "Documents and Settings\desktop", typing "cd Documents and settings\desktop" will yield an error.

This can be a pain. There are 3 simple solutions:
  • Move one directory at a time
  • Put the name of the directory with spaces in double quotes: eg. 'cd "Documents and settings"\desktop'
  • Figure out which is which is to use the "dir" command (which lists the names of the files in the directory). Say I have two folders, "Documents and settings" and "Documents to give to my cat to shred". I can type "dir DOCUME~1*" and the system will return "Documents and Settings", or type "dir DOCUME~2*" and get back "Documents to give to my cat to shred" Notice the aterisk at the end of the commands. Asterisk is a "wildcard" that will match any collection of characters. Using it is especially important for names that have periods in the ("jython-2.5.2"). It is important to include it.
We have covered moving down one directory ("cd child") and moving directly to a specific directory "cd \xxx\yyy". To move up on directory, we can make use of a special names, "." and ".." Whever you've type "dir" or "ls", at the top of the list are two entries with no names, just a period (".") and two periods (".."). These indicate special references. "." refers to the current directory, and ".." refers to the parent of the current directory. So, the command "cd .." will take you up one directory.

One final note for this post: If "Documents and Settings" is the only directory that begins with the characters "Doc", I can save myself some typing by just using "cd Doc*" and Windows will figure it out.

Next time we'll begin using this knowledge of the command line to extract specific information from an output file.