File Transfer Protocol (FTP) was once a widely used method for transferring files or data remotely. However, it transmits information in an unencrypted format, making it an insecure way to communicate.

As we all know, FTP is not secure because all transmissions occur in clear text, which means that anyone sniffing network packets can easily read the data.

Because of this, FTP should only be used in limited cases or on networks you fully trust. Over time, protocols like SCP (Secure Copy) and SSH (Secure Shell) have addressed these security concerns by introducing encrypted layers for securely transferring data between remote systems.

[ You might also like: Best Command-Line FTP Clients for Linux ]

What Is sFTP?

sFTP (Secure File Transfer Protocol) is a part of the SSH protocol suite that runs over the SSH protocol on the standard port 22 by default to establish a secure connection. SFTP has been integrated into many GUI tools such as FileZilla, WinSCP, and FireFTP.

You can access sFTP from the Linux terminal using the sftp command, which often pre-installed on most Linux distributions.

which sftp

If that returns a path like /usr/bin/sftp, you’re good to go.

Security Warning: Please don’t expose the SSH (Secure Shell) port to the public internet, as this poses a security risk. Instead, allow access only from specific IP addresses that will be used to transfer or manage files on the remote system.

Related Articles:

This article walks you through real-world sFTP command examples, from logging in and navigating directories to uploading and downloading files. We’ll also cover batch transfers, scripting, and automation tips using sFTP.

1. How to Connect to SFTP

By default, the same SSH protocol is used to authenticate and establish an SFTP connection. To start an SFTP session, enter the username and the remote hostname or IP address at the command prompt.

Once authentication is successful, you will see a shell with the sftp> prompt.

sftp [email protected]

If SSH is running on a custom port (say 2222), use:

sftp -oPort=2222 [email protected]
sFTP Remote Connection
sFTP Remote Connection

Once, you are in the sftp prompt, check the available commands by typing ‘?‘ or ‘help‘ at the command prompt.

sftp> ?
sFTP Help and Usage
sFTP Help and Usage

2. Check Present Working Directory

When you’re connected to a remote server via sFTP, it’s important to know where you are – both locally (on your own machine) and remotely (on the server). sFTP provides two simple commands for this purpose: lpwd and pwd.

The command lpwd (local print working directory) is used to display your current local directory on your own machine from which you’re working. On the other hand, the command pwd (print working directory) shows your current directory on the remote server.

Here’s how they look in an active sFTP session:

sftp> lpwd
Local working directory: /
sftp> pwd
Remote working directory: /tecmint/
  • lpwd helps you verify where files will be downloaded to.
  • pwd helps you confirm where files will be uploaded from.

Understanding these commands is especially useful when you’re navigating multiple directories during file transfers.

3. Listing Files with sFTP

Once you’re connected to a remote server using sFTP, you’ll often need to browse through directories to check the available files on remote system and on your local machine.

To list files on the remote server, simply use the ls command, which will show the contents of the current directory on the remote host.

sftp> ls

If you want to see detailed file information like size and permissions, you can also use the -l option:

sftp> ls -l

Now, if you want to list files on your local system (the machine you’re running sFTP from), you’ll use the lls command, which behaves like the regular ls command but shows the contents of your local directory.

sftp> lls

You can also pass options to lls to list files in long format:

sftp> lls -l

Using ls and lls together helps you manage files efficiently between local and remote systems within the sFTP interface.

🔒 Pro Tip: Tired of running the same Linux commands over and over? Learn how to automate them with our Bash Scripting for Beginners course and start creating scripts to save time and simplify your daily tasks.

4. Upload File Using sFTP

Once you’ve connected to the remote server using the sftp command, you can use the put command to upload a file. For example, let’s say you have a file called local.profile on your local machine, and you want to transfer it to the remote server.

put local.profile

When you run this command, sFTP will upload the file from your current local directory to the current directory on the remote server.

You should see output similar to:

Uploading local.profile to /home/username/local.profile

