Transferring files with SSH tools

The most common file transfer program of old was a program called ftp, short for ``file transfer program.'' Like telnet and rlogin, the ftp program is unsecure and has been replaced by ssh tools. There are two tools you might use to copy files between machines. They are sftp and scp.

Transferring files with sftp

The sftp program is an interactive client, much like smbclient (or an ftp client if you have used one). You can use it to transfer files with any computer that you can ssh to. The syntax is simple. You may simply type sftp remotehost, or if you need to specify a username, sftp user@remotehost. When running, it accepts many of the same commands as smbclient, including:

Table 7-2. sftp commands

help lists the commands understood by the program
cd change the remote directory
dir (or ls) list the contents of the remote directory
get remotefile get a file from the remote machine
put localfile put a file onto the remote machine

For example:

Example 7-7. Get and put with sftp

$ sftp kronos
Connecting to kronos...
sftp> get
Fetching /home/ldap/dbindner/ to
sftp> put
Uploading to /home/ldap/dbindner/
sftp> quit

Transferring files with scp

The scp program is the other (secure) means to transfer files between computers. It works very much like the regular copy command cp, except that either the source file or destination file may be prepended with a hostname. For example:

Example 7-8. Copying with scp

$ scp             100% |*****************************|   344       00:00    
$ scp kronos:hello.o .
hello.o              100% |*****************************|  3528       00:00    
$ scp /tmp/             100% |*****************************|   344       00:00

Here the first command copies a local file, to the remote host kronos, leaving it in the home directory there. The second command copies a remote file hello.o to the local current directory (dot always refers to the current directory). The third command copies a file from kronos, specifying explicitly that the connection on kronos should be made as user dbindner.