sftp Command in Linux

Learn via video courses
Topics Covered

Overview

The Secure File Transfer Protocol, or SFTP, is a protocol used for securely transferring files over network connections. The 'sftp' command in Linux is the client-side command for this protocol. It provides a secure, encrypted channel to transfer files between local and remote systems, while ensuring data integrity and confidentiality. This article aims to delve into the details of the 'sftp' command in Linux, exploring its syntax, options, and various use cases.

Syntax of sftp Command in Linux

The basic syntax of the sftp command in Linux is as follows:

Where:

  • options: These are flags or parameters that modify the behavior of the sftp command.
  • user@host: This specifies the remote host to which you are connecting. You may also optionally include the username to use for the connection.
  • dir: This is an optional parameter to specify a directory on the remote host.

Options in sftp Command in Linux

  1. -P: Specify the port number of the remote host.

    For example -

    Output:

    This command initiates a secure file transfer session to 'remotehost' on port 2222.

  2. -b: Use a batch file for automating tasks without user interaction.

    For example -

    Output:

    This command executes the commands in 'batchfile.txt' on 'remotehost'.

  3. -C: Enables compression of the data before it's sent.

    For example -

    Output:

    This command initiates a secure file transfer session with 'remotehost' with compression enabled.

Example Usages

  • Upload a file to the remote host:

    Output:

    Explanation: This command uploads 'localfile.txt' from your local system to the connected remote host.

  • Download a file from the remote host:

    Output:

    Explanation: This command downloads 'remotefile.txt' from the connected remote host to your local system.

Tips

  • You can use the 'ls' command within an sftp session to list the files in the current directory on the remote host.

  • Remember to use the 'exit' command to end your sftp session once you have finished your tasks.

Advanced Use Cases of sftp Command in Linux

  • Use of wildcard (*) in SFTP:

    Output:

    Explanation: This command downloads all files starting with 'file' from the connected remote host to your local system.

  • Renaming a file on the remote server:

    Output:

    Explanation: This command renames 'oldfile.txt' to 'newfile.txt' on the connected remote host.

  • Creating a directory on the remote server:

    Output:

    Explanation: This command creates a new directory named 'newdir' on the connected remote host.

Conclusion

  • SFTP is a secure method for transferring files over a network connection.

  • SFTP sessions can be customized using a variety of options.

  • The SFTP command in Linux allows for a range of file and directory operations, from uploading and downloading to renaming and creating new directories.