Unix command lines
can be simple, one-word entries such as the date
command. They can also be more complex; you may need to type more
than the command or program name.[1]
A Unix command can have arguments . An argument can be an option or a filename. The general format for a Unix command line is:
command option(s) filename(s)
There isn’t a single set of rules for writing Unix commands and arguments, but these general rules work in most cases:
Enter commands in lowercase.
Options modify the way in which a command works. Options are often single letters prefixed with a dash (
-
, also called “hyphen” or “minus”) and set off by any number of spaces or tabs. Multiple options in one command line can be set off individually (such as-a -b
). In some cases, you can combine them after a single dash (such as-ab
), but most commands’ documentation doesn’t tell you whether this will work; you’ll have to try it.Some commands also have options made from complete words or phrases and starting with two dashes, such as
--delete
or--confirm-delete
. When you enter a command line, you can use this option style, the single-letter options (which each start with a single dash), or both.The argument
filename
is the name of a file you want to use. Most Unix programs also accept multiple filenames, separated by spaces or specified with wildcards (see Chapter 3). If you don’t enter a filename correctly, you may get a response such as "filename: no such file or directory” or "filename: cannot open.”Some commands, such as
telnet
andwho
(shown earlier in this chapter), have arguments that aren’t filenames.You must type spaces between commands, options, and filenames. You’ll need to “quote” filenames that contain spaces. For more information, see Section 3.1.
Options come before filenames.
In a few cases, an option has another argument associated with it; type this special argument just after its option. Most options don’t work this way, but you should know about them. The
sort
command is an example of this feature: you can tellsort
to write the sorted text to a filename given after its-o
option. In the following example,sort
reads the file sortme (given as an argument), and writes to the file sorted (given after the-o
option):%
sort -o sorted -n sortme
We also used the
-n
option in that example. But-n
is a more standard option; it has nothing to do with the final argument sortme on that command line. So, we also could have written the command line this way:%
sort -n -o sorted sortme
Don’t be too concerned about these special cases, though. If a command needs an option like this, its documentation will say so.
Command lines can have other special characters, some of which we see later in this book. They can also have several separate commands. For instance, you can write two or more commands on the same command line, each separated by a semicolon (
;
). Commands entered this way are executed one after another by the shell.
Mac OS X has a lot of commands! Don’t try to memorize all of them. In fact, you’ll probably need to know just a few commands and their options. As time goes on, you’ll learn these commands and the best way to use them for your job. We cover some useful commands in later chapters. This book’s quick reference card has quick reminders.
Let’s look at a sample command. The
ls
program displays a list of files. You can use
it with or without options and arguments. If you enter:
% ls
you’ll see a list of filenames. But if you enter:
% ls -l
there’ll be an entire line of information for each
file. The -l
option (a dash and a lowercase letter
“L”) changes the normal
ls
output to a long format. You can also get
information about a particular file by using its name as the second
argument. For example, to find out about a file called
chap1, enter:
% ls -l chap1
Many Unix commands have more than one option. For instance,
ls
has the -a
(all) option for
listing hidden files. You can use multiple options in either of these
ways:
%ls -a -l
%ls -al
You must type one space between the command name and the dash that
introduces the options. If you enter ls-al
, the
shell will say “ls-al: command not
found.”
The best way to get used to the Terminal is to enter some commands. To run a command, type the command and then press the Return key. Remember that almost all Unix commands are typed in lowercase.
Here are a few to try:
Task |
Command |
---|---|
Get today’s date. |
|
List logged-in users. |
|
Obtain more information about users. |
|
Find out who is at your terminal. |
|
Enter two commands in the same line. |
|
Mistype a command. |
|
In this session, you’ve tried several simple commands and seen the results on the screen.
Get Learning Unix for Mac OS X, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.