SFTP (SSH File Transfer Protocol) is a secure file transfer protocol that operates over SSH. In this article, we will review the basic SFTP commands that will help you work effectively with remote servers.
Connecting to the server
To connect to an SFTP server, use the following command:
sftp username@hostname # connect to the server
For example:
sftp user@example.com # connect to example.com
If your server uses a non-standard port, specify it using the -P option:
sftp -P 2222 user@example.com # connect to port 2222
Navigating directories
Viewing the current directory
pwd # shows the current remote directory
lpwd # shows the current local directory
Changing directory
cd directory # changes the remote directory
lcd directory # changes the local directory
Viewing directory contents
ls # shows the contents of the current remote directory
lls # shows the contents of the current local directory
Transferring files
Downloading files
get remote_file # downloads a file from the server
get remote_file local_file # downloads a file and saves it under a different name
Downloading folders
get -r remote_directory # recursively downloads the entire folder
Uploading files
put local_file # uploads a file to the server
put local_file remote_file # uploads a file under a different name
Uploading folders
put -r local_directory # recursively uploads the entire folder
Additional useful commands
mkdir directory # creates a new directory on the server
rm file # deletes a file on the server
rmdir directory # removes an empty directory on the server
rename old_name new_name # renames a file or directory on the server
Ending the session
To exit the SFTP session, use the command:
exit # ends the SFTP session
or
bye # also ends the SFTP session
Usage tips
1. Use the Tab key for auto-completion of file and directory names.
2. The help
command will show a list of available commands.
3. Add -v
to the sftp command to enable verbose output, which can be useful for diagnosing problems.
Remember that SFTP is a secure protocol, but always follow best security practices when working with remote servers.