Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»A Comprehensive Guide to Transferring Files Over SSH

    A Comprehensive Guide to Transferring Files Over SSH

    By RahulApril 6, 20233 Mins Read

    Secure Shell (SSH) is a cryptographic network protocol used for secure communication and remote command execution between computers. One of its most common use cases is transferring files securely over a network. In this guide, we will explore various tools and methods for transferring files over SSH, including scp, rsync, and sftp.

    Advertisement

    SCP (Secure Copy Protocol)

    SCP is a secure and straightforward method for transferring files over SSH. It uses the same authentication and security as SSH and is available on most Unix-based systems.

    1. Copy a file from a local machine to a remote machine:

      1
      scp /path/to/local/file username@remote_host:/path/to/remote/directory

    2. Copy a file from a remote machine to a local machine:

      1
      scp username@remote_host:/path/to/remote/file /path/to/local/directory

    3. Copy a directory from a local machine to a remote machine:

      1
      scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory

    4. Copy a directory from a remote machine to a local machine:

      1
      scp -r username@remote_host:/path/to/remote/directory /path/to/local/directory

    5. Copy files with a specific extension (e.g., .txt) from a remote machine to a local machine:

      1
      scp username@remote_host:'/path/to/remote/directory/*.txt' /path/to/local/directory

    Rsync Over SSH

    Rsync is a powerful and versatile file synchronization tool that can transfer files over SSH. It is particularly useful when transferring large files or directories, as it can compress and transfer only the changes between the source and destination.

    1. Sync a local directory with a remote directory:

      1
      rsync -avz /path/to/local/directory username@remote_host:/path/to/remote/directory

    2. Sync a remote directory with a local directory:

      1
      rsync -avz username@remote_host:/path/to/remote/directory /path/to/local/directory

    3. Transfer only new or modified files between local and remote directories:

      1
      rsync -avzu /path/to/local/directory username@remote_host:/path/to/remote/directory

    4. Sync a local directory with a remote directory and delete files on the remote side that are not present in the local directory:

      1
      rsync -avz --delete /path/to/local/directory username@remote_host:/path/to/remote/directory

    5. Transfer files with a specific extension (e.g., .txt) from a local directory to a remote directory:

      1
      rsync -avz --include='*.txt' --exclude='*' /path/to/local/directory username@remote_host:/path/to/remote/directory

    SFTP (SSH File Transfer Protocol)

    SFTP (SSH File Transfer Protocol) is a secure and reliable protocol for transferring files over SSH. It provides an interactive, command-line interface similar to FTP, allowing you to navigate and manipulate remote file systems securely. Here are some practical examples of using SFTP for transferring files between local and remote systems:

    1. Connecting to a remote system using SFTP:
    2. To start an SFTP session, run the following command:

      1
      sftp username@remote_host

      Enter your password when prompted. Once connected, you will be in the remote system’s home directory.

    3. Navigating directories:
    4. Use the following commands to navigate directories in the remote system:

      • To list the contents of the current directory, run:

        1
        ls

      • To change the current directory, run:

        1
        cd /path/to/directory

      • To show the current working directory, run:

        1
        pwd

    5. Transferring files:
      • Upload a file from the local machine to the remote machine:

        1
        put /path/to/local/file /path/to/remote/directory

      • Download a file from the remote machine to the local machine:

        1
        get /path/to/remote/file /path/to/local/directory

      • Upload a directory from the local machine to the remote machine:

        1
        put -r /path/to/local/directory /path/to/remote/directory

      • Download a directory from the remote machine to the local machine:

        1
        get -r /path/to/remote/directory /path/to/local/directory

    6. Manipulating files and directories:
      • To create a new directory on the remote system, run:

        1
        mkdir /path/to/new/directory

      • To delete a file on the remote system, run:

        1
        rm /path/to/remote/file

      • To delete a directory on the remote system, run:

        1
        rmdir /path/to/remote/directory

      • To rename a file or directory on the remote system, run:

        1
        rename /path/to/old/name /path/to/new/name

    7. Exiting the SFTP session:
    8. To exit the SFTP session and disconnect from the remote system, run:

      1
      exit

    Conclusion

    Transferring files over SSH is a secure and efficient way to exchange data between computers. By using tools like scp, rsync, and sftp, you can easily transfer files over SSH while maintaining the confidentiality and integrity of your data. Familiarize yourself with these tools and their options to make the most of your file transfers in a secure environment.

    rsync scp SFTP SSH
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    free Command in Linux (Check Memory Uses)

    Setting Up an SFTP Server on CentOS/RHEL Systems

    A Practical Guide to Extracting Compressed Files in Linux

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Setting Up Angular on Ubuntu: Step-by-Step Guide
    • Converting UTC Date and Time to Local Time in Linux
    • Git Restore: Functionality and Practical Examples
    • Git Switch: Functionality and Practical Examples
    • Git Switch vs. Checkout: A Detailed Comparison with Examples
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.