Topic: Issuing UNIX Commands
In an increasingly GUI-centered computing environment, the UNIX command line is unfamiliar territory for many people. People who have used other command-driven operating systems, such as VMS or MS-DOS, may be used to the command line but accustomed to specifying command options and arguments differently.
A few guidelines for commands are helpful, along with some terminology that is commonly used in manual pages when describing how to use different commands. Terminology first:
- White-space: Spaces or tabs, typically, that separate each part of a command. Technically white-space also includes form-feed, newline, carriage return, and vertical tab characters, but when entering commands we're usually limited to space and tab.
- Option: Something that changes how a command behaves. Sometimes options are merely turning some behavior on or off; other options may specify non-default locations to work in, names, or other information.
- Argument: Usually the "thing" a command is being applied to. If the command is an editior, for example, the argument is probably going to be the name of the file to be edited.
The Concept
In a GUI world when you want to edit a document, you double-click its iconic representation, an appropriate program is started (based on the type of file you have), and the file is opened for editing. In a UNIX command-line world things work the other way around; you identify the editor (by which command you use) and then tell the editor which file you want to edit. I tend to think "Process (how) What" when I put together a UNIX command. Process determines which command I use, (how) defines which options I need, and What is the file or information I am working with.
The Nitty Gritty
Let's start with a simple example:
ls -al /tmp
The command, which always appears first, is ls. It is separted from the options, -al, by whitespace, in this case a single space. The argument, /tmp, follows the options, also separated by white-space. Using my "Process (how) What" approach this would be List (all files, long format) in the directory /tmp.
Most UNIX commands expect options to have a single dash, and allow multiple options to be clumped together (without any intervening white-space) following a single dash (this style of option specification is called Unix98). Our example could have been issued as:
ls -a -l /tmp
with the exact same result. Note that
ls -a l /tmp
is not the same. The l will not be interpreted as an option if it is separated by white-space from the -a. Instead, it will be treated as an argument, in this case the name of a file you wish to have listed.
Here's a more complex example, and one that introduces a different way of specifying options:
ls --all --format=long --sort=extension
Options preceded by two dashes are called GNU long options. This format is much more readable and is prefered for scripting or other programmatic applications where readability is more important than the amount of time it takes to type in the command. The last option of --sort=extension introduces one style of option that needs information. When using GNU long options there shouldn't be any spaces between the option name (sort) and its value (extension).
Some commands that use Unix98-style options may also need information with some options. One example that comes to mind is the tar command:
tar -cf tmp.tar /tmp
The f option specifies the name of a file to create as the archive, so the file name follows the option, separated by white-space. "Process (how) What" is "Process a tape archive (create as a file) the /tmp directory" Some commands omit the space:
lpr -Plaser1 -T "Latest Draft Version" document.txt
Commands in the lp system are notorious for their mix of option formats. The -P insists that the printer name follow without any separation, while most other options allow white-space. Notice that the information for the -T option, since it contains white-space itself, must be quoted. Without the quotes, only the first word would be used as the document title and the remaining two would be interpretted as files to print.
One other common style of options is BSD. Options in this style are specified with no leading dashes:
ps aux
In the long history of UNIX and the commands it includes, a few non-standard ones have crept into the mix and stayed. The find command is one that comes to mind; it expects arguments to be listed first, followed by an expression, which is basically a list of options. When in doubt, consult the manual page.
Back to UNIX Command of the Day
|