12    Copying Files to Another Host

This chapter explains how to use operating system commands to perform the following tasks:

To determine the host name or online status of a remote host before copying files, use the finger, who, rwho, ping, or ruptime commands described in Chapter 10.

In addition to the information in this chapter, Chapter 14 provides information on using the UNIX-to-UNIX Copy Program (UUCP) to copy files to and from remote systems.

Note

The security features on the remote host determine whether or not you can copy a file. See your system administrator if you cannot copy a file.


12.1    Copying Files Between a Local and a Remote Host

You can use the following commands to copy files between a local and a remote host:


12.1.1    Using rcp to Copy Files Between Local and Remote Hosts

When using rcp to copy files from a local to a remote host or from a remote to a local host, name the file to be copied first, followed by the destination file, as shown in this rcp syntax statement:

rcp [ option... ] localfile hostname:file

The localfile variable identifies the local file you want to copy. The hostname:file variable identifies the remote host (hostname) followed by a colon (:) and the name of the file (file) to which the local file is copied.

The following example uses rcp to copy the local file, YTD_sum from the directory /usr/reports on the local host to the file year-end in the directory /usr/acct on the remote host moon:

rcp /usr/reports/YTD_sum  moon:/usr/acct/year-end

You can also send a file on the local host to a user at a remote host. The following example shows how to copy the file YTD_sum from the directory /usr/reports on the local host to the file acct_summaries in the home directory of user jones on the remote host moon:

rcp /usr/reports/YTD_sum  jones@moon:acct_summaries

As used in the preceding examples, the rcp command assigns a new creation date and time to the file created from the original. It also assigns file "read-write-execute" permissions according to the host or user directory containing the newly created file.

You may need to preserve the original creation date and access permission mode of the copied file in the new file. As shown in the following example, the -p option enables you to preserve the original creation date and time and file access permission of YTD_sum in the file, year-end:

rcp -p /usr/reports/YTD_sum  moon:/usr/acct/year-end

If the -p option was not entered, a new date and time would have been assigned, and the file access permission would be set to the default assigned by the system administrator for remote host, moon. In the next example, the -p option preserves the same file creation and access permissions in the file acct_summaries as in the original file, YTD_sum:

rcp -p /usr/reports/YTD_sum  jones@moon:acct_summaries

If the -p option was not entered, a new date and time would have been assigned, but unlike the previous example, the file access permission would be the default set by user jones through the umask command (if any) in the .login file. If the umask is not set in the .login file, the default for remote host moon determines the file access permission mode. See umask(2) for more information about setting umask.

To copy a file from a remote host to a local host, follow the rcp syntax statement shown below. The command syntax is the same as copying a local file to a remote host with the exception that localfile is the destination file, so it is placed last on the command line:

rcp [ option... ] hostname:file localfile


12.1.2    Using ftp to Copy Files Between Local and Remote Hosts

The ftp command is the interface to the File Transfer Protocol (FTP) and has an extensive set of subcommands (described in Table 12-1, Table 12-2, and Table 12-3) that support the main task of copying files. You can use the ftp command to copy files between any two hosts that use the following operating systems: Digital UNIX, UNIX, and VMS (with UCX).

See the ftp(1) reference page for a description of the ftp command options, which are used primarily for network administration tasks.

Copying files through FTP consists of the following steps:

  1. Establishing a session on the remote host

  2. Copying the files

  3. Disconnecting the session

The ftp command has the following syntax:

ftp host_name

The host_name variable specifies the name of the host you want to reach. If you do not specify a host_name on the command line, you must use the ftp subcommand, open (described in Table 12-1) to connect with a remote host.

After you type ftp, the ftp> prompt is displayed and you are logged in to the remote host. You can then use ftp subcommands to perform the following tasks:

Example 12-1 shows how user alice on local host earth logs on to remote host moon, and uses ftp subcommands to check the current working directory, list its contents, copy a binary file, and end the session.

Example 12-1: Using ftp to Copy a File



earth% 
ftp moon          [1]

Connected to moon.
220 moon FTLPServer systemname ready.      [2]
Name(moon:alice):  [Return]        [3]


Password:     [4]


230 User alice logged in     [5]
ftp>   binary   [6]
200 Type set to I
ftp>  pwd      [7]
257 "u/alice" is current directory
ftp>  ls -l    [8]
200 PORT command successful.
150 Opening data connection for /bin/ls (192&.9&.200&.1,1026) 
   (0 bytes)
total 2

 
-rw-r--r-- 1 alice system 101 Jun 5 10:03 file1
 
-rw-r--r-- 1 alice system 171 Jun 5 10:03 file2
 
