How Automate SFTP Transfer in Linux Shell Scripting?


Angela Bailey

In the world of Linux shell scripting, automating SFTP (Secure File Transfer Protocol) transfers is a powerful tool that can save time and effort. With the use of a few simple commands and scripts, you can easily set up automated file transfers between remote servers and your local machine. In this tutorial, we will explore how to automate SFTP transfers in Linux shell scripting.


Before we dive into the details, let’s make sure we have everything we need:

  • A Linux system: This tutorial assumes you are running a Linux distribution such as Ubuntu or CentOS.
  • OpenSSH installed: Ensure that you have OpenSSH installed on your system. If not, you can install it using the package manager specific to your distribution.

Step 1: Generating SSH Keys

The first step in setting up automated SFTP transfers is generating SSH keys. SSH keys allow for secure authentication between systems without the need for passwords. To generate SSH keys, follow these steps:

  1. Open a terminal: Launch a terminal on your local machine.
  2. Generate SSH keys: Run the following command to generate a new SSH key pair:
    $ ssh-keygen -t rsa
  3. Choose a file name: You will be prompted to choose a file name and location for your key pair. Press Enter to accept the default values.
  4. Create a passphrase (optional): You can choose to create a passphrase for added security. Press Enter if you want an empty passphrase.
  5. Key generation complete: Once the key pair is generated, you will see a success message in the terminal.

Step 2: Setting up SFTP Script

Now that we have our SSH keys, we can proceed to set up the SFTP script. The script will contain the necessary commands to establish an SFTP connection and transfer files between servers.

Create a new file using your preferred text editor and give it a meaningful name, such as Make sure to include the .sh extension to indicate that it is a shell script.

In the newly created file, we need to add the necessary content:


# Variables

# SFTP command
sftp -i "$KEY_PATH" "$USER"@"$HOST" <<EOF
  # SFTP commands go here

Note: Remember to replace your_remote_host, your_username, and /path/to/your/private/key with your specific values.

Step 3: Adding SFTP Commands

Now it’s time to add the actual SFTP commands within the script. Here are some commonly used commands:

  • cd: Change directory on the remote server.
  • lcd: Change directory on your local machine.
  • put: Upload files from your local machine to the remote server.
  • get: Download files from the remote server to your local machine.
  • rm: Remove files on the remote server.
  • ls: List files and directories on the remote server.

You can use these commands, along with any other SFTP commands you may need, to automate your file transfers. An example script to upload a file to a remote server might look like this:

# SFTP command
sftp -i “$KEY_PATH” “$USER”@”$HOST” <<EOF
cd /remote/directory
put /local/directory/file.txt

This script changes to the desired remote directory, uploads a file from the local directory, and finally quits the SFTP session.

Step 4: Making the Script Executable

To make our script executable, we need to change its permissions. Open a terminal and navigate to the directory where your script is located. Then, run the following command:

$ chmod +x

This command grants execute permissions to the owner of the file.

Step 5: Running the Script

Finally, we can run our script by executing it in a terminal. Navigate to the directory where your script is located and run the following command:

$ ./

If everything is set up correctly, you should see the file transfer taking place and receive a success message once the script finishes executing.


In this tutorial, we have explored how to automate SFTP transfers in Linux shell scripting. By generating SSH keys, setting up an SFTP script, and adding the necessary commands, you can easily automate file transfers between servers.

This can greatly simplify your workflow and save valuable time. With these techniques in your arsenal, you are now equipped to handle automated SFTP transfers in your Linux shell scripts.

Remember to always double-check your scripts and ensure that you have proper permissions and configurations in place for secure file transfers.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy