scp and sftp remote file management
You use the ssh
command to start an
interactive shell session on a remote machine. During this session, commands
you type are run on the remote machine and you interact with the remote machine's
file system, not your own.
You can use an ssh connection to transfer files between your local computer
and the remote machine, but not with the ssh
command. Instead,
you use one of two file transfer commands. Secure copy (scp
) is
simpler and faster, but also more limited, while secure file transfer
(sftp
) is more powerful but somewhat slower for large file
transfers.
scp
remote copy
You use the scp
command to copy files between your (local) machine
and the remote machine.
scp
- secure file copy
$ scp [options]... source...
target
This command works just like the cp
command, except the source or destination can be remote paths.
Writing a remote path for scp
[user@]hostname:[path]
The remote path has two parts. The first part (before the colon) identifies the remote machine (and, optionally, the username on that machine). The second part (after the colon) is the file path on the remote machine.
Example: using scp
to copy source code to or
from cs-class
You can copy a file from your computer to cs-class with an scp
command like:
$ scp main.cpp re268@class-1.cs.georgetown.edu:~
This copies the file main.cpp
(in my current working directory)
to my home directory on cs-class.
You can copy a file from cs-class back to your computer by reversing the source and destination arguments:
$ scp re268@class-1.cs.georgetown.edu:main.cpp .
This copies the file to my current working directory.
(To use these commands yourself, substitute your netID and the desired source and destination paths to your individual setup.)
You'll be prompted for your password on the remote machine before the copy takes place (whether the remote is the source or destination).
sftp
remote file management
While scp
is a simple, efficient program for file transfers, it
has limitations. It is not interactive, and cannot modify either the local or
remote file system.
The sftp
command creates an interactive session with a remote
machine, during which you can modify both the local and remote file system
and transfer files between the two.
sftp
- secure file transfer program
$ sftp [options]... destination
After providing your password, sftp
provides a prompt where you
can type sftp commands. These commands let you navigate the remote and local
file systems and to upload ("put") or download ("get") files between the two.
At any time you can type help
to see a complete list of available
commands.
sftp
tracks your remote working directory and provides several
commands for navigating the remote file system (named for the commands they
imitate).
sftp
commands for navigating the remote
file system
pwd
- display remote working directory
cd path
- change remote directory to
path
ls [path]
- display remote directory listing
mkdir path
- create remote directory
rename oldpath newpath
- rename remote file
sftp
also tracks your local working directory and provides commands
for navigating the local file system (beginning with "l" for "local").
sftp
commands for navigating the remote file
system
lpwd
- print local working directory
lcd path
- change local directory to
path
lls [path]
- display local directory listing
lmkdir path
- create local directory
The commands for both local and remote file system navigation take many of the same options as their command-line counterparts.
The last pair of sftp
commands are used to upload and download
files.
sftp
commands for uploading and downloading
files
get remote [local]
- download file
remote
(to local working directory unless
local
is given).
put local [remote]
- upload file
local
(to remote working directory unless
remote
is given).
When you're finished working with sftp
, you can type exit
,
quit
, or bye
to end the session and return to your
shell.
Example: Using sftp to move a file to cs-class
In the sftp session below I navigate both the local and remote file systems
to copy the local file ~/project1/main.cpp
to the remote location
~/cosc051/project1/main.cpp
. The session begins after a successful
login.
sftp> pwd
/home/re268
sftp> ls
cosc051
sftp> cd cosc051
sftp> mkdir project1
sftp> cd project1
At this point, the remote working directory is
~/cosc051/project1
.
sftp> lls
project1
sftp> lcd project1
sftp> lls
main.cpp MakeFile
Now the local working directory is ~/project1
, where
main.cpp
is found.
sftp> put main.cpp
Uploading main.cpp to /home/re268/cosc051/project1/main.cpp
main.cpp
sftp> ls
main.cpp
The file main.cpp
has been copied to cs-class.