If you want to upload multiple files at once, you can use wildcard characters with the mput command. For instance, to upload all .txt files from the current local directory:

mput *.txt

Tip: Before uploading, it’s always good to check and set your local and remote working directories using the lcd and cd commands, respectively.

For example:

lcd /home/user/documents
cd /var/www/html
put index.html

5. Download Files Using sFTP

To download a single file from the remote system to your current local directory, use the get command followed by the filename.

sftp> get SettlementReport_1-10th.xls

If you want to download multiple files at once, you can use the mget command, which is especially useful when you’re dealing with a bunch of reports, logs, or data files:

sftp> mget *.xls

The mget command uses wildcard patterns like *.xls to grab all files with the .xls extension from the remote directory and copy them into your local working directory.

6. Renaming Files While Downloading Using sFTP

By default, the get command downloads the file using its original name. However, if you wish to save the file under a different name locally, you can specify a second argument with the desired name.

sftp> get SettlementReport_1-10th.xls Report_Jan.xls

In this case, the remote file SettlementReport_1-10th.xls will be downloaded and saved locally as Report_Jan.xls.

7. Switching Directories in sFTP

To change the remote directory (the directory on the server you’re connected to), use the cd command followed by the desired path.

sftp> cd test

You can verify your current location on the remote system by running:

sftp> pwd

Similarly, to switch to a different local directory (your current machine’s file system), use the lcd command:

sftp> lcd Documents

To confirm the local directory change, you can run:

sftp> lpwd

8. Creating Directories Using sFTP

To create a new directory on the remote server, you can use the mkdir command from within the sFTP prompt:

mkdir test

This command creates a directory named test in the current working directory on the remote server. You can then upload files into this directory using put, or change into it using cd.

On the other hand, if you want to create a directory on your local machine while inside the sFTP session, use the lmkdir command:

lmkdir Documents

This creates a directory called Documents in your current local working directory. You might use this before downloading multiple files into a dedicated folder using the mget command.

9. Remove Directories Using sFTP

To delete a file, use the rm command inside the sFTP prompt. For example, if you want to remove a file named Report.xls from the current remote directory, run:

rm Report.xls

To remove a directory, use the rmdir command.

rmdir sub1

Important Note: sFTP can only delete empty directories. If the directory contains files or subdirectories, you’ll need to delete those contents first using rm, or remove them recursively using other tools like SSH or rsync.

So before removing any directory, make sure it’s empty. Otherwise, the rmdir command will fail with an error like:

rmdir failed: Directory not empty

Use sFTP with SSH Keys (No Password Prompt)

If you want to avoid typing your password every time you connect via sFTP, you can set up SSH key-based authentication using SSH key pair on your local machine.

ssh-keygen -t rsa -b 4096

You can simply press Enter to accept the default file location (~/.ssh/id_rsa) and optionally set a passphrase, which will generate two files: a private key (id_rsa) and a public key (id_rsa.pub).

Next, copy your public key to the remote server using:

ssh-copy-id user@remote_host

Once that’s done, you can connect to the server using sFTP without entering a password:

sftp user@remote_host

10. Exit sFTP Shell

To exit the sFTP shell and end your session with the remote server, you simply need to type:

bye
Or
exit

But there’s also another helpful trick you should know.

If you’re inside an sFTP session and need to temporarily drop into your local Linux shell without disconnecting from the remote sFTP session, you can use the ! command, which lets you run local Linux commands directly from within the sFTP environment.

sftp> !

Now you can run any regular Linux command.

ls -l

Once you’re done with the local shell and want to return to the sFTP prompt, just type:

exit

After running exit, you’ll return to the sFTP session as shown:

exit
Shell exited with status 1
sftp>

Finally, when you’re ready to fully leave the sFTP session, run:

sftp> bye
Conclusion

The SFTP is a very useful tool for administrating servers and transferring files to and from (Local and Remote). We hope these examples will help you to understand the usage of SFTP to some extent.

Similar Posts