Datasheet

Book VIII
Chapter 1
. . . And UNIX Lurks
Beneath
681
UNIX Commands 101
Anatomy of a UNIX command
UNIX commands can perform many amazing feats. Despite their vast abili-
ties, all commands follow a similar structure:
command <optional flag(s)> <optional operand(s)>
The simplest form of a UNIX command is the command itself. (For a basic
discussion on UNIX commands such as ls, see the earlier section, “A few
commands to get started.”) You can expand your use of the ls command by
appending various flags, which are settings that enable or disable optional
features for the command. Most flags are preceded by a dash (-) and always
follow the command. For instance, you can display the contents of a direc-
tory as a column of names by tacking on a -l flag to the ls command.
ls –l
Besides flags, UNIX commands sometimes also have operands. An operand
is something that is acted upon. For example, instead of just entering the ls
command, which lists the current directory, you can add an operand to list a
specific directory:
ls ~/Documents/myProject/
The tilde (~) denotes the user’s Home directory.
Sometimes a command can take multiple operands, as is the case when
you copy a file. The two operands represent the source file and the destina-
tion of the file that you want to copy, separated by a space. The following
example copies a text file from the Documents folder to the Desktop folder
by using the cp command (short for copy).
cp ~/Documents/MyDocument ~/Desktop/MyDocument
You can also combine flags and operands in the same command. This exam-
ple displays the contents of a specific folder in list format:
ls –l ~/Documents/myProject/
Command-line gotchas
In earlier sections, I describe a few simple command-line functions. All these
commands have something in common: You might not have noticed, but
every example thus far involved folder names and filenames that contained
only alphanumeric characters. Remember what happens if you have a folder
name that has a space in it? Try the following example, but don’t worry
when it doesn’t work.
55_9781118022061-bk08ch01.indd 68155_9781118022061-bk08ch01.indd 681 8/10/11 9:39 AM8/10/11 9:39 AM