Linux sftp command

The sftp command in Linux is used to securely transfer files between local and remote systems using the SSH (Secure Shell) protocol. It provides a secure alternative to FTP (File Transfer Protocol) for file transfer operations.

Here’s an overview of how to use the sftp command:

  1. Connecting to a Remote Server:
    To establish an SFTP connection to a remote server, use the following command:
   sftp username@remote_host

Replace username with your remote server username and remote_host with the hostname or IP address of the remote server. For example:

   sftp john@example.com
  1. Interactive SFTP Shell:
    Once connected, you’ll enter an interactive shell where you can issue various SFTP commands. The shell prompt will change to sftp>, indicating that you are in the SFTP environment.
  2. Basic SFTP Commands:
  • ls: List files and directories in the current remote directory.
  • cd directory: Change the current remote directory.
  • pwd: Display the current remote directory.
  • get file: Download a file from the remote server to the local system.
  • put file: Upload a file from the local system to the remote server.
  • mkdir directory: Create a new directory on the remote server.
  • rmdir directory: Remove a directory from the remote server.
  • rm file: Delete a file from the remote server.
  • exit or quit: Exit the SFTP session and return to the local shell.
  1. File Transfers:
    To upload a file from the local system to the remote server, use the put command followed by the filename:
   put local_file

Replace local_file with the path to the file you want to upload.

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

   get remote_file

Replace remote_file with the path to the file you want to download.

  1. Specifying Remote Paths:
    You can specify the remote path for file transfers by prefixing the remote filename or directory with the desired path. For example, to upload a file to a specific remote directory:
   put local_file remote_directory/
  1. Recursive File Transfers:
    To transfer directories and their contents recursively, use the -r option with the put or get command. This ensures that all subdirectories and files are transferred:
   put -r local_directory
  1. Examples:
  • Connect to a remote server: sftp john@example.com
  • Upload a file to the remote server: put local_file.txt
  • Download a file from the remote server: get remote_file.txt
  • Change the current remote directory: cd remote_directory
  • List files and directories on the remote server: ls
  • Exit the SFTP session:
    exit

The sftp command provides a secure and reliable method for transferring files between local and remote systems using the SSH protocol. It supports various file transfer operations and provides an interactive shell for managing remote files and directories.

For more information about the sftp command and its options, you can refer to the manual page by typing man sftp in your terminal.