Section 9.6. Distributing Data


[Page 350 (continued)]

9.6. Distributing Data

A very basic kind of remote operation is the transmission of files, and once again Linux has several utilities for doing this:

  • rcp (remote copy) and scp (secure copy) allow you to copy files between your local Linux host and another remote Linux or UNIX host.

  • ftp (file transfer protocol or program) and sftp (secure ftp) allow you to copy files between your local Linux host and any other host (possibly non-Linux) that supports FTP (the File Transfer Protocol). ftp is thus more powerful than rcp.

  • uucp (unix-to-unix copy) is similar to rcp, and allows you to copy files between any two Linux or UNIX hosts.

subc sections that follow describe rcp, scp, ftp, and sftp.


[Page 351]

9.6.1. Copying Files Between Two Linux or UNIX Hosts: rcp and scp

rcp and scp allow you to copy files between Linux or UNIX hosts (Figure 9-15).

Figure 9-15. Description of the rcp and scp commands.

Utility: rcp -p originalFile newFile

rcp -pr { fileName }+ directory

scp -p originalFile newFile

scp -pr { fileName }+ directory

rcp and scp both allow you to copy files between Linux or UNIX hosts. Both your local host and the remote host must be registered as equivalent machines (as described in the section on Security beginning on page 342). To specify a remote file on host, use the syntax:

host:pathName


If pathName is relative, it's interpreted as being relative to your home directory on host. The -p option tries to preserve the last modification time, last access time, and permission flags during the copy. The -r option causes any file that is a directory to be recursively copied.

When scp is used, the TCP/IP connection to the remote host is encrypted so that a network sniffer cannot observe the data contained in the packets.


In the following example, I copied the file "original.txt" from the remote "vanguard" host to a file called "new.txt" on my local "csservr2" host. I then copied the file "original2.txt" from my local host to the file "new2.txt" on the remote host.

$ rcp vanguard:original.txt new.txt           ...remote to local. $ rcp original2.txt vanguard:new2.txt         ...local to remote. $ _ 


9.6.2. Copying Files Between Non-Linux/UNIX Hosts: ftp and sftp

The File Transfer Protocol is a generic protocol for the transmission of files, and is supported by many machines. You can therefore use it to transfer files from your local Linux host to any other kind of remote host as long as you know the Internet address of the remote host's ftp server. Users of non-Linux or non-UNIX computers often use ftp for transferring files between Linux or UNIX and their own system. Figure 9-16 gives a brief description of ftp and sftp.


[Page 352]

Figure 9-16. Description of the ftp and sftp commands.

Utility: ftp -n [ hostName ]

sftp [ user@ ] hostName [:file]

ftp and sftp allow you to manipulate files and directories on both your local host and a remote host.

If you supply a remote host name, ftp searches the ".netrc" file to see if the remote host has a passwordless anonymous ftp account. If it does, it uses it to log you into the remote host. Otherwise, it assumes that you have an account on the remote host and prompts you for its username and password. If the login is successful, ftp enters its command mode and displays the prompt "ftp>". If you don't supply a remote host name, ftp enters its command mode immediately and you must use the open command to connect to a remote host.

The -n option prevents ftp from attempting the initial automatic login sequence.

ftp's command mode supports many commands for file manipulation. The most common of these commands are described in Figure 9-17. You may abort file transfers without quitting ftp by pressing Control-C.

sftp opens a secure connection to the remote host and transfers files via encrypted TCP/IP packets. Once a secure connection is established, if file is not specified, sftp enters an interactive mode similar to ftp where files can be transferred.


Figure 9-17 lists the most useful ftp commands that are available from its command mode.

Figure 9-17. Commands within the ftp program.

[Page 353]

Command

Meaning

!command

Executes command on local host.

append localFile remoteFile

Appends the local file localFile to the remote file remoteFile.

ascii

Transfers a file as ASCII text (maintains proper text format between machines whose text format may differ). ASCII transfer is the default behavior.

bell

Causes a beep to be sounded after every file transfer.

binary

Transfers a file exactly as it is with no format changes.

bye

Shuts down the current remote host connection and then quits ftp.

cd remoteDirectory

Changes your current remote working directory to be remoteDirectory.

close

Shuts down the current remote host connection.

delete remoteFile

Deletes remoteFile from the remote host.

get remoteFile [ localFile ]

Copies the remote file remoteFile to the local file localFile. If localFile is omitted, it is given the same name as the remote file.

help [ command ]

Displays help about command. If command is omitted, a list of all ftp commands is displayed.

lcd localDirectory

Changes your current local working directory to be localDirectory.

ls remoteDirectory

Lists the contents of your current remote working directory.

mkdir remoteDirectory

Creates remoteDirectory on the remote host.

open hostName [ port ]

Attempts a connection to the host with name hostName. If you specify an optional port number, ftp assumes that this port is an ftp server.

put localFile [ remoteFile ]

Copies the local file localFile to the remote file remoteFile. If remoteFile is omitted, it is given the same name as the local file.

pwd

Displays your current remote working directory.

quit

Same as bye.

rename remoteFrom remoteTo

Renames a remote file from remoteFrom to remoteTo.

rmdir remoteDirectory

Deletes the remote directory remoteDirectory.



[Page 353]

In the following example, I copied "writer.c" from the remote host "vanguard" to my local host, and then copied "who.c" from my local host to the remote host:

$ ftp vanguard     ...open ftp connection to "vanguard". Connected to vanguard.utdallas.edu. vanguard FTP server (SunOS 5.4) ready. Name (vanguard:glass): glass       ...login Password required for glass. Password:                          ...secret! 
[Page 354]
User glass logged in. ftp> ls ...obtain directory of remote host. PORT command successful. ASCII data connection for /bin/ls (129.110.42.1,4919) (0 bytes). ... ...lots of files were listed here. uniq upgrade who.c writer.c ASCII Transfer complete. 1469 bytes received in 0.53 seconds (2.7 Kbytes/s) ftp> get writer.c ...copy from remote host. PORT command successful. ASCII data connection for writer.c (129.110.42.1,4920) (1276 bytes). ASCII Transfer complete. local: writer.c remote: writer.c 1300 bytes received in 0.012 seconds (1e+02 Kbytes/s) ftp> !ls ...obtain directory of local host. reader.c who.c writer.c ftp> put who.c ...copy file to remote host. PORT command successful. ASCII data connection for who.c (129.110.42.1,4922). ASCII Transfer complete. ftp> quit ...disconnect. Goodbye. $ _





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net