sections in this module City College of San Francisco - CS260A
Unix/Linux System Administration

Module: Review
module list

Transferring Files between Systems

I will start this section with a warning. Never use ftp. The only exception to this is anonymous ftp. Make sure that any protocol that requires you to supply your password is a secure (encrypted) protocol. In this section we will cover two secure file transfer methods: sftp (the ssh-variant of ftp) and scp, the secure file copy program, which again uses ssh.

sftp is useful for transferring individual files between systems. It is also useful if you need to retrieve a file from a remote system and you do not know where the file is.

scp is useful if you want to transfer one unit of information (a file or a directory) and you know exactly where the information is and where it should go.

With both programs, you are briefly connected to two systems at the same time. The system from which the connection was initiated is referred to as the local system. The system you are connecting to is the remote system. This may sound simple, but it can create confusion, especially when using sftp.

Using sftp

If you've used ftp, you can skip this section after reading the following differences:

For the rest of us, a connection is initiated using

sftp [user@]remotesystem

Here, the remote user defaults to the current username if it is not given. You will be prompted for the remote password, then connected to remotesystem:

$ sftp hills.ccsf.edu
Connecting to hills.ccsf.edu...
Password:
sftp>

You are now connected to your home directory on the remote system. This is your remote working directory. Remember you still have a local working directory.

From this point on, sftp commands default to the remote system. sftp commands look just like filesystem commands, but they are not! They are simplified sftp versions of useful filesystem commands. The useful commands are ls, pwd, cd, mkdir. They all work as you might expect: ls even has an option: -l that works!

sftp> ls -l a*
-rw-------    0 3496     208             6 Nov  1  2007 a
-rwx------    0 3496     208         70144 Sep 24 20:02 a.out
-rw-------    0 3496     208            93 Mar 22  2007 about_vi
-rw-------    0 3496     208            30 Aug 26 14:18 aboutcat
-rw-r--r--    0 3496     208          1126 Dec  4  2007 addr
-rw-------    0 3496     208            35 Dec  4  2007 address
lrwx------    0 3496     208            24 Sep 23 20:35 apple_pie
-rw-r--r--    0 3496     208          8377 Feb 20  2008 asmt01.notes.sxw
-rw-r--r--    0 3496     208         11257 Feb 20  2008 asmt01_notes.sxw
-rw-r--r--    0 3496     208         30720 May 10  2008 asmt04.tar
sftp>

Remember that these are on the remote system! (This is the confusing thing about sftp) However, sftp is pretty good about reminding you:

sftp> pwd
Remote working directory: /users/gboyd
sftp> 

If you want to execute a command on the local system, simply prefix one of the above simple commands with l (for local):

sftp> lls -l a*
-rw-r--r-- 1 gboyd gboyd  1127 May  9  2008 address
-rwxr-xr-x 1 gboyd gboyd  6766 Oct 27 10:52 a.out
-rw-r--r-- 1 gboyd gboyd 17920 Apr 27  2008 archive1.cpio
sftp> lpwd
Local working directory: /home/gboyd
sftp>

If you're not in the correct directories, simply use the cd or lcd commands to navigate. WIldcards are expanded correctly, but, unfortunately, ~ doesn't work, since it isn't a wildcard.

Ok, that's all pretty easy. Now what we want to do is backup the file named address to the remote system. The two transfer commands are get and put. Which do you use? (Remember, if you use the wrong one, you just overwrote the current version with some other version, rather than backing up the current version!) 

If you guessed put, you were correct. Remember, by default you are looking at the remote system, so put transfers to that system! sftp kindly informs you that you uploaded the file, but by then it is too late!

sftp> put address
Uploading address to /users/gboyd/address
address                                      100% 1127     1.1KB/s   00:00   
sftp>

Last, you exit sftp. You can use any of bye, exit, or quit.


Using scp

It is very common to need to copy a single piece of information between systems. This may be a file or a directory. In this case, scp is much easier than sftp. Note that with scp you connect, transfer and disconnect in a single command. 

scp is a general-purpose utility designed to transfer information between any two unix systems. That is, I can be on system A and copy information from system B to system C. Unfortunately, this capability does not work on our systems - you can only copy from your current account on the current system to some account on a remote system OR from some account on a remote system to the current account on the current system. Thus, we will dispense with the general format of an scp command in favor of one syntax for each direction:

To copy to a remote system:

scp [ -r ]  localpath  [user@]remotedomain:[remotepath]

The -r [recursive] option allows the source to be a directory. In this case the destination must also be a directory.

You will be asked for the password for user@remotedomain.

Here, if user@ is missing, it defaults to the current user. If remotepath is missing, the user's home directory is assumed. If remotepath is relative, it is interpreted relative to user's home directory.

Note: the most common mistake with scp is to forget the : after the domain when copying to a remote domain. If this occurs, the domain will not be recognized and the command will become a copy (cp) command with a very strange target filename.

To copy from a remote system

scp [ -r ]   [user@]remotedomain:remotepath  localpath

Examples:

First, an actual scp command and the output. Afterwards, we will just give some sample commands:

$ scp transferring_files.html hills.ccsf.edu:
Password:
transferring_files.html                       100%   15KB  14.9KB/s   00:00    
$

This transfers the single file transferring_files.html to the home directory of the current user on hills.ccsf.eduNote the : after hills.ccsf.edu !!

scp -r jane@foo.com:mybackup ~/work

This transfers mybackup from beneath the home directory of jane on foo.com. Exactly what happens depends on whether mybackup is a file or a directory

Note that these semantics are similar to a cp -r command.

There can be multiple source specifications, and wildcards can be used for either local or remote source specifications:

scp work/* foo.com:work

scp foo.com:work/* .

You can also specify the ssh port number using -P port, if your version of ssh does not use the standard port.


Prev This page was made entirely with free software on Linux:  
  Kompozer
and Openoffice.org    
Next

Copyright 2016 Greg Boyd - All Rights Reserved.