-rw-r--r-- 1 alice system 1201 Jun 5 10:03 sales
ftp>  get sales newsales     [9]
200 PORT command successful.
150 Opening data connection for testfile (192&.9&.200&.1,1029) 
   (1201 bytes)
226 Transfer complete.
local:tmp.testfile remote:testfile
ftp>  quit      [10]
221 Goodbye.
earth% 


  1. User alice enters the ftp command at the prompt of local host, earth to begin an ftp session with remote host, moon. [Return to example]

  2. A message verifying the connection is displayed on the local host. [Return to example]

  3. User alice presses Return at the prompt because her login name is the same on the remote host. [Return to example]

  4. At the Password: prompt, user alice enters a valid password that is not displayed. [Return to example]

  5. The login to the remote host is verified and the ftp> prompt appears, establishing the ftp session with the remote host. [Return to example]

  6. User alice enters the binary subcommand at the ftp> prompt to set the file transfer type to binary and FTP verifies it with the message 200 Type set to I. [Return to example]

  7. User alice enters the pwd subcommand to identify the current working directory, and FTP verifies it with the message u/alice is current directory. [Return to example]

  8. User alice enters the ls -l subcommand to list the contents of the current working directory, file1, file2, and sales. [Return to example]

  9. User alice copies the file sales from the remote host to a file called newsales on the local host through the get subcommand. [Return to example]

  10. User alice enters the quit subcommand to end the ftp session and returns to the local system prompt. [Return to example]

Table 12-1 describes the ftp subcommands that copy files and exit ftp. The binary, get, and quit subcommands were used in Example 12-1.

Table 12-1: ftp Subcommands for Connecting to a Host and Copying Files

Subcommand Description
account [password] Sends a supplemental password that a remote host other than a Digital UNIX host may require before granting access to its resources. If the password is not specified, the user is prompted for it. The password does not appear on the screen.
ascii Sets the file transfer type to network ASCII, which is the default. For example, a PostScript file is an ASCII file.
binary Sets the file transfer type to binary image. This may be more efficient when copying non-ASCII files. For example, a DECwrite file is non-ASCII.
bye Ends the file copying session and exits FTP; same as quit.
get remfile locfile Copies the remote file, remfile to the file, locfile on the local host. If locfile is not specified, the remote file name is used locally. See also the runique subcommand.
mget remfile [locfile] Copies one or more specified files (remfile) from the remote host to locfile in the current directory on the local host (supports wildcard or pattern-matching metacharacter expansion).
mput locfile [remfile] Copies one or more specified files (locfile) from the local host to remfile on the remote host (supports wildcard or pattern-matching metacharacter expansion).
open host [port] Establishes a connection with the specified host, if you did not specify it on the command line. If port is specified, FTP attempts to connect to a server at that port. If the autologin feature is set (the default), FTP tries to log the user in to the remote host.
put locfile [remfile] Stores a file, locfile on the local host, in the file remfile on the remote host. If you do not specify remfile, FTP uses the local file name to name the remote file. See also the sunique subcommand.
quit Ends the file copying session and exits FTP; same as bye.
recv remfile [locfile] Copies the remote host file, remfile to the file, locfile on the local host; recv works like get.
runique Toggles, creating unique file names for local destination files during get operations. If the unique local file name feature is off (the default), FTP overwrites local files. Otherwise, if a local file has the same name as one specified for a local destination file, FTP appends a .1 extension to the specified name of the local destination file. If a local file already has the new name, FTP appends a .2 extension to the specified name, and so on up to a value of 99. If FTP still cannot find a unique name, it reports an error and the file is not copied. Note that runique does not affect local file names generated from a shell command.
send locfile [remfile] Stores a local file, locfile in the file, remfile on the remote host; send works like put.
sunique Toggles, creating unique file names for remote destination files during put operations. If the unique remote file name feature is off (the default), FTP overwrites remote files. Otherwise, if a remote file has the same name as specified for a remote destination file, the remote FTLPserver modifies the name of the remote destination file in the same way that runique does, and it must be supported on the remote host.

Table 12-2 describes the ftp subcommands that enable you to verify, change, or create the current directory and list its contents before you copy files, if necessary. The pwd and ls subcommands were used in Example 12-1.

Table 12-2: ftp Subcommands for Directory and File Modification

Subcommand Description
cd remotedir Changes the working directory on the remote host to remotedir.
cdup Changes the working directory on the remote host to the parent of the current directory.
delete remfile Deletes the specified remote file.
dir [remdir] [locfile] Lists the contents of remote directory remdir to the file, locfile on the local host.
lcd [directory] Changes the working directory on the local host. If you do not specify a directory, FTP uses your home directory.
ls [remdir] [locfile] Writes an abbreviated file listing of a remote directory, remdir to a local host file, locfile.
mkdir [remdir] Creates specified directory on remote host.
pwd Displays the name of the current directory on the remote host.
rename from to Renames a file on the remote host.
rmdir remdir Removes the remote directory remdir from the remote host.

