What is SFTP?
Secure File Transfer Protocol (or sftp as the program is called) is a secure version of the interactive File Transfer Protocol (FTP).
FTP allows you to transfer files to and from a remote machine interactively (that is you can interactively change directories on either box, get files from the remote box, and put files on the remote box, list files, create directories etc ...).
Like scp, sftp uses ssh for the underlying data transfer and authentication, so it inherits all the security benefits of ssh.
Also, any auto login keys you've setup for ssh will also apply to sftp, and if you've set up connection sharing, your sftp session will be multiplexed over the master connection.
Starting an SFTP session
To start an interactive sftp session with a remote host with ip address 192.168.0.7, run:
sftp 192.168.0.7
To start it as a different user on the remote box, (where user is the username you wish to use) run:
sftp [email protected]
Either of these will start the interactive mode which will show the sftp prompt:
sftp>
Interactive commands
Below are examples of some of the more common interactive commands.
In interactive mode, all commands are entered at the sftp prompt.
Note that auto-completion (using TAB) will work both locally and remotely.
Changing directory
To change to a different remote directory, use:
cd
To change to a different local directory, use:
lcd
Print working directory
To print the remote working directory, use:
pwd
To print the local working directory, use:
lpwd
Directory creation
To create a new directory on the remote machine, use:
mkdir /path/to/new_dir_name
To create a new local directory, use:
lmkdir /path/to/new_dir_name
In both cases, leave the path out if you want to create the new directory in the current working directory.
Listing files
To list files in the working directory of the remote machine, use:
ls
To list files in the working directory on the local machine, use:
lls
Retrieving files from the remote machine with get
To copy files from the remote machine, use the get
command. The syntax is:
get [-Ppr] /remote/path/to/file [/local/path/to/save/file]
The remote path is optional, as is the entire local part of the command.
For example to copy a file from the remote working directory to the local working directory (if the file exists), use:
get file_name
To keep the same file permissions and file access times, use the -p
option:
get -p file_name
To copy files recursively, use the -r
option:
get -r dir_name
Uploading files to the remote machine with put
To copy file to the remote machine, use the put
command. The syntax is:
put [-Ppr] /local/path/to/file [/remote/path/to/save/file]
The local path is optional, as is the entire remote part of the command.
So for example to upload a file from the current working directory to the remote working directory, use:
put file_name
To keep the same file permissions and file access times, use the -p
option:
put -p file_name
To upload files recursively, use the -r
option:
put -r dir_name
Remote directory and file removal
To remove a remote directory, use:
rmdir directory_name
To remove a remote file use:
rm file_name
Rename remote file
To rename a remote file, use:
rename old_file_name new_file_name
Help!
If you get stuck, use the help command:
help
or use the man page
Thanks to T. Ylonen and S. Lehtinen for their work with sftp.