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.[4]
[4] The command can be the name of a Unix program (such as date), or it can be a command that's built into the shell (such as exit). You probably don't need to worry about this! You can read more precise definitions of these terms and others in Glossary.
A Unix command may or may not have arguments. An argument can be an option or a filename. The general format for Unix command lines is:
command option(s) filename(s)
There isn't a single set of rules for writing Unix commands and arguments, but you can use these general rules 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, including those on Linux systems, also have options made from complete words or phrases and starting with two dashes, like ––delete or ––confirm–delete. When you enter a command line, you can use this option style, the single-letter options (which all start with a single dash), or both.
The argument filename is the name of a file that you want to use. Most Unix programs also accept multiple filenames, separated by spaces. 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 and who (shown earlier in this chapter), have arguments that aren't filenames.
You must type spaces between commands, options, and filenames.
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: you can tell sort 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
Another example is the mail –s option, shown in Section 6.5.2 of Chapter 6. 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 also can 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.
Unix 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 Unix commands in later chapters. This book's quick reference card has quick reminders.
Let's look at a sample Unix 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 Unix 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.
Get today's date. |
Enter date |
List logged-in users. |
Enter who |
Obtain more information about users. |
|
Find out who is at your terminal. |
Enter who am i |
Enter two commands in the same line. |
Enter who am i;date |
Mistype a command. |
Enter woh |
In this session, you've tried several simple commands and seen the results on the screen.
Copyright © 2003 O'Reilly & Associates. All rights reserved.