Open menu

Learn

SFTP Get Command: A Complete Guide

The Secure File Transfer Protocol (SFTP) provides a reliable method to transfer files between a local system and remote server. Among its various commands, sftp get stands as one of the most essential tools for downloading files from remote systems to your local machine. This article explores how to effectively use the sftp get command to transfer remote files securely.

Understanding SFTP Connections

Before using the sftp get command, you need to establish an SFTP connection to a remote server. This typically requires SSH access credentials:
sftp username@remote_server
Once connected, the sftp prompt will appear, indicating you're ready to transfer files. For servers using a custom SSH port, you can specify it during connection:
sftp -P 2222 username@remote_server

Basic Usage of SFTP Get

The fundamental syntax for downloading a file is:
get remote_file [local_path]
This command transfers a file from the remote server to your local machine. If you don't specify a local path, the file will download to your local working directory with the same name. Example:
sftp> get documents/report.txt
This retrieves the report.txt file from the remote directory and saves it to your current local directory.

Advanced SFTP Get Usage

Downloading Multiple Files To transfer multiple files at once, use the mget command with wildcards:
mget *.txt
This downloads all txt files from the current remote directory to your local system. Specifying Directories You can specify both remote directory and local directory paths:
get /var/www/data/config.txt /home/user/backups/
This command downloads the remote file at the specified file path to your designated local directory. Preserving File Permissions By default, downloaded files adopt permissions based on your local umask. To preserve the original permissions:
get -p important_script.sh
The -p flag maintains the remote file's permissions on your local machine.

Managing Your SFTP Session

While downloading files, you might need to navigate both remote and local environments:
  • pwd - Display current remote directory
  • lpwd - Show current local directory
  • cd path - Change remote directory
  • lcd path - Change local directory
  • Example Workflow:
    sftp> cd /var/data
    
    sftp> lcd ~/Downloads
    
    sftp> get database_backup.sql
    This sequence navigates to a specific directory on the remote system, sets your local download folder, and transfers the file.

    Practical Applications

    The sftp get command can be scripted to regularly download backup files from a remote server:
    #!/bin/bash
    
    sftp user@remote_server << EOF
    
    get /var/backups/daily_backup.tar.gz /home/user/backups/
    
    exit
    
    EOF

    Troubleshooting Common Issues

    Permission Denied If you encounter access issues:
  • Verify your SSH keys are properly configured
  • Check local file permissions
  • Ensure you have read permissions on the remote file
  • Connection Problems For connection difficulties:
  • Confirm the remote server address
  • Verify network connectivity
  • Check if the remote system's firewall allows SFTP
  • Advanced Features

    Display Transfer Statistics Use the -S flag to display statistics about your file transfer:
    get -S large_archive.zip

    Resume Interrupted Transfers

    For large files, the -a flag allows resuming an interrupted download:
    get -a huge_dataset.tar.gz

    Security Considerations

    Since SFTP operates over a secure connection, your data remains protected during transfer. However, best practices include:
  • Using SSH keys instead of passwords
  • Regularly updating your client
  • Limiting access to sensitive files on both local and remote systems
  • Conclusion

    The sftp get command provides a powerful and secure method to transfer files from remote servers to your local machine. Whether you're downloading a single txt file or retrieving multiple files, understanding the nuances of this command will significantly enhance your file maintenance workflows. By mastering the various options and techniques described above, you'll be able to efficiently manage file transfers between systems while maintaining security and data integrity.