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. Or, 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, 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. 8.2.1 scp and rcpMac 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. [2] 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 ). 8.2.2 FTPFTP, 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 into the remote server with the username anonymous . The password is your email address, such as alex@foo.co.uk . (The password isn't usually 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. Here's how that might look: $ ftp ftp.apple.com Trying 17.254.16.11... Connected to ftp.apple.com. 220 ProFTPD 1.2.8 Server (Apple Anonymous FTP Server) [ftp02.apple.com] Name (ftp.apple.com:taylor): ftp 331 Anonymous login ok, send your complete email address as your password. Password: 230 Anonymous access granted, restrictions apply. Remote system type is UNIX. Using binary mode to transfer files. ftp> dir 500 EPSV not understood 227 Entering Passive Mode (17,254,16,11,223,250). 150 Opening ASCII mode data connection for file list drwxrwxrwx 3 ftpprod ftpprod 102 May 7 19:11 Apple_Support_Area drwxrwxr-x 20 ftpprod ftpprod 680 Aug 28 22:07 developer drwxrwxr-x 30 ftpprod ftpprod 1020 Sep 15 13:44 emagic drwxrwxr-x 10 ftpprod ftpprod 340 Sep 3 16:23 filemaker drwxrwxrwx 10 ftpprod ftpprod 340 Apr 7 16:50 research 226 Transfer complete. ftp> quit 221 Goodbye. $ 8.2.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 8.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 8-1 lists the most important ftp commands. Table 8-1. Some ftp commands
Here's an example. Carol moves into the local directory she wants to use as a starting point (a good idea whether you're uploading or downloading), then uses ftp to copy the file todo from her work subdirectory on her account on the remote computer rhino : $ cd uploads $ 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 local: todo remote: todo 227 Entering Passive Mode (17,254,16,11,224,18). 150 Opening BINARY mode data connection for todo (724 bytes) 226 Transfer complete. 724 bytes received in 00:00 (94.06 KB/s) 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. 8.2.2.2 SFTP: FTP to secure sitesIf 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 is compatible with the standard SSH server programs and works identically to regular FTP. Just type sftp at the command line. Here's an example: $ cd downloads $ sftp taylor@intuitive.com Connecting to intuitive.com... The authenticity of host 'intuitive.com (128.121.96.234)' can't be established. RSA key fingerprint is d0:db:8a:cb:74:c8:37:e4:9e:71:fc:7a:eb:d6:40:81. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'intuitive.com,128.121.96.234' (RSA) to the list of known hosts. taylor@intuitive.com's password: sftp> cd mybin sftp> dir -l drwxr-xr-x 0 24810 100 1024 Jun 26 20:18 . drwxr-xr-x 0 24810 100 1536 Sep 16 18:59 .. -rw-r--r-- 0 24810 100 140 Jan 17 2003 .library.account. info -rwxr-xr-x 0 24810 100 3312 Jan 27 2003 addvirtual ... -rw-r--r-- 0 24810 100 406 Jan 24 2003 trimmailbox.sh -rwxr-xr-x 0 24810 100 1841 Jan 24 2003 unpacker -rwxr-xr-x 0 24810 100 946 Jan 22 2003 webspell sftp> get webspell webspell 100% 946 4.7KB/s 00:00 sftp> quit $ ls -l webspell -rwxr-xr-x 1 taylor taylor 946 25 Sep 11:28 webspell 8.2.2.3 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 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: $ curl http://www.oreilly.com less 8.2.2.4 Other FTP solutionsOne 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 , Transmit , FTPeel , rbrowser , and Anarchie , and encompassing many other possibilities. Either open the Apple menu and select "Get Mac OS X Software . . . ", or try VersionTracker (see http://www. versiontracker .com/), Mac OS X Apps (see http://www.macosxapps.com/), MacUpdate (see http://macupdate.com/), or the shareware archive site Download.com (see http://www.download.com/). 8.2.3 Easy Shortcuts with Connect to ServerThe Terminal application has a very helpful feature that can make connecting to remote systems via telnet , ssh , ftp , or sftp a breeze , once it's set up. Connect To Server is available off the File menu and is shown in Figure 8-4. Figure 8-4. Connect to Server offers simple shortcutsTo add a service, click on the + icon on the left side of the window. More commonly, you'll add servers, which you can do by clicking on the + icon on the right side of the window. It produces a window that asks for the hostname or host IP address, which is easily entered, as shown in Figure 8-5. Figure 8-5. Adding a New Server to Connect to ServerOnce added in one area, the new server is available for all services, so to connect to Apple's anonymous FTP archive site, choose ftp , then the new server name, and then enter ftp into the User box, as shown in Figure 8-6. Figure 8-6. Specifying user ftp on ftp connections to ftp.apple.comFinally, the connection to Apple's server is a breeze: specify the server, specify the user, and click on Connect. The results are shown in Figure 8-7. Figure 8-7. Instant connection to Apple's ftp server8.2.4 PracticeYou can practice your ftp skills by connecting to the public FTP archive 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. |