In this post, we'll run through the commands that will allow you to use SFTP to securely transfer files from one machine to another using this interactive alternative to SCP.
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 (i.e. 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.7To start it as a different user on the remote box, (where user is the username you wish to use) run:
sftp firstname.lastname@example.orgEither of these will start the interactive mode which will show the sftp prompt:
Below are examples of some of the more common interactive commands.
In interactive mode, all commands are entered at the sftp prompt.
Note that autocompletion (using TAB) will work both locally and remotely.
To change to a different remote directory, use:
cdTo change to a different local directory, use:
Print Working Directory
To print the remote working directory, use:
pwdTo print the local working directory, use:
To create a new directory on the remote machine, use:
mkdir /path/to/new_dir_nameTo create a new local directory, use:
lmkdir /path/to/new_dir_nameIn both cases, leave the path out if you want to create the new directory in the current working directory.
Listing filesTo list files in the working directory of the remote machine, use:
lsTo list files in the working directory on the local machine, use:
Get - Retrieving Files from the Remote Machine
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_nameTo keep the same file permissions and file access times, use the "-p" option:
get -p file_nameTo copy files recursively, use the "-r" option:
get -r dir_name
Put - Uploading Files to the Remote Machine
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 it 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_nameTo keep the same file permissions and file access times, use the "-p" option:
put -p file_nameTo upload files recursively, use the "-r" option:
put -r dir_name
Remote Directory and File Removal
To remove a remote directory, use:
rmdir directory_nameTo remove a remote file use:
Rename Remote File
To rename a remote file, use:
rename old_file_name new_file_name
If you get stuck, use the help command:
helpor use the man page
Thanks to T. Ylonen and S. Lehtinen for their work with sftp.