Table 12-3 describes the ftp subcommands that provide help or status information directly or by invoking the shell from within ftp.

Table 12-3: ftp Subcommands for Help and Status Information

Subcommand Description
!command [option] Invokes an interactive shell on the local host.
?[subcommand] Displays a help message describing the subcommand. If you do not specify subcommand, FTP displays a list of known subcommands. See also the help subcommand.
help [subcommand] Displays help information. See also the ? subcommand.
status Displays current status of ftp, including the current transfer mode (ASCII or binary), connection status, time-out value, and so on.
verbose Toggles verbose mode. When verbose mode is on (the default), FTP displays all responses from the remote FTLPserver. Also, FTP displays statistics on all completed file transfers.

The tftp command, which is the interface to the Trivial File Transfer Protocol (TFTP), provides another way of copying files. Unlike ftp, it does not provide subcommands for any other tasks and is recommended only for tasks performed by the superuser or the installer of the operating system (e.g., copying the operating system kernel). Limited file access privileges are given to the remote tftp server daemon, tftpd. See the tftp(1) reference page for more information.


12.1.3    Using mailx to Copy ASCII Files Between Local and Remote Hosts

The mailx command copies ASCII files to a local or remote host, although mailx is most often used to send and receive mail messages as described in Chapter 11. You can copy an ASCII file to one or more users through mailx by using the left-angle bracket redirection symbol (<) as shown in the following syntax:

mailx [ option... ] recipient... < filename

The recipient variable specifies one or more user names or a mailx alias to whom you want to send the file, filename.

For example, to send the file schedule to several users, you could use the mailx command, as shown with its -s option that indicates the subject of the message:


mailx -s "games" tom jeff craig jim ken < schedule



If you create a mail alias of wombats (See Section 11.6.1) for these five members of a team called Wombats, you can send the file to that alias, as shown:


mailx -s "games" wombats < schedule




12.1.4    Using write to Copy Files Between Local and Remote Hosts

The write command copies files to a local or remote host, although write is most often used to write messages to other users as tasks described in Section 11.8. After you type write, enter the user name of the recipient, a left-angle bracket redirection symbol (<), and the name of the file you want to send. For example, to send a file named letter in your current directory to user maria, enter the following command at the host prompt:


write maria < letter




12.2    Copying Directories of Files Between a Local and a Remote Host

The -r option of the rcp command enables you to copy entire directories of files recursively (that is, including files within any subdirectories) between a local and a remote host. To copy a directory recursively from your local host to a remote host, use the following syntax:

rcp -r localdirectory hostname:directory

The localdirectory variable identifies the local directory that you want to copy recursively. The hostname:directory variable identifies the remote host (hostname) followed by a colon (:) and the name of the remote directory (directory) to which the local directory is copied.

The following example uses rcp -r to copy recursively the directory /usr/reports from the local host to the directory /user/status/newdata on remote host moon:

rcp -r /usr/reports  moon:/user/status/newdata

You can also copy recursively a directory on your local host to a user at a remote host. The following example shows how to copy the directory /usr/reports on the local host to the directory /user/status/newdata in the home directory of user smith on the remote host moon. This example also uses the -p option, as explained in Section 12.1.1, to preserve the original creation date and access permission mode of the directory that is copied in the new directory:

rcp -p -r /usr/reports  smith@moon:/user/status/newdata

To copy a directory recursively from a remote host to your local host, follow the rcp syntax statement shown below. The command syntax is the same as copying a directory recursively from a local to a remote host with the exception that localdirectory is the destination file, so it is placed last on the command line:

rcp -r hostname:directory localdirectory


12.3    Copying Files Between Two Remote Hosts

From your local host, rcp can copy a file on one remote host to a file on another remote host. To do this, use the following rcp syntax:

rcp remhost1:filesend remhost2:file-recv

The remhost1 variable identifies the remote host containing the file you want to send, followed by a colon (:) and the file, filesend that you want to send. The last part of the statement identifies the second remote host, remhost2, and the file name, file-recv, to which the file from remhost1 will be copied. If only a directory name is given in file-recv (as in the example below), filesend will be copied there with the same file name.

The following example uses rcp to copy the file spark from the directory /u/cave/fred on remote host flint to the directory /u/hut/barney on remote host stone:

rcp flint:/u/cave/fred/spark  stone:/u/hut/barney