Syntax of UNIX Command Lines

UNIX command lines can be simple, one-word entries like the date command. They can also be more complex: you may need to type more than the command name.

A UNIX command may or may not have arguments. An argument can be an option or a filename. The general format for UNIX commands 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 (-) and set off by any number of spaces or tabs. Multiple options in one command line can be set off individually (like -a -b), or, in some cases, you can combine them after a single dash (like -ab).

    Some commands, including those on Linux systems, also have options made from complete words or phrases, like --delete or --confirm-delete. When you enter a command, 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. If you don’t enter a filename correctly, you may get the response "filename: no such file or directory” or "filename: cannot open.”

    Some commands, like 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.

  • Two or more commands can be written 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.

Let’s look at a sample UNIX command. The ls command displays a list of files. It can be used with or without options and arguments. If you enter:

% ls

a list of filenames will be displayed on the screen. But if you enter:

% ls -l

there will be an entire line of information for each file. The -l option (a dash and a lowercase letter “l”) modifies the normal output of the ls command and lists files in the 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.”

Exercise: Entering a few commands

The best way to get used to UNIX is to enter some commands. To run a command, type in 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.

Enter who -u or finger or w

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.

Get Learning the UNIX Operating System, Fourth 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.