sections in this module City College of San Francisco - CS160B
Unix/Linux Shell Scripting
Module: Unix Review
module list

Transferring files between systems

To effectively use a Unix or linux, you need to be able to transfer files between systems using command-line tools. There are two programs to do this: secure transfer file protocol (sftp) and secure copy protocol (scp). 

Both sftp and scp are secure, which means that your password is encrypted when you enter it. In fact, the entire transmission is encrypted, since both use ssh encryption, meaning no one can read your data in transit. You may not care about this, but you certainly care about your password. For this reason you should never use insecure protocols (ftp or telnet) to login to a site on which your personal data resides. (Some systems allow public ftp connections, with a public password. For these sites, guarding the password is unnecessary, as anyone is allowed to log on.)

sftp and scp each have different strengths. We can see these strengths by some examples of how they work. We will start with sftp, which is used to transfer individual files between two systems. It proceeds like this:

sftp [login@]host

and respond with your password when prompted. When you log in you are connected to your default directory (in the case of a personal login, this is your home directory) on the remote system.

You are now ready to issue sftp commands

sftp commands

The most confusing thing to remember about commandline sftp is that you are simultaneously connected to two different systems. Simple sftp commands allow you to change directory, create directories, list files, remove files, and, finally, transfer files. These commands can be applied to either one of the systems, or, in the case of file transfer, to transfer a file in either direction.

For simplicity, sftp commands look very similar to standard Unix commands. They are not standard Unix commands, however. They are sftp commands that have the same names as standard Unix commands. The sftp versions [with one exception] do not have options, and, since no shell is involved, wildcards are not expanded. 

The remote system (the one you sftp-ed to) is the default system used for commands. The familiar commands (such as ls for 'list') apply to the remote system. The corresponding command for the local system has the prefix l (for local), in this case lls for 'local list'.

Remote CommandsLocal Commands
CommandMeaningCommandMeaning
pwdprint remote working directorylpwdprint local working directory
cdchange remote directorylcdchange local directory
mkdircreate remote directorylmkdircreate local directory
lslist remote directoryllslist local directory
ls -llist remote directory with attributeslls -llist local directory with attributes
get filetransfer file from remote to local systemput filetransfer file from local to remote system
exit  OR
quit  OR
bye  OR
^D
exit sftphelpneither local nor remote, gives a
list of sftp commands.

sftp is very useful when you want to transfer multiple files from different directories OR when you are unsure where a piece of data is. This makes it ideal for public repositories, where you may not know the exact name of the file or where it is located, requiring some searching.

Let's try a couple of simple problems using sftp:

Example #1 - You are logged into hills.ccsf.edu and you want to back up your archive file files.tar.gz (a gzipped tar archive is similar to a zip file) to your home system. Your login at home is gsmith and your home system's IP address is 204.23.44.121. The archive is in the current directory on your local system, and you want to put it in the directory /backups on your home system. Below are the commands to do it. (The text appearing after # on some lines are annotations, or comments.)

$ sftp gsmith@204.23.44.121
Password:            # you enter your password
sftp> cd /backups    # change your remote directory to /backups
sftp> put files.tar.gz  # transfer the file to the remote system.
# sftp will show you some progress as the file is transferred
sftp> quit           # done. quit sftp.
$

Example #2 - there is a sample output file for the shell script for assignment one in the public directory for cs160b on hills. You know the file is beneath /pub/cs/gboyd/cs160b, but you are unsure what it's name is and what directory it is in. You want to transfer the file to your home system and place it in the directory asmt01 beneath your home directory. (In the example, you think the asmt01 directory exists, but it does not.) Your login on hills is you1. Here are some commands to do it. (Note: the example is for a lecture section of cs160b. An online section would use the directory asmt01-online)

$ cd     # you are in your home directory on your system
$ pwd
/home/gsmith
$ sftp you1@hills.ccsf.edu
Password:
# change the remote directory to our hills public directory
sftp> cd /pub/cs/gboyd/cs160b    
sftp> ls                # list the remote directory
HILLS_SYNC_DATE     asmt01              asmt01-online       asmt02             
asmt03              asmt04              asmt04.old          asmt05.old         
basics              bin                 class               class.old          
ex                  examples            filelist            getopts            
labs                old                 payroll             samples            
shellscripts1       shellscripts2       variables          
sftp> cd asmt01         # change to the directory for the assignment

