sftp command is the implementation of the Secure File Transport Protocol (SFTP). SFTP uses SSH tunnel as a transportation mechanism to encrypt data. Whereas scp is used purely for transferring files, sftp can transfer files and manage files and directories. So, you can list, create, and remove directories on the remote system. The sftp command also supports resuming file transfers, whereas scp does not.
Just like with the standard ftp command, you can use sftp interactively or non-interactively. For example, to retrieve a file non-interactively:
# sftp user@host:file.txt
If you encounter the below error while running the sftp command:
sftp: command not found
you may try installing the below package as per your choice of distribution:
OS Distribution | Command |
---|---|
Debian | apt-get install openssh-client |
Ubuntu | apt-get install openssh-client |
Alpine | apk add openssh-client |
Arch Linux | pacman -S scp |
Kali Linux | apt-get install openssh-client |
CentOS | yum install openssh-clients |
Fedora | dnf install openssh-clients |
Raspbian | apt-get install openssh-client |
sftp Command Examples in Linux
1. Connect to a remote server and enter an interactive command mode:
# sftp remote_user@remote_host
2. Connect using an alternate port:
# sftp -P remote_port remote_user@remote_host
3. Connect using a predefined host (in `~/.ssh/config`):
# sftp host
4. Transfer remote file to the local system:
get /path/remote_file
5. Transfer local file to the remote system:
put /path/local_file
6. Transfer remote directory to the local system recursively (works with `put` too):
get -R /path/remote_directory
7. Get list of files on local machine:
lls
8. Get list of files on remote machine:
ls