Secure File Transfer Protocol (SFTP) Basic Terminal Commands

Posted by Ray on January 24, 2024

A guide for SFTP basic terminal commands.

Connect to Host

  1. Open up terminal, and connect to host using the command:
    1
    2
    3
    4
    5
    
     # sftp <username>@<host_name_or_ip_address>
     $ sftp mrestepa@XXX.XXX.XX.XXX
    
     # To use custom port:
     # sftp -P <custom_port> <username>@<host_name_or_ip_address>
    

sftp

Performing Commands

  • To perform commands in remote host, type the commands directly into the terminal as usual.
    1
    
      $ sftp> pwd
    
  • To perform commands in local computer, type the commands in the terminal after typing ā€œlā€
    1
    
      $ sftp> lpwd
    

sftp

Downloading File From Remote to Local

  1. Navigate into the remote directory where the file is stored.
  2. Navigate into the local directory where you want to store the downloaded file.
  3. Type command get <name of file> .. The first parameter is the name and location of the file in remote host, and the 2nd parameter is the downloaded file path and name in local.
    1
    2
    
     # get -r <remote_path_and_filename> <local_path_and_filename>
     $ get samplefile.txt .
    
  • Steps 1 and 2 can be skipped, but include the full path and name of the remote file to download, and full path of local directory to store the file.
  • The downloaded file can be renamed in the 2nd parameter by specifying the new name:
    1
    
      $ get samplefile.txt newname.txt
    

Downloading Folder From Remote to Local

  1. Navigate into the remote directory where the folder is stored.
  2. Navigate into the local directory where you want to store the downloaded folder.
  3. Type command get -r <name of folder> .. The first parameter is the name and location of the folder in remote host, and the 2nd parameter is the downloaded folder path and name in local.
    1
    2
    
     # get -r <remote_path> <local_path>
     $ get -r media/ .
    
  • Steps 1 and 2 can be skipped, but include the full path and name of the remote folder to download, and full path of local directory to store the folder.
  • The downloaded folder can be renamed in the 2nd parameter by specifying the new name:
    1
    
      $ get -r media/ media-backup/
    

Uploading File From Local to Remote

  1. Navigate into the local directory where the file is stored.
  2. Navigate into the remote directory where you want to store the uploaded file.
  3. Type command put <name of file> .. The first parameter is the name and location of the file in local computer, and the 2nd parameter is the uploaded file path and name in remote.
    1
    2
    
     # put <local_path_and_filename> <remote_path_and_filename>
     $ put samplefile.txt .
    
  • Steps 1 and 2 can be skipped, but include the full path and name of the remote file to download, and full path of local directory to store the file.
  • The downloaded file can be renamed in the 2nd parameter by specifying the new name:
    1
    
      $ put samplefile.txt newname.txt
    

Uploading Folder From Local to Remote

  1. Navigate into the local directory where the folder is stored.
  2. Navigate into the remote directory where you want to store the uploaded folder.
  3. Type command put -r <name of folder> .. The first parameter is the name and location of the folder in local computer, and the 2nd parameter is the uploaded folder path and name in remote.
    1
    2
    
     # put -r <local_path_and_foldername> <remote_path_and_foldername>
     $ put -r mediabackup/ .
    
  • Steps 1 and 2 can be skipped, but include the full path and name of the remote file to download, and full path of local directory to store the file.
  • The downloaded file can be renamed in the 2nd parameter by specifying the new name:
    1
    
      $ put -r mediabackup/ media/