SFTP, or Secure File Transfer Protocol, is a network protocol that allows secure file access, transfer, and management over an encrypted SSH connection. SFTP is commonly used to transfer files between servers securely, often to or from a Linux server. It combines the ease of FTP with the security of SSH, making it suitable for use in environments that require encryption.

In this guide, we’ll explore how to use the sftp command effectively with practical examples.

Connecting to a Remote Server

To connect to a remote server using sftp, you can use the following command:

sftp user@remote_server_ip

For example:

sftp [email protected]

After entering the command, you’ll be prompted for the user’s password. Once authenticated, you will see the sftp> prompt, indicating that you have successfully connected.

Basic Commands in sftp

When connected via sftp, you can use various commands to navigate and manipulate files, similar to the commands used in a regular shell.

1. Listing Files

To list the files and directories in the current directory on the remote server, use:

ls

You can also use lls to list files in your local working directory:

lls

2. Changing Directories

To change the current directory on the remote server:

cd /path/to/directory

To change the directory on the local system:

lcd /path/to/local/directory

3. Uploading Files

To upload a file from your local system to the remote server, use the put command:

put local_file.txt /remote/directory

For example:

put document.txt /home/john/documents

4. Downloading Files

To download a file from the remote server to your local system, use the get command:

get /remote/directory/file.txt

For example:

get /home/john/documents/report.pdf

5. Uploading/Downloading Directories

To recursively upload a directory:

put -r local_directory /remote/directory

To download a remote directory recursively:

get -r /remote/directory local_directory

6. Renaming Files

To rename a file on the remote server:

rename old_name.txt new_name.txt

7. Deleting Files

To delete a file on the remote server:

rm /path/to/file.txt

Checking Remote Directory

To display the current remote working directory:

pwd

To display the current local working directory:

lpwd

Creating and Removing Directories

To create a new directory on the remote server:

mkdir /path/to/new_directory

To remove a directory on the remote server:

rmdir /path/to/directory

Using sftp in Batch Mode

If you need to automate file transfers, you can use sftp in batch mode by providing a file containing a list of commands.

Create a batch file, commands.txt, with the following content:

lcd /local/directory
cd /remote/directory
put file1.txt
get file2.txt

Then run sftp in batch mode:

sftp -b commands.txt user@remote_server_ip

This allows you to automate repetitive file transfer tasks without manually typing each command.

Using sftp in Non-Interactive Mode

To run sftp non-interactively, echo commands to sftp or use here documents. This is particularly useful for integrating sftp commands into scripts.

Example with here document:

sftp user@hostname <<EOF
put /local/path/file1.txt /remote/path/
get /remote/path/file2.txt /local/path/
EOF

Conclusion

The sftp command is an essential tool for secure file transfers in Linux environments. Whether you’re transferring individual files or automating backups, sftp provides the flexibility and security needed to handle data in a reliable manner. Unlock the full potential of secure file transfers with SFTP on Atlantic.Net’s dedicated server hosting.