sftp> pwd               
Remote working directory: /pub/cis/gboyd/cs160b/asmt01
sftp> ls                # list the directory
sample.script           # there's the file we want
sftp> lpwd

Local working directory: /home/gsmith
sftp> lcd asmt01        # try to move to our local asmt01 directory

Couldn't change local directory to "asmt01": No such file or directory
sftp> lmkdir asmt01     # first, create the directory

sftp> lcd asmt01        # then connect to it.
sftp> lpwd
Local working directory: /home/gsmith/asmt01
sftp> get sample.script
Fetching /pub/cis/gboyd/cs160b/asmt01/sample.script to sample.script
/pub/cis/gboyd/cs160b/asmt01/sample.script         100% 2113     2.1KB/s   00:00   
sftp> lls

sftp> quit
# note when you quit sftp you are in the directory you were in when you started.
$ pwd
/home/gsmith
$ ls asmt01
sample.script
$

This last example, while complicated, shows you abilities only available in sftp: searching for a file, and creating a directory as needed. Some problems require this type of flexibility, but the most common problems do not. For these, the overhead of sftp outweighs the flexibility, as does the limitation of only transferring one file at a time.

Public ftp

The non-secure counterpart of sftp, ftp, can still be used for public repositories. When accessing a public repository, it is easiest to use the ftp protocol via a browser. To see if your favorite website has a public repository, simply prefix the web domain with ftp in a browser window. Using this technique I found public ftp sites for Apple (ftp.apple.com), CCSF (ftp.ccsf.edu), and Electronic Arts (ftp.ea.com), but not one for Google. If logging in without a password is allowed (or if your browser can login using your email address as password (below)), the ftp site will show up in the browser window.

Some public ftp sites insist on a password to connect. In this case you can connect using command-line ftp. Use the account anonymous and your email address as the password. (You can probably use any email address.) One site that I found that refused a browser connection but allowed public command-line ftp access was ftp.irs.gov

Differences between ftp and sftp

At the level of our discussion, the biggest difference between sftp and ftp is that ftp does not have the local commands. Instead, prefix a standard shell command with ! (exclamation point) to run the command on the local machine. Note that the command !cd x will not change ftp's current local directory, as the cd command runs in a separate process.

Secure Copy 

If you know the exact path of the data you want to transfer and exactly where you want to put it, scp is more convenient than sftp. scp functions the same as any Unix command: you issue the command, the command is executed, either succeeding or failing, and control is returned to your shell. The syntax is strange at first, but it is consistent with another standard Unix command, cp.

scp [ -r ] source destination

where each of source and destination have the following syntax:

[[user@]host:]path

Here, [[user@host:] must be present for the remote system, and absent for the local system. Most versions of scp only allow one of source and destination to be remote. The path can be absolute or relative. Remote paths are relative to the default directory on the remote host (in our case, the home directory). 

Note that the host specification ends with a colon (:). If there is no colon, the host specification is not recognized, and it is treated as a path! If you run scp with two local paths, it functions the same as a cp command does! 

Let's analyze a few scp commands:

scp you1@hills.ccsf.edu:cs160b/typescript .

This scp command proceeds as follows

scp typescript hills.ccsf.edu:

Now let's try a couple of problems:

Example #1 - You are logged into hills.ccsf.edu and you want to back up your archive file files.tar.gz to your home system. Your login at home is gsmith and your home system's IP address is 204.23.44.121. The archive is in the current directory on your local system, and you want to put it in the directory /backups on your home system. (This is the problem we solved as Example #1 in sftp.) Here is the command to do it:

scp files.tar.gz gsmith@204.23.44.121:/backups

Example #2 - You are logged into your home system and you want to backup up your entire home directory from hills.ccsf.edu to your home system. Again, your login at hills.ccsf.edu is you1. You want to place the copy in the existing directory hillshome beneath your home directory:

scp -r hills.ccsf.edu:~ ~/hillshome

Remember to use the -r option when you are transferring a directory!

These examples show the strengths of scp: the ability to transfer a directory, and avoiding the necessity to connect, change directories appropriately, transfer and disconnect. There is one more benefit to scp: since the path given with the remote host on the commandline is interpreted on the remote system, wildcards are allowed. Thus:

scp -r hills.ccsf.edu:* ~/hillshome

would work as well. (Note that hidden files and directories would be skipped.)

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

Copyright 2011 Greg Boyd - All Rights Reserved.

Document made with Kompozer