Basic Command Structure
Basic Command Structure
Chances are that most of the commands you learn in Mac OS X will be written by a different programmer but all conform to some kind of “standard” structure that will make it easier to interact with. For instance, most programs you’ll use will probably need you to provide it with some kind of basic information. Be it a filename, an action, the number of times you need something done, or whatever. Very rarely will you just be typing the command all by itself.
The intention of this page is break down what is being typed on the command line and some tips for what to look for when something has gone wrong.
Let’s look at the most complex command we’ve seen yet:
#> ls -a -l
In this example, ls is our command. The -a and -l portions are called switches or options. A switch will alter a programs behavior, sometimes slightly, sometimes dramatically. In our case -a is a switch to enable listing of all files. And -l switches on long listing. Most programmers try and correlate the letter used for the switch to the first letter of what it means: -a for all files, -l for long list.
Not all switches are single characters. One of the most helpful options you can try when first starting with a program is the --help option. Most programs will output some kind of description on how to use the program when you use this option, but your milage may vary. If it doesn’t have a real help option, it will likely produce an error and spit out some kind of information about how the program should be run.
Another thing that they try and do is use a switch that has been used by other programs frequently. We should see a few examples of this later.
Switches
Arguments
When we were changing directories from one to another we had to specify which directory we wanted to go into. That value is called an Argument. These are generally files, directories, or other commands that you are passing to the command you are calling that it will then act on.
#> cd ~/Documents/
In this example, cd is our command. We are passing a single argument to cd, which in the case is the path to the directory we want to go to: ~/Documents/
Some programs will allow multiple arguments and will run the action on each argument specified. This can be tested with the ls command:

Right after listing the contents of ~/Documents, it lists the contents of ~/Music.
Switches can even have their own arguments that further specify how that option should function. The head command will read output the contents of a file starting at the top and going until it reaches the number of lines you specify. It can be run as such:

So there is one argument, test.txt, which is the name of the file we would like to read. There is one switch, -n, which says we would like to see a certain number of lines. And an argument, 5, to that switch which specifies the number of lines.
Capitalization, Spaces, and Special Characters
Finder makes it simple to type long file names with upper and lower case, spaces, and any number of random non-alphanumeric characters. It makes things friendlier, and easier to read. When typing something in the terminal everything must be typed case-sensitively.