6.4. Transferring FilesYou 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 data file 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, your system administrator may be able to set up a networked filesystem connection; then you'll be able to use local programs such as cp and mv. But Unix systems also have command-line tools for transferring files between computers. These often do it more quickly than working with graphical tools does. We explore them later in this section. 6.4.1. scp and rcpYour system may have an scp (secure copy) or rcp (remote copy) program 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 like 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 named report.may and report.june from your home directory on the computer named giraffe and put the copies into your working directory (.) on the machine you're logged in to now. If you haven't set up the SSH agent that lets you use scp without typing your passphrase, scp will ask you:
$ scp giraffe:report.may giraffe:report.june . Enter passphrase for RSA key 'jpeek@home': To use wildcards in the remote filenames, put quotation marks ("name") around each remote name.[17] 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:food/lunch/*" . Unlike cp, most 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). 6.4.2. FTPFTP, file transfer protocol, is a standard way to transfer files between two computers. The Unix ftp program does FTP transfers from the command line.[18] (Your system may have a friendlier version of ftp named ncftp. Some graphical filesystem browsers can also handle FTP transfers. But we cover the standard ftp program here.) Both computers must be connected by a network (such as the Internet), but they don't need to run Unix.
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 may see them--and use them to log into your account on that system. A special kind of FTP, anonymous FTP, happens if you log into the remote server with the username anonymous. The password is your email address, like alex@foo.co.uk. (The password usually isn't required; it's a courtesy to the remote server.) Anonymous FTP lets anyone log into a remote system and download publicly-accessible files to their local systems. 6.4.2.1. Command-line ftpTo 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 6.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 6-1 lists the most important ftp commands. Table 6-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 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. 6.4.2.2. FTP with a web browserIf 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 handy Lynx web browser. Its -dump option sends a page to the standard output, where you can redirect it to a file or pipe it to another program (see Chapter 5). For example, to save the report in a file named report.pdf, enter:
$ lynx -dump "ftp://somecorp.za/pub/reports/2001.pdf" > report.pdf Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|