You may need to copy files
between computers. For instance, you can put a backup copy of an
important file you’re editing onto an account at a
computer in another building or another city. Dr. Nelson could put a
copy of a datafile from her local computer onto a central computer,
where her colleagues can access it. Or you might want to download 20
files from an FTP server, but not want to go through the tedious
process of clicking on them one by one in a web browser window. If
you need to do this sort of thing often, you may be able to set up a
networked filesystem connection; then you’ll be able
to use the Finder or local programs such as cp
and
mv
. But Unix systems also have command-line tools
for transferring files between computers. These often work more
quickly than graphical tools. We explore them later in this section.
Mac OS X includes both
scp
(secure copy) and rcp
(remote copy) programs for copying files between two computers. In
general, you must have accounts on both computers to use these. The
syntax of scp
and rcp
are
similar to cp
, but also let you add the remote
hostname to the start of a file or directory pathname. The syntax of
each argument is:
hostname:pathname
hostname:
is needed only for remote files.
You can copy from a remote computer to the local computer, from the
local computer to a remote computer, or between two remote computers.
The scp
program is much more secure than
rcp
, so we suggest using scp
to
transfer private files over insecure networks such as the Internet.
For privacy, scp
encrypts the file and your
passphrase.
For example, let’s copy the files
report.may
and report.june
from your home directory on the computer named
giraffe.intuitive.com
and put the copies into
your working directory (.
) on the machine
you’re presently logged in to. If you
haven’t set up the SSH agent that lets you use
scp
without typing your passphrase,
scp
will ask you:
% scp giraffe.intuitive.com:report.may giraffe.intuitive.com:report.june .
Enter passphrase for RSA key 'taylor@mac':
To use wildcards in the remote filenames, put quotation marks
("
name
"
)
around each remote name.[9]
You can use absolute or relative pathnames; if you use relative
pathnames, they start from your home directory on the remote system.
For example, to copy all files from your
food/lunch subdirectory on your
giraffe account into your working directory
(.
) on the local account, enter:
% scp "giraffe.intuitive.com:food/lunch/*" .
Unlike cp
, the Mac OS X versions of
scp
and rcp
don’t have an -i
safety option.
If the files you’re copying already exist on the
destination system (in the previous example, that’s
your local machine), those files are overwritten.
If your system has rcp
, your system administrator
may not want you to use it for system security reasons. Another
program, ftp
, is more flexible and secure than
rcp
(but much less secure than
scp
).
FTP, or file transfer protocol, is a standard way to transfer files between two computers. Many users of earlier Mac OS versions are familiar with Fetch (http://fetchsoftworks.com/), a shareware graphical FTP client that runs on Mac OS X as well as earlier versions.
The Unix ftp
program does FTP transfers from the
command line. There are also a number of easy-to-use graphical FTP
tools available from the Apple web site (go to Get Mac OS X
Software... from the Apple menu and click on Internet Utilities). But
we cover the standard ftp
program here. The
computers on either end of the FTP connection must be connected by a
network (such as the Internet).
To start FTP, identify yourself to the remote computer by giving the username and password for your account on that remote system. Unfortunately, sending your username and password over a public network means that snoopers might see them — and use them to log into your account on that system.
A special kind of FTP, anonymous FTP , happens if you log in to the remote server with the username anonymous. The password is your email address, such as [email protected]. (The password isn’t usually required; it’s a courtesy to the remote server.) Anonymous FTP lets anyone log in to a remote system and download publicly accessible files to their local systems.
To start the standard Unix
ftp
program, provide the remote
computer’s hostname:
ftp hostname
ftp
prompts for your username and password on the
remote computer. This is something like a remote login (see Section 7.1, earlier in
this chapter), but ftp
doesn’t
start your usual shell. Instead, ftp
prints its
own prompt and uses a special set of commands for transferring files.
Table 7-1 lists the most important
ftp
commands.
Table 7-1. Some ftp commands
Here’s an example. Carol uses ftp
to copy the file todo from her
work subdirectory on her account on the remote
computer rhino:
%ls
afile ch2 somefile %ftp rhino.zoo.edu
Connected to rhino.zoo.edu. Name (rhino:carol):csmith
Password: ftp>cd work
ftp>dir
total 3 -rw-r--r-- 1 csmith mgmt 47 Feb 5 2001 for.ed -rw-r--r-- 1 csmith mgmt 264 Oct 11 12:18 message -rw-r--r-- 1 csmith mgmt 724 Nov 20 14:53 todo ftp>get todo
ftp>quit
%ls
afile ch2 somefile todo
We’ve explored the most basic ftp
commands here. Entering help
at an
ftp>
prompt gives a list of all commands;
entering help
followed by an
ftp
command name gives a one-line summary of that
command.
If you can only use
ssh
to connect to a remote site, chances are it
won’t support regular FTP transactions either,
probably due to higher security. Mac OS X also includes a version of
ftp
that works with the standard SSH server
programs and works identically to regular FTP. Just type
sftp
at the command line.
If you need a file from a remote site,
and you don’t need all the control that you get with
the ftp
program, you can use a web browser to
download files using anonymous FTP. To do that, make a URL (location)
with this syntax:
ftp://hostname
/pathname
For instance, ftp://somecorp.za/pub/reports/2001.pdf specifies the file 2001.pdf from the directory /pub/reports on the host somecorp.za. In most cases, you can also start with just the first part of the URL — such as ftp://somecorp.za — and browse your way through the FTP directory tree to find what you want. If your web browser doesn’t prompt you to save a file, use its Save menu command.
An even faster way to download a file is with the
curl
(copy from URL) command. For example,
to save a copy of the report in the current directory, simply enter:
% curl -O ftp://somecorp.za/pub/reports/2001.pdf
Without the -O option, curl
will display the file
in the Terminal window. If you want to read a text file from an
Internet server, you can combine curl
and
less
:
% curl ftp://ftp.oreilly.com/pub/README.ftp | less
You can also use curl
with web pages, but this
will bring the page up in HTML source view (to read HTML documents
from the Terminal, see Section 8.2):
% curl http://www.oreilly.com | less
The
wget
utility is available through Fink (see
Section 8.1). With wget
, you can
download individual files. The following example saves the
O’Reilly home page to
index.html:
% wget http://www.oreilly.com
You can also mirror web sites to your local system with the
-r
option (the mirrored web site is saved in a
subdirectory of your current directory):
% wget -r http://www.oreilly.com
Use caution with the -r
option, since it can use a
lot of bandwidth and take up a lot of disk space. For more
information about wget
, read its manpage with the
command man wget
.
One of the pleasures of working with Unix
within the Mac OS X environment is that there are a wealth of great
Aqua applications. In the world of FTP-based file transfer, the
choices are all uniformly excellent, starting with
Fetch
, NetFinder
,
rbrowser
, and Anarchie
, and
encompassing many other possibilities. Again, either open the Apple
menu and select “Get Mac OS X
Software...” or try VersionTracker (http://www.versiontracker.com/), Mac OS X
Apps (http://www.macosxapps.com/), MacUpdate
(http://macupdate.com/) or the
shareware archive site Download.com (http://www.download.com/).
You can practice your ftp
skills by connecting to
the public FTP archive ftp://ftp.apple.com. Log in as
ftp with your email address as the password,
then look around. Try downloading a research paper or document. If
you have an account on a remote system, try using
rcp
and scp
to copy files back
and forth.
[9] Quotes tell the local shell not to interpret special characters, such as wildcards, in the filename. The wildcards are passed, unquoted, to the remote shell, which interprets them there.
18.227.190.120