SFTP Command: A Comprehensive Guide to Secure File Transfers

Are you looking for a secure file transfer protocol that prevents your data from unauthorized access? Then SFTP is the best option for you.

SFTP is the secure version of FTP (File Transfer Protocol). SFTP encrypts data before transferring, making SFTP more secure than FTPS. SFTP is widely used to transfer files, backup files, securely deliver client files, etc.

In this blog post, I’ll show how you can use the SFTP command to transfer files between two systems securely.

SFTP Command Overview

SFTP stands for Secure File Transfer Protocol, which securely transfers files over the Internet. SFTP is alternative to popular file tranfer protocol FTP. SFTP uses SSH protocol, which is widely used for secure remote login.

SFTP is a command line tool that securely transfers files between two systems. You can use SFTP in most operating systems, including Linux, Windows, and macOS.

How SFTP Works

First, SFTP establishes an SSH connection to the remote machine and encrypts files before transferring over SFTP. You can transfer data between two systems using the SFTP protocol. Encryption of data before sending makes it more secure than FTP and FTPS.

SFTP vs Other File Transfer Protocols

SFTP is a secure alternative to FTP (File Transfer Protocol). Unlike FTP, SFTP encrypts data before transferring, restricting data from unauthorized access. FTPS is another secure alternative to FTP which uses SSL/TLS to encrypt data before sending.

Overall, SFTP is more secure than FTP and FTPS because of its SSH connection.

Basic Use SFTP Command

SFTP commands offer a secure way to transfer files. SFTP supports a wide variety of use cases but first, I will discuss the most basic file transfer using the sftp command. After I will discuss more advanced examples of sftp command.

Step 1: Connecting to a Remote System

Before transferring a file with sftp command, you need to establish a ssh connection with the remote server. To establish a connection use the following command

sftp username@hostname

After executing this command, you will be prompted to enter your password to establish the connection.

If you use SSH key pair instead of a password use the following command to establish the connection

sftp -i private_key.pem username@hostname

Once the connection has been established, you can see similar output in the terminal.

SFTP command to securely transfer file

Step 2: Navigating File System

Before transferring the files or directory, you need to navigate to the file directory. SFTP supports both local and remote navigation.

CommandDescription
lcdChanges the local directory on your local machine
lpwdPrints the local working directory
llsLists the contents of the local directory
lmkdirCreates a new directory on the local machine
lrmdirRemoves an empty directory on the local machine
cdChanges the remote directory on the remote machine
pwdPrints the remote working directory on the remote machine
lsLists the contents of the remote directory on the remote machine
mkdirCreates a new directory on the remote machine
rmdirRemoves an empty directory on the remote machine

With these navigation commands, you easily navigate between the directory.

For example, you want to copy files to Documents on the remote server. Then you can use the following command to navigate the Documents directory

sftp> cd /path/to/directory

Step 3: Transferring Files

SFTP command supports two transfer commands put and get

  • put – Upload a file from your local system to the remote system
  • get – Download a file from the remote system to your local system

For example, if you want to upload a local directory name myfile.txt to the remote server, you can use the following command

sftp> put /path/myfile.txt

This simple command uploads myfile.txt to the current working directory in your remote server. If you want to upload a file other than the working directory, you can specify a directory location like this

sftp> put /path/myfiles.txt /path/myfile.txt

Suppose you want to download a file from a remote server. The file name is file.txt. Then you can navigate to the file directory and use the following command to download the file

sftp> get file.txt

This simple command will download files from the remote server to the local server. You can also specify a directory if you don’t want to navigate to the file directory. In such cases, use the following command

sftp> get /path/to/remote/file /path/to/local/file

Step 4: Closing the Connection

After transferring the files, close the connection with the exit command or press Ctrl+D to terminate the connection.

Advanced SFTP Commands

In the previous section, I’ve discussed the basic file transfer option but sftp supports more advanced commands. Here are some commands

1. Transfer The Entire Folder

If you need to download or upload an entire folder you need to use -r flag, which means recursive file transfer. For example, you need to upload myfolder to a remote server and then use the following command

sftp> put -r /path/myfolder /path/directory

Or if you need to download myfolder directory to a local machine, you need to use -r flag as the following command

sftp> get -r /path/myfolder /path/directory

2. Preserve File Attributes

If you want to upload or download files or folders and preserve file attributes like file permission, time stamps, or ownership information, you need to use -p flag to keep attributes.

For example, if you have a file called myfile.txt and you want to upload it to a remote server and preserve the file attributes, then use the following command

sftp> put -p /path/myfile.txt /path/directory

You can also use -p flag with get command.

3. Transfer Multiple Files

If you need to transfer multiple files simultaneously, sftp also got you covered. SFTP has two commands, mput and met, for multiple file transfers.

For example, you need to upload all the files in your local directory myfolder, to the remote server. You can use the following command for this

sftp> mput /path/myfolder/*

Similarly, you can use mget to download all files from the remote server to the local directory using the following command

sftp> mget /path/myfolder/*

4. Delete Remote Files

If you need to remove some files in the remote server, you can delete the file with rm bash command. For example, you want to delete a file named myfile.txt in a remote server with the following command

sftp> rm /path/myfile.txt

Or, if you want to delete the entire directory, you can use the following command

sftp> rm -r /path/myfolder

Here -r flag means recursive remove.

Conclusion

SFTP is a secure and efficient way to transfer files between systems. SFTP is used where security is the top priority. I’ve covered basic and advanced file transfer commands in this blog post. Following this blog post, you can use sftp command efficiently.

SCP is another secure file transfer protocol based on SSH connection. SCP is more simpler than SFTP while SFTP is more advanced tool.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *