Shell script for file transfer in CentOS 7

     



To set up a shell script for file transfer in CentOS 7, you can use the scp (secure copy) command, which is a standard tool for securely transferring files between a local host and a remote host over SSH (Secure Shell).



Here's a basic example of a shell script that uses scp to transfer a file from a local directory to a remote server: 

Create a new shell script file (e.g., transfer_file.sh) using a text editor:

nano transfer_file.sh


Add the following content to the script file:  

#!/bin/bash


# Define variables

LOCAL_FILE="/path/to/local/file.txt"

REMOTE_USER="username"

REMOTE_HOST="remote_host_ip_or_domain"

REMOTE_DIR="/path/to/remote/directory/"


# Transfer file using scp

scp "$LOCAL_FILE" "$REMOTE_USER"@"$REMOTE_HOST":"$REMOTE_DIR"



Replace the placeholder values with your actual file path, remote server username, remote server IP address or domain, and remote directory path.

Save the file and exit the text editor.


Make the script executable:


Now you can execute the script to transfer the file:


./transfer_file.sh

This will transfer the specified file from the local host to the remote server using scp.

Make sure that the SSH service is running on the remote server, and the user specified in the script has the necessary permissions to write to the remote directory. Additionally, ensure that the remote server is accessible from the local host over SSH.

You can customize this script further based on your specific requirements, such as handling error conditions, transferring multiple files, or including additional options for scp (e.g., specifying SSH port, using SSH keys for authentication, etc.




                      

Comments