Skip to main content

3.2.2 scp

The secure shell also has a program for copying files across the network, so you can put files on one machine onto another machine. The principle is the same as you've already experienced for regular copying (the cp command), except this time the communication between your current working directory and the target location for your file are encrypted. There are two types of remote copy:

1. copying a file from your local machine to a remote machine
2. copying a file from a remote machine to your local machine

In the examples below, you can always use the option of using a wildcard (*) to specify many files.

Option 1 copying a file from your local machine to a remote machine

Suppose you have a file in your current working directory (called, say, input_May05 ) that you wish to copy over to a remote machine (called, say, faraway.csc.warwick.ac.uk ). Say also that you wish to copy this file over to your home directory on faraway.csc.warwick.ac.uk, and also suppose that you want this file to keep the same name (ie, you don't want to give it a different name on the remote machine). The basic syntax is:

scp input_May05 userid@faraway.csc.warwick.ac.uk:.

The colon marks the end of the address of the remote machine. The dot after the colon is just the same as the dot you've used already for the regular copying command cp - it means that you wish to copy this file such that it keeps the same file name. If instead you wished to copy your file to a given location on your home filespace on faraway.csc.warwick.ac.uk , then all you have to do is supply the pathname after the colon. If you want to rename the file this should come at the end of this pathname. Similarly, if the local file you wish to copy over is not in your current directory, all have to do is supply the pathname to this file.

Example:

The user phillipa is in the current working directory /home/phillipa/food/ and she wishes to copy a file called pizza.types (located in her current working directory) over to the machine faraway.csc.warwick.ac.uk . On the remote machine, she wishes to keep the file name the same (ie keep it as pizza.types ). Her userid on the remote machine is msulg . She will copy this file to the top directory in her home filespace on the remote machine (ie, /home/msulg/ ). To do this she types:
 scp pizza.types msulg@faraway.csc.warwick.ac.uk:. 
Now suppose she wishes that she wishes to rename this file on the remote machine, such that it's called food_preferences , while still copying this file over to her top directory on the remote machine. To do this, she types:
 scp pizza.types msulg@faraway.csc.warwick.ac.uk:food_preferences 
Now suppose instead that she wishes to copy pizza.types to the remote location /home/msulg/menu/items/ on faraway.csc.warwick.ac.uk . She wishes to retain the file name (ie, it stays as pizza.types ). To do this she types
 scp pizza.types msulg@faraway.csc.warwick.ac.uk:menu/items/ 
She then decides that she's rather change the name of the file to food_preferences , while still copying the file to the remote location /home/msulg/menu/items/ . To do this she types
 scp pizza.types msulg@faraway.csc.warwick.ac.uk:menu/items/food_preferences 
Finally, phillipa would also like to copy over a file in /home/phillipa/drinks/ called wine.types . She wants to copy this over to faraway.csc.warwick.ac.uk , to the location /home/msulg/menu/items/ , and she wants to rename this flie to drink_preferences . While staying in her current working directory ( /home/phillipa/food/ ), she types
 scp ../drinks/wine.types msulg@faraway.csc.warwick.ac.uk:menu/items/drink_preferences 

Option 2 copying a file from a remote machine to your local machine

In a similar spirit, consider a similar situation as before, except now you have a file (called, say, input_May05 ) in your remote top directory that you wish to copy over to a local machine. Say also that you wish to copy this file over to your current working directory on your local machine, and also suppose that you want this file to keep the same name (ie, you don't want to give it a different name on the local machine). The basic syntax is:

scp userid@faraway.csc.warwick.ac.uk:input_May05 .

Again, the dot is important, signifying (as in a regular cp operation) that you wish to copy the file to your current working directory while keeping the file name the same.

Example:

The user phillipa is in the current working directory /home/phillipa/food/ and she wishes to copy a file called pizza.types (located in her remote top directory) from to the machine faraway.csc.warwick.ac.uk . On the local machine, she wishes to keep the file name the same (ie keep it as pizza.types ). Her userid on the remote machine is msulg . She will copy this file to her current working directory in her local machine. To do this she types:
 scp msulg@faraway.csc.warwick.ac.uk:pizza.types . 
Now suppose she wishes that she wishes to rename this file on the local machine, such that it's called food_preferences , while still copying this file over to her current working directory on the local machine. To do this, she types:
 scp msulg@faraway.csc.warwick.ac.uk:pizza.types  food_preferences  
Finally, phillipa (still in the current working directory /home/phillipa/food/ ) would also like to copy over a remote file in /home/msulg/menu/items/ called wine.types . She would like to copy this to the local location /home/phillipa/wine/, and she would like to rename this file to drink_preferences . She types
 scp msulg@faraway.csc.warwick.ac.uk:menu/items/wine.types  ../wine/drink_preferences