Cover | Table of Contents | Colophon
-N to open each
one, and
-~ to cycle between
them without removing your hands from the keyboard.
-F (or select
Find → Find from the Edit menu) and enter the specific text.
-G (Find Next) lets you
search down the scroll buffer for the next occurrence, and
-N to open each
one, and
-~ to cycle between
them without removing your hands from the keyboard.
-F (or select
Find → Find from the Edit menu) and enter the specific text.
-G (Find Next) lets you
search down the scroll buffer for the next occurrence, and
-D (Find Previous) lets you search up the scroll buffer
for the previous occurrence. You can also search for material by
highlighting a passage, entering
-E (Use
Selection for Find) or jumping to the selected material with
-J (Jump to
Selection). You can also save an entire
Terminal session as a text file with
File → Save Text As, and you can print the entire session
with File → Print. It's a good idea to
study the key sequences shown in the Scrollback menu, as illustrated
in Figure 1-2.
date
command. They can also be more complex; you may need to type more
than the command or program name.
command option(s) filename(s)
-, 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.
--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.
telnet and
who (shown earlier in this chapter), have
arguments that aren't filenames.
sort command is an example of this feature:
you can tell sort to write the sorted text to a
filename given after its open command. For instance,
when you type open -a Chess at a shell prompt, the
chess game starts. It opens one or more windows on your screen. The
program has its own way to receive your commands — through menus
and buttons on its windows, for instance. Although you
can't interact with these programs using traditional
Unix utilities, Mac OS X includes the osascript
utility, which lets you run AppleScript commands from the Unix shell.
ftp at a shell prompt,
you'll see a new prompt from the
ftp program. Enter FTP commands to transfer files
to and from remote systems. When you enter the special command
quit to quit the ftp program,
ftp will stop prompting you. Then
you'll get another shell prompt, where you can enter
other Unix commands.
jobs command to find the
program's name, then restart the program with
fg or terminate it with kill.
-..
vi
report, the vi
editor is started, and a file named report is
created in your working directory. But if you type a command such as
ls to see what's in the directory
and use wildcards to match files in it.
sudo, a utility that prompts you
for your password and executes the command as the superuser. You must
be an Admin user to use sudo. The user you created
when you first set up your Mac will be an Admin user. You can add new
Admin users, or grant Admin status to a user in System Preferences
→ Accounts.
sudo when you install Unix
utilities or if you want to modify a file you don't
own. Suppose that you accidentally created a file in the
/Users directory while you were doing something
else as the superuser. You won't be able to modify
it with your normal privileges, so you'll need to
use sudo:
% ls -l logfile.out -rw-r--r-- 1 root wheel 1784064 Nov 6 11:25 logfile.out % rm logfile.out override rw-r--r-- root/wheel for logfile.out? y rm: logfile.out: Permission denied % sudo rm logfile.out Password:******** % ls -l logfile.out ls: logfile.out: No such file or directory
sudo again within five minutes, it
won't ask for your password. Be careful using
sudo, since it gives you the ability to modify
protected files, all of which are protected to ensure the system runs
properly.
/, which is
reserved as the separator between files and directories in a
pathname. Filenames are usually made of upper- and lowercase letters,
numbers, "." (dots), and
"_" (underscores). Other characters
(including spaces) are legal in a filename, but they can be hard to
use because the shell gives them special meanings. However, spaces
are a standard part of Macintosh file and folder names, so while we
recommend using only letters, numbers, dots, and underscore
characters for filenames, the reality is that you will have to work
with spaces in file and directory names. The
Finder, by contrast, dislikes colons
(which older versions of Mac OS used as a directory separator, just
as Unix uses the slash). If you display a file called
test:me
in the Finder, the name is shown as
test/me instead. (The reverse is also true: if
you create a file in the Finder whose name contains a slash, it will
appear as a colon in the Terminal.)
")
around the argument or preface each space with a backslash
(\).
rm program, covered later in this
chapter, removes Unix files. To remove a file named a
confusing name, the first rm command in
the following snippet doesn't work, but the second
one does. Also note that you can escape spaces (that is, avoid having
the shell interpret them inappropriately) by using a
backslash character, as shown in the
third example:
% ls -l
total 2
-rw-r--r-- 1 taylor staff 324 Feb 4 23:07 a confusing name
-rw-r--r-- 1 taylor staff 64 Feb 4 23:07 another odd name
% /, which is
reserved as the separator between files and directories in a
pathname. Filenames are usually made of upper- and lowercase letters,
numbers, "." (dots), and
"_" (underscores). Other characters
(including spaces) are legal in a filename, but they can be hard to
use because the shell gives them special meanings. However, spaces
are a standard part of Macintosh file and folder names, so while we
recommend using only letters, numbers, dots, and underscore
characters for filenames, the reality is that you will have to work
with spaces in file and directory names. The
Finder, by contrast, dislikes colons
(which older versions of Mac OS used as a directory separator, just
as Unix uses the slash). If you display a file called
test:me
in the Finder, the name is shown as
test/me instead. (The reverse is also true: if
you create a file in the Finder whose name contains a slash, it will
appear as a colon in the Terminal.)
")
around the argument or preface each space with a backslash
(\).
rm program, covered later in this
chapter, removes Unix files. To remove a file named a
confusing name, the first rm command in
the following snippet doesn't work, but the second
one does. Also note that you can escape spaces (that is, avoid having
the shell interpret them inappropriately) by using a
backslash character, as shown in the
third example:
% ls -l
total 2
-rw-r--r-- 1 taylor staff 324 Feb 4 23:07 a confusing name
-rw-r--r-- 1 taylor staff 64 Feb 4 23:07 another odd name
% *
(asterisk),
?
(question
mark), and [
] (square brackets). When used in a file or
directory name given as an argument on a command line, the following
is true:
*
ae* would match
aegis, aerie,
aeon, etc. if those files were in the same
directory. You can use this to save typing for a single filename (for
example, al* for
alphabet.txt) or to choose many files at once
(as in ae*). A * by itself
matches all file and subdirectory names in a directory, with the
exception of any starting with a period. To match all your dot files,
try .??*.
?
h?p matches hop and
hip, but not help).
[]
[Cc]hapter would match either
Chapter or chapter, but
chap[12] would match chap1 or
chap2. Use a hyphen (-) to
separate a range of consecutive characters. For example,
chap[1-3] would match chap1,
chap2, or chap3.
% ls chap10 chap2 chap5 cold chap1a.old chap3.old chap6 haha chap1b chap4 chap7 oldjunk % ls chap? chap2 chap5 chap7 chap4 chap6 % ls chap[5-7] chap5 chap6 chap7 % ls chap[5-9] chap5 chap6 chap7 % ls chap?? chap10 chap1b % ls *old chap1a.old chap3.old cold % ls *a*a* chap1a.old haha
open command within the Terminal to launch the
editor with the proper file already loaded. For example:
open -e myfile.txt.
cp and
CpMac commands: if you copy a file to a directory,
it automatically reuses the filename in the new location. This can
save lots of typing!
mkdir program.
The syntax is:
mkdir dirname(s)
% mkdir spy boston.dine
cp program when copying plain files
and directories containing only plain files. Other files having
resource forks should be copied with CpMac
(available only if you have installed Apple's Mac OS
X Developer Tools).
cp program can
put a copy of a file into the same directory or into another
directory. cp doesn't affect the
original file, so it's a good way to keep an
identical backup of a file.
cp old new
tcsh as
its shell. If you'd like to configure it to use a
different shell, you can do so by selecting Terminal →
Preferences and specifying the shell to use.
tcsh as
its shell. If you'd like to configure it to use a
different shell, you can do so by selecting Terminal →
Preferences and specifying the shell to use.
set prompt="%/ %h% " setenv LESS 'eMq' alias desktop "cd ~/Desktop" date
set
prompt tells
the shell to use a different prompt than the standard one.
We'll explain the details of prompt setting in
Section 4.2.1 later in this
chapter.
setenv
LESS tells
the less program which options you want to set
every time you use it. Not all commands recognize environment
variables, but for those that do, this saves you the trouble of
typing the options on every man
cmd. Start with man man
to learn more about the man system.
http://gimp-print.sourceforge.net/). The
gimp-print release is available as a disk image that includes a
graphical installer.
enscript
for good results. Before we cover printing itself,
let's look at both pr and
enscript to see how they work.
pr program does minor formatting of files on the
terminal screen or for a printer. For example, if you have a long
list of names in a file, you can format it onscreen into two or more
columns.
enscript
for good results. Before we cover printing itself,
let's look at both pr and
enscript to see how they work.
pr program does minor formatting of files on the
terminal screen or for a printer. For example, if you have a long
list of names in a file, you can format it onscreen into two or more
columns.
pr option(s) filename(s)
pr changes the format of the file only on the
screen or on the printed copy; it doesn't modify the
original file. Table 5-1 lists some
pr options.
|
Option
|
Description
|
|---|---|
-
k
|
Produces k columns of output
|
-d
|
Double-spaces the output
|
-h
header
|
lpr command can
handle a variety of file types, including PDF, plain text, and a
variety of image types (JPEG, TIFF, and others). If your printer does
not support PostScript, you will not be able to use
lpr to print PostScript files directly. This also
means that you won't be able to use
enscript for printing.
ps2pdf
to turn your PostScript file into a
PDF. To run enscript on the
food file, convert it to PDF, and print it, use
pipes between enscript, ps2pdf,
and lpr:
% enscript -o - food | ps2pdf - - | lpr
-o - switches and the pipe symbol
(|) tell enscript to send its
PostScript output to the ps2pdf program. The
- - options and the pipe tell
ps2pdf to read its input from the pipe and send
its output to lpr, which sends the PDF to the
printer. For more information on pipes, see Chapter 6.
less, and
more do work together in this way.) It also
doesn't apply to graphical programs, such as the
Finder or Internet Explorer, that open their own windows on your
screen.
< (less-than symbol) operator.
tr (character translator) is such a program.
Here's how to use the input redirection operator to
convert commas to linefeeds in the to_do file:
% cat to_do Install Mac OS X,Learn Unix,???,Profit! % tr ',' '\n' < to_do Install Mac OS X Learn Unix ??? Profit! %
>
)
operator. The pipe operator (|
) sends the
standard output of one program to the standard input of another
program. Input/output redirection is one of the most powerful and
flexible Unix features.
< (less-than symbol) operator.
tr (character translator) is such a program.
Here's how to use the input redirection operator to
convert commas to linefeeds in the to_do file:
% cat to_do Install Mac OS X,Learn Unix,???,Profit! % tr ',' '\n' < to_do Install Mac OS X Learn Unix ??? Profit! %
>
)
operator. The pipe operator (|
) sends the
standard output of one program to the standard input of another
program. Input/output redirection is one of the most powerful and
flexible Unix features.
cat
, which is short for
"concatenate," reads files and
outputs their contents one after another, without stopping.
cat file(s)
% cat /etc/csh.login
# System-wide .login file for csh(1).
setenv PATH "/bin:/sbin:/usr/bin:/usr/sbin"
%|
) on the command line between two
commands. When a pipe is set up between two commands, the standard
output of the command to the left of the pipe symbol becomes the
standard input of the command to the right of the pipe symbol. Any
two commands can form a pipe as long as the first program writes to
standard output and the second program reads from standard input.
grep
program searches a file or files for
lines that have a certain pattern. The syntax is:
grep "pattern" file(s)
ed (a Unix line editor) command
g/re/p, which means
"globally search for a regular
expression and print all matching lines containing
it." A regular
expression
is either some plain text (a word,
for example) and/or special characters used for pattern matching.
When you learn more about regular expressions, you can use them to
specify complex patterns of text.
grep is to look for a pattern
consisting of a single word. It can be used in a pipe so only those
lines of the input files containing a given string are sent to the
standard output. But let's start with an example
reading from files: searching all files in the working directory for
a word — say, http://www.ssh.com),
OpenSSH (http://www.openssh.org), or a compatible
application such as
PuTTY (a Windows
implementation of SSH available at http://www.chiark.greenend.org.uk/~sgtatham/putty/).
SSH and OpenSSH can be installed on many Unix systems, and OpenSSH is
included with many Linux distributions as well as Mac OS X.
ssh work. In a local login, you interact directly
with the shell program running on your local system. In a remote
login, you run a remote-access program